Spaces:
Runtime error
Runtime error
Florin Bobiș
commited on
Commit
•
6b6680b
1
Parent(s):
a60f4f7
fixed search
Browse files- Program.cs +15 -16
- QuotesAPI.http +8 -6
Program.cs
CHANGED
@@ -101,30 +101,29 @@ app.MapGet("/quotes/random", async (QuoteDbContext db) =>
|
|
101 |
});
|
102 |
|
103 |
// Search quotes by author, categories, book, or quoteText with pagination
|
104 |
-
app.MapGet("/quotes/search", async (string
|
105 |
{
|
106 |
-
var
|
107 |
-
|
108 |
-
if (!string.IsNullOrWhiteSpace(
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
query = query.Where(q => q.QuoteText.Contains(search));
|
119 |
|
120 |
if (pageNumber < 1) pageNumber = 1;
|
121 |
if (pageSize < 1) pageSize = 10;
|
122 |
pageSize = Math.Min(pageSize, MaxPageSize); // Limit pageSize to MaxPageSize
|
123 |
|
124 |
-
var paginatedQuotes =
|
125 |
.Skip((pageNumber - 1) * pageSize)
|
126 |
.Take(pageSize)
|
127 |
-
.
|
128 |
|
129 |
return paginatedQuotes.Any() ? Results.Ok(paginatedQuotes) : Results.NotFound("No matching quotes found.");
|
130 |
});
|
|
|
101 |
});
|
102 |
|
103 |
// Search quotes by author, categories, book, or quoteText with pagination
|
104 |
+
app.MapGet("/quotes/search", async (string query, QuoteDbContext db, int pageNumber = 1, int pageSize = 10) =>
|
105 |
{
|
106 |
+
var queryQuotes = GlobalData.Quotes.AsQueryable(); //db.Quotes.AsQueryable();
|
107 |
+
|
108 |
+
if (!string.IsNullOrWhiteSpace(query))
|
109 |
+
{
|
110 |
+
query = query.ToLower();
|
111 |
+
queryQuotes = queryQuotes.Where(q =>
|
112 |
+
q.Author.ToLower().Contains(query) ||
|
113 |
+
(q.Categories != null && q.Categories.ToLower().Contains(query)) ||
|
114 |
+
(q.Book != null && q.Book.ToLower().Contains(query)) ||
|
115 |
+
q.QuoteText.ToLower().Contains(query)
|
116 |
+
);
|
117 |
+
}
|
|
|
118 |
|
119 |
if (pageNumber < 1) pageNumber = 1;
|
120 |
if (pageSize < 1) pageSize = 10;
|
121 |
pageSize = Math.Min(pageSize, MaxPageSize); // Limit pageSize to MaxPageSize
|
122 |
|
123 |
+
var paginatedQuotes = queryQuotes
|
124 |
.Skip((pageNumber - 1) * pageSize)
|
125 |
.Take(pageSize)
|
126 |
+
.ToList();
|
127 |
|
128 |
return paginatedQuotes.Any() ? Results.Ok(paginatedQuotes) : Results.NotFound("No matching quotes found.");
|
129 |
});
|
QuotesAPI.http
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
-
@
|
2 |
-
@
|
|
|
3 |
|
4 |
-
|
|
|
5 |
Accept: application/json
|
6 |
|
7 |
-
###
|
8 |
-
GET {{
|
9 |
-
Accept: application/json
|
|
|
1 |
+
@host = http://localhost:7860
|
2 |
+
# @host = https://florinbobis-quotes-net.hf.space
|
3 |
+
@search = romance
|
4 |
|
5 |
+
### Random quote
|
6 |
+
GET {{host}}/quotes/random
|
7 |
Accept: application/json
|
8 |
|
9 |
+
### Search quote
|
10 |
+
GET {{host}}/quotes/search?query={{search}}&pageSize=3
|
11 |
+
Accept: application/json
|