Spaces:
Runtime error
Runtime error
Florin Bobiș
commited on
Commit
•
77ebc6a
1
Parent(s):
27fff89
Super fast api
Browse files- .vscode/tasks.json +3 -3
- Dockerfile.original +0 -16
- GlobalData.cs +4 -0
- Program.cs +12 -6
- QuotesAPI.http +2 -2
.vscode/tasks.json
CHANGED
@@ -7,7 +7,7 @@
|
|
7 |
"type": "process",
|
8 |
"args": [
|
9 |
"build",
|
10 |
-
"${workspaceFolder}/
|
11 |
"/property:GenerateFullPaths=true",
|
12 |
"/consoleloggerparameters:NoSummary;ForceNoAlign"
|
13 |
],
|
@@ -19,7 +19,7 @@
|
|
19 |
"type": "process",
|
20 |
"args": [
|
21 |
"publish",
|
22 |
-
"${workspaceFolder}/
|
23 |
"/property:GenerateFullPaths=true",
|
24 |
"/consoleloggerparameters:NoSummary;ForceNoAlign"
|
25 |
],
|
@@ -33,7 +33,7 @@
|
|
33 |
"watch",
|
34 |
"run",
|
35 |
"--project",
|
36 |
-
"${workspaceFolder}/
|
37 |
],
|
38 |
"problemMatcher": "$msCompile"
|
39 |
}
|
|
|
7 |
"type": "process",
|
8 |
"args": [
|
9 |
"build",
|
10 |
+
"${workspaceFolder}/QuotesAPI.sln",
|
11 |
"/property:GenerateFullPaths=true",
|
12 |
"/consoleloggerparameters:NoSummary;ForceNoAlign"
|
13 |
],
|
|
|
19 |
"type": "process",
|
20 |
"args": [
|
21 |
"publish",
|
22 |
+
"${workspaceFolder}/QuotesAPI.sln",
|
23 |
"/property:GenerateFullPaths=true",
|
24 |
"/consoleloggerparameters:NoSummary;ForceNoAlign"
|
25 |
],
|
|
|
33 |
"watch",
|
34 |
"run",
|
35 |
"--project",
|
36 |
+
"${workspaceFolder}/QuotesAPI.sln"
|
37 |
],
|
38 |
"problemMatcher": "$msCompile"
|
39 |
}
|
Dockerfile.original
DELETED
@@ -1,16 +0,0 @@
|
|
1 |
-
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
|
2 |
-
# current folder
|
3 |
-
WORKDIR /
|
4 |
-
|
5 |
-
# Copy everything
|
6 |
-
COPY . ./
|
7 |
-
# Restore as distinct layers
|
8 |
-
RUN dotnet restore
|
9 |
-
# Build and publish a release
|
10 |
-
RUN dotnet publish -c Release -o out
|
11 |
-
|
12 |
-
# Build runtime image
|
13 |
-
FROM mcr.microsoft.com/dotnet/aspnet:8.0
|
14 |
-
WORKDIR /
|
15 |
-
COPY --from=build-env /out .
|
16 |
-
ENTRYPOINT ["dotnet", "QuotesAPI.dll"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GlobalData.cs
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
public static class GlobalData
|
2 |
+
{
|
3 |
+
public static List<Quote> Quotes { get; set; }
|
4 |
+
}
|
Program.cs
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
using System.Text.Json;
|
2 |
using Microsoft.EntityFrameworkCore;
|
3 |
|
@@ -32,10 +33,14 @@ app.MapGet("/quotes", async (QuoteDbContext db, int pageNumber = 1, int pageSize
|
|
32 |
if (pageSize < 1) pageSize = 10;
|
33 |
pageSize = Math.Min(pageSize, MaxPageSize); // Limit pageSize to MaxPageSize
|
34 |
|
35 |
-
var quotes = await db.Quotes
|
|
|
|
|
|
|
|
|
36 |
.Skip((pageNumber - 1) * pageSize)
|
37 |
.Take(pageSize)
|
38 |
-
.
|
39 |
|
40 |
return Results.Ok(quotes);
|
41 |
});
|
@@ -84,22 +89,21 @@ app.MapDelete("/quotes/{id}", async (int id, QuoteDbContext db) =>
|
|
84 |
// Random quote endpoint
|
85 |
app.MapGet("/quotes/random", async (QuoteDbContext db) =>
|
86 |
{
|
87 |
-
var quoteCount = await db.Quotes.CountAsync();
|
88 |
if (quoteCount == 0)
|
89 |
return Results.NotFound("No quotes available.");
|
90 |
|
91 |
var random = new Random();
|
92 |
var randomIndex = random.Next(quoteCount);
|
93 |
|
94 |
-
var randomQuote = await db.Quotes.Skip(randomIndex).Take(1).FirstOrDefaultAsync();
|
95 |
-
|
96 |
return randomQuote != null ? Results.Ok(randomQuote) : Results.NotFound("No quote found.");
|
97 |
});
|
98 |
|
99 |
// Search quotes by author, categories, book, or quoteText with pagination
|
100 |
app.MapGet("/quotes/search", async (string search, QuoteDbContext db, int pageNumber = 1, int pageSize = 10) =>
|
101 |
{
|
102 |
-
var query = db.Quotes.AsQueryable();
|
103 |
|
104 |
if (!string.IsNullOrWhiteSpace(search))
|
105 |
query = query.Where(q => q.Author.Contains(search));
|
@@ -224,6 +228,8 @@ using (var scope = app.Services.CreateScope())
|
|
224 |
{
|
225 |
var db = scope.ServiceProvider.GetRequiredService<QuoteDbContext>();
|
226 |
db.Database.EnsureCreated();
|
|
|
|
|
227 |
await SeedDatabase(db);
|
228 |
}
|
229 |
|
|
|
1 |
+
using System.Linq;
|
2 |
using System.Text.Json;
|
3 |
using Microsoft.EntityFrameworkCore;
|
4 |
|
|
|
33 |
if (pageSize < 1) pageSize = 10;
|
34 |
pageSize = Math.Min(pageSize, MaxPageSize); // Limit pageSize to MaxPageSize
|
35 |
|
36 |
+
// var quotes = await db.Quotes
|
37 |
+
// .Skip((pageNumber - 1) * pageSize)
|
38 |
+
// .Take(pageSize)
|
39 |
+
// .ToListAsync();
|
40 |
+
var quotes = GlobalData.Quotes
|
41 |
.Skip((pageNumber - 1) * pageSize)
|
42 |
.Take(pageSize)
|
43 |
+
.ToList();
|
44 |
|
45 |
return Results.Ok(quotes);
|
46 |
});
|
|
|
89 |
// Random quote endpoint
|
90 |
app.MapGet("/quotes/random", async (QuoteDbContext db) =>
|
91 |
{
|
92 |
+
var quoteCount = GlobalData.Quotes.Count(); //await db.Quotes.CountAsync();
|
93 |
if (quoteCount == 0)
|
94 |
return Results.NotFound("No quotes available.");
|
95 |
|
96 |
var random = new Random();
|
97 |
var randomIndex = random.Next(quoteCount);
|
98 |
|
99 |
+
var randomQuote = GlobalData.Quotes[randomIndex]; //await db.Quotes.Skip(randomIndex).Take(1).FirstOrDefaultAsync();
|
|
|
100 |
return randomQuote != null ? Results.Ok(randomQuote) : Results.NotFound("No quote found.");
|
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));
|
|
|
228 |
{
|
229 |
var db = scope.ServiceProvider.GetRequiredService<QuoteDbContext>();
|
230 |
db.Database.EnsureCreated();
|
231 |
+
|
232 |
+
GlobalData.Quotes = await db.Quotes.ToListAsync();
|
233 |
await SeedDatabase(db);
|
234 |
}
|
235 |
|
QuotesAPI.http
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
@QuotesAPI_HostAddress = http://localhost:
|
2 |
|
3 |
-
GET {{QuotesAPI_HostAddress}}/
|
4 |
Accept: application/json
|
5 |
|
6 |
###
|
|
|
1 |
+
@QuotesAPI_HostAddress = http://localhost:7860
|
2 |
|
3 |
+
GET {{QuotesAPI_HostAddress}}/quotes/random
|
4 |
Accept: application/json
|
5 |
|
6 |
###
|