Spaces:
Running
Running
Florin Bobiș
commited on
Commit
•
a07d469
1
Parent(s):
b944c8a
DB mapping issue + new endpoint for random quote
Browse files- DbContext/QuoteDbContext.cs +4 -0
- Program.cs +15 -0
DbContext/QuoteDbContext.cs
CHANGED
@@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore;
|
|
2 |
using System;
|
3 |
using System.Collections.Generic;
|
4 |
using System.ComponentModel.DataAnnotations;
|
|
|
5 |
|
6 |
public class QuoteDbContext : DbContext
|
7 |
{
|
@@ -50,6 +51,7 @@ public class Quote
|
|
50 |
|
51 |
[Required]
|
52 |
[MaxLength(1000)]
|
|
|
53 |
public string QuoteText { get; set; } // Indexed and unique column
|
54 |
|
55 |
[MaxLength(255)]
|
@@ -71,7 +73,9 @@ public class Quote
|
|
71 |
public string? Language { get; set; } // Nullable column
|
72 |
|
73 |
[MaxLength(50)]
|
|
|
74 |
public string? OriginalLanguage { get; set; } // Nullable column
|
75 |
|
|
|
76 |
public DateTime DateCreated { get; set; } = DateTime.Now; // Default to current datetime
|
77 |
}
|
|
|
2 |
using System;
|
3 |
using System.Collections.Generic;
|
4 |
using System.ComponentModel.DataAnnotations;
|
5 |
+
using System.ComponentModel.DataAnnotations.Schema;
|
6 |
|
7 |
public class QuoteDbContext : DbContext
|
8 |
{
|
|
|
51 |
|
52 |
[Required]
|
53 |
[MaxLength(1000)]
|
54 |
+
[Column("quote")]
|
55 |
public string QuoteText { get; set; } // Indexed and unique column
|
56 |
|
57 |
[MaxLength(255)]
|
|
|
73 |
public string? Language { get; set; } // Nullable column
|
74 |
|
75 |
[MaxLength(50)]
|
76 |
+
[Column("original_language")]
|
77 |
public string? OriginalLanguage { get; set; } // Nullable column
|
78 |
|
79 |
+
[Column("created_at")]
|
80 |
public DateTime DateCreated { get; set; } = DateTime.Now; // Default to current datetime
|
81 |
}
|
Program.cs
CHANGED
@@ -77,6 +77,21 @@ app.MapDelete("/quotes/{id}", async (int id, QuoteDbContext db) =>
|
|
77 |
return Results.NoContent();
|
78 |
});
|
79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
// Search quotes by author, categories, book, or quoteText with pagination
|
81 |
app.MapGet("/quotes/search", async (string search, QuoteDbContext db, int pageNumber = 1, int pageSize = 10) =>
|
82 |
{
|
|
|
77 |
return Results.NoContent();
|
78 |
});
|
79 |
|
80 |
+
// Random quote endpoint
|
81 |
+
app.MapGet("/quotes/random", async (QuoteDbContext db) =>
|
82 |
+
{
|
83 |
+
var quoteCount = await db.Quotes.CountAsync();
|
84 |
+
if (quoteCount == 0)
|
85 |
+
return Results.NotFound("No quotes available.");
|
86 |
+
|
87 |
+
var random = new Random();
|
88 |
+
var randomIndex = random.Next(quoteCount);
|
89 |
+
|
90 |
+
var randomQuote = await db.Quotes.Skip(randomIndex).Take(1).FirstOrDefaultAsync();
|
91 |
+
|
92 |
+
return randomQuote != null ? Results.Ok(randomQuote) : Results.NotFound("No quote found.");
|
93 |
+
});
|
94 |
+
|
95 |
// Search quotes by author, categories, book, or quoteText with pagination
|
96 |
app.MapGet("/quotes/search", async (string search, QuoteDbContext db, int pageNumber = 1, int pageSize = 10) =>
|
97 |
{
|