Spaces:
Running
Running
File size: 7,655 Bytes
77ebc6a d9836ef c7e7da5 d9836ef 35103e1 d9836ef 77ebc6a d9836ef 77ebc6a d9836ef a07d469 77ebc6a a07d469 77ebc6a a07d469 d9836ef 6b6680b d9836ef 6b6680b d9836ef 6b6680b d9836ef 6b6680b d9836ef 27fff89 d9836ef 77ebc6a d9836ef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
using System.Linq;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.Listen(System.Net.IPAddress.Any, 7860); // Listen on all network interfaces on port 5000
});
builder.Services.AddDbContext<QuoteDbContext>(options =>
options.UseSqlite("Data Source=quotes.db"));
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
const int MaxPageSize = 100;
app.UseHttpsRedirection();
app.MapGet("/quotes", async (QuoteDbContext db, int pageNumber = 1, int pageSize = 10) =>
{
if (pageNumber < 1) pageNumber = 1;
if (pageSize < 1) pageSize = 10;
pageSize = Math.Min(pageSize, MaxPageSize); // Limit pageSize to MaxPageSize
// var quotes = await db.Quotes
// .Skip((pageNumber - 1) * pageSize)
// .Take(pageSize)
// .ToListAsync();
var quotes = GlobalData.Quotes
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();
return Results.Ok(quotes);
});
app.MapGet("/quotes/{id}", async (int id, QuoteDbContext db) =>
await db.Quotes.FindAsync(id) is Quote quote
? Results.Ok(quote)
: Results.NotFound("Quote not found"));
app.MapPost("/quotes", async (Quote newQuote, QuoteDbContext db) =>
{
db.Quotes.Add(newQuote);
await db.SaveChangesAsync();
return Results.Created($"/quotes/{newQuote.Id}", newQuote);
});
app.MapPut("/quotes/{id}", async (int id, Quote updatedQuote, QuoteDbContext db) =>
{
var quote = await db.Quotes.FindAsync(id);
if (quote is null) return Results.NotFound("Quote not found");
quote.Author = updatedQuote.Author;
quote.QuoteText = updatedQuote.QuoteText;
quote.Source = updatedQuote.Source;
quote.Book = updatedQuote.Book;
quote.Categories = updatedQuote.Categories;
quote.Url = updatedQuote.Url;
quote.Isbn = updatedQuote.Isbn;
quote.Language = updatedQuote.Language;
quote.OriginalLanguage = updatedQuote.OriginalLanguage;
await db.SaveChangesAsync();
return Results.NoContent();
});
app.MapDelete("/quotes/{id}", async (int id, QuoteDbContext db) =>
{
var quote = await db.Quotes.FindAsync(id);
if (quote is null) return Results.NotFound("Quote not found");
db.Quotes.Remove(quote);
await db.SaveChangesAsync();
return Results.NoContent();
});
// Random quote endpoint
app.MapGet("/quotes/random", async (QuoteDbContext db) =>
{
var quoteCount = GlobalData.Quotes.Count(); //await db.Quotes.CountAsync();
if (quoteCount == 0)
return Results.NotFound("No quotes available.");
var random = new Random();
var randomIndex = random.Next(quoteCount);
var randomQuote = GlobalData.Quotes[randomIndex]; //await db.Quotes.Skip(randomIndex).Take(1).FirstOrDefaultAsync();
return randomQuote != null ? Results.Ok(randomQuote) : Results.NotFound("No quote found.");
});
// Search quotes by author, categories, book, or quoteText with pagination
app.MapGet("/quotes/search", async (string query, QuoteDbContext db, int pageNumber = 1, int pageSize = 10) =>
{
var queryQuotes = GlobalData.Quotes.AsQueryable(); //db.Quotes.AsQueryable();
if (!string.IsNullOrWhiteSpace(query))
{
query = query.ToLower();
queryQuotes = queryQuotes.Where(q =>
q.Author.ToLower().Contains(query) ||
(q.Categories != null && q.Categories.ToLower().Contains(query)) ||
(q.Book != null && q.Book.ToLower().Contains(query)) ||
q.QuoteText.ToLower().Contains(query)
);
}
if (pageNumber < 1) pageNumber = 1;
if (pageSize < 1) pageSize = 10;
pageSize = Math.Min(pageSize, MaxPageSize); // Limit pageSize to MaxPageSize
var paginatedQuotes = queryQuotes
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();
return paginatedQuotes.Any() ? Results.Ok(paginatedQuotes) : Results.NotFound("No matching quotes found.");
});
app.MapGet("/health", () => Results.Ok(new { Status = "Healthy", Timestamp = DateTime.UtcNow }));
async Task SaveSource1Async(string jsonFilePath, QuoteDbContext db)
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "data", jsonFilePath);
if (!File.Exists(path))
throw new FileNotFoundException("The JSON file for seeding is missing.");
// var jsonString = await File.ReadAllTextAsync(path);
// // Deserialize JSON to a list of dictionaries
// var dictionaryList = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(jsonString);
// if (dictionaryList == null)
// throw new InvalidOperationException("Failed to deserialize JSON into dictionary list.");
// Fields
// ==========
// Quote (string)
// Author (string)
// Tags (list<string>)
// Category (string)
// foreach (var dictionary in dictionaryList)
// {
// var tags = "";
// if(dictionary.TryGetValue("Tags", out var categories)) {
// var list = JsonSerializer.Deserialize<List<string>>(categories.ToString());
// tags = String.Join(",", list.Select(x=> x.Trim().Trim('"')));
// }
// var quote = new Quote
// {
// Author = dictionary.TryGetValue("Author", out var author) ? author.ToString() : null,
// QuoteText = dictionary.TryGetValue("Quote", out var quoteText) ? quoteText.ToString() : null,
// Categories = tags,
// };
// db.Quotes.Add(quote);
// }
// TODO: import data and sanitize relevant fields: Trim and Trim('"')
//save for each seed file
await db.SaveChangesAsync();
}
async Task SaveSource2Async(string jsonFile, QuoteDbContext db)
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "data", jsonFile);
if (!File.Exists(path))
throw new FileNotFoundException("The JSON file for seeding is missing.");
// Fields
// ==========
// content (string)
// author (string)
// tags (list<string>)
// TODO: import data and sanitize relevant fields: Trim and Trim('"')
//save for each seed file
await db.SaveChangesAsync();
}
async Task SaveSource3Async(string csvFile, QuoteDbContext db)
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "data", csvFile);
if (!File.Exists(path))
throw new FileNotFoundException("The CSV file for seeding is missing.");
// Fields
// ==========
// quote (string)
// author (string)
// category (string)
// TODO: import data and sanitize relevant fields: Trim and Trim('"')
//save for each seed file
await db.SaveChangesAsync();
}
// Seed database
async Task SeedDatabase(QuoteDbContext db)
{
if (await db.Quotes.AnyAsync())
return; // Database is already seeded
await SaveSource1Async("source1.json", db);
await SaveSource2Async("source2.json", db);
await SaveSource3Async("source3.csv", db);
}
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<QuoteDbContext>();
db.Database.EnsureCreated();
GlobalData.Quotes = await db.Quotes.ToListAsync();
await SeedDatabase(db);
}
app.Run();
|