Florin Bobiș commited on
Commit
6b6680b
1 Parent(s): a60f4f7

fixed search

Browse files
Files changed (2) hide show
  1. Program.cs +15 -16
  2. 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 search, QuoteDbContext db, int pageNumber = 1, int pageSize = 10) =>
105
  {
106
- var query = GlobalData.Quotes.AsQueryable(); //db.Quotes.AsQueryable();
107
-
108
- if (!string.IsNullOrWhiteSpace(search))
109
- query = query.Where(q => q.Author.Contains(search));
110
-
111
- if (!string.IsNullOrWhiteSpace(search))
112
- query = query.Where(q => q.Categories.Contains(search));
113
-
114
- if (!string.IsNullOrWhiteSpace(search))
115
- query = query.Where(q => q.Book.Contains(search));
116
-
117
- if (!string.IsNullOrWhiteSpace(search))
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 = await query
125
  .Skip((pageNumber - 1) * pageSize)
126
  .Take(pageSize)
127
- .ToListAsync();
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
- @Localhost = http://localhost:7860
2
- @Production = https://florinbobis-quotes-net.hf.space
 
3
 
4
- GET {{Localhost}}/quotes/random
 
5
  Accept: application/json
6
 
7
- ### Production call
8
- GET {{Production}}/quotes/random
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