Spaces:
Running
Running
Florin Bobiș
commited on
Commit
•
724036f
1
Parent(s):
ce73f00
added tag support as in quotable api
Browse files- Program.cs +26 -8
Program.cs
CHANGED
@@ -105,19 +105,39 @@ app.MapGet("/quotes", async (QuoteDbContext db, int pageNumber = 1, int pageSize
|
|
105 |
// });
|
106 |
|
107 |
// Random quote endpoint
|
108 |
-
app.MapGet("/quotes/random", async (QuoteDbContext db, [FromQuery] DataSetSources dataset = DataSetSources.all) =>
|
109 |
{
|
110 |
if (!Enum.IsDefined(typeof(DataSetSources), dataset))
|
111 |
return Results.BadRequest("Invalid dataset.");
|
112 |
|
113 |
int quoteCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
if (dataset != DataSetSources.all)
|
115 |
{
|
116 |
-
quoteCount =
|
117 |
}
|
118 |
else
|
119 |
{
|
120 |
-
quoteCount =
|
121 |
}
|
122 |
|
123 |
if (quoteCount == 0)
|
@@ -125,11 +145,10 @@ app.MapGet("/quotes/random", async (QuoteDbContext db, [FromQuery] DataSetSource
|
|
125 |
|
126 |
var random = new Random();
|
127 |
var randomIndex = random.Next(quoteCount);
|
128 |
-
|
129 |
-
Quote? randomQuote = null;
|
130 |
if (dataset != DataSetSources.all)
|
131 |
{
|
132 |
-
randomQuote =
|
133 |
.Where(q => q.DataSet == dataset.ToString())
|
134 |
.Skip(randomIndex)
|
135 |
.Take(1)
|
@@ -137,7 +156,7 @@ app.MapGet("/quotes/random", async (QuoteDbContext db, [FromQuery] DataSetSource
|
|
137 |
}
|
138 |
else
|
139 |
{
|
140 |
-
randomQuote =
|
141 |
}
|
142 |
return randomQuote != null ? Results.Ok(randomQuote) : Results.NotFound("No quote found.");
|
143 |
});
|
@@ -281,7 +300,6 @@ using (var scope = app.Services.CreateScope())
|
|
281 |
{
|
282 |
var db = scope.ServiceProvider.GetRequiredService<QuoteDbContext>();
|
283 |
//db.Database.EnsureCreated();
|
284 |
-
|
285 |
GlobalData.Quotes = await db.Quotes.ToListAsync();
|
286 |
//await SeedDatabase(db);
|
287 |
}
|
|
|
105 |
// });
|
106 |
|
107 |
// Random quote endpoint
|
108 |
+
app.MapGet("/quotes/random", async (QuoteDbContext db, [FromQuery] DataSetSources dataset = DataSetSources.all, [FromQuery] string tags = "") =>
|
109 |
{
|
110 |
if (!Enum.IsDefined(typeof(DataSetSources), dataset))
|
111 |
return Results.BadRequest("Invalid dataset.");
|
112 |
|
113 |
int quoteCount = 0;
|
114 |
+
var quotes = GlobalData.Quotes;
|
115 |
+
|
116 |
+
var tagsList = new List<string>();
|
117 |
+
if (!string.IsNullOrEmpty(tags))
|
118 |
+
{
|
119 |
+
try {
|
120 |
+
tagsList = tags.Split('|', StringSplitOptions.RemoveEmptyEntries).Select(x => x.ToLower()).ToList();
|
121 |
+
} catch (Exception) {
|
122 |
+
return Results.BadRequest("Invalid tags.");
|
123 |
+
}
|
124 |
+
}
|
125 |
+
|
126 |
+
if (tagsList.Count > 0)
|
127 |
+
{
|
128 |
+
quotes = quotes.Where(q => !string.IsNullOrEmpty(q.Categories) && q.Categories.Split(',').Any(x => tagsList.Select(y => y.ToLower()).Contains(x))).ToList();
|
129 |
+
}
|
130 |
+
|
131 |
+
Quote? randomQuote = null;
|
132 |
+
|
133 |
+
|
134 |
if (dataset != DataSetSources.all)
|
135 |
{
|
136 |
+
quoteCount = quotes.Count(q => q.DataSet == dataset.ToString());
|
137 |
}
|
138 |
else
|
139 |
{
|
140 |
+
quoteCount = quotes.Count(); //await db.Quotes.CountAsync();
|
141 |
}
|
142 |
|
143 |
if (quoteCount == 0)
|
|
|
145 |
|
146 |
var random = new Random();
|
147 |
var randomIndex = random.Next(quoteCount);
|
148 |
+
|
|
|
149 |
if (dataset != DataSetSources.all)
|
150 |
{
|
151 |
+
randomQuote = quotes
|
152 |
.Where(q => q.DataSet == dataset.ToString())
|
153 |
.Skip(randomIndex)
|
154 |
.Take(1)
|
|
|
156 |
}
|
157 |
else
|
158 |
{
|
159 |
+
randomQuote = quotes[randomIndex]; //await db.Quotes.Skip(randomIndex).Take(1).FirstOrDefaultAsync();
|
160 |
}
|
161 |
return randomQuote != null ? Results.Ok(randomQuote) : Results.NotFound("No quote found.");
|
162 |
});
|
|
|
300 |
{
|
301 |
var db = scope.ServiceProvider.GetRequiredService<QuoteDbContext>();
|
302 |
//db.Database.EnsureCreated();
|
|
|
303 |
GlobalData.Quotes = await db.Quotes.ToListAsync();
|
304 |
//await SeedDatabase(db);
|
305 |
}
|