-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEndpoints.cs
More file actions
78 lines (67 loc) · 2.81 KB
/
Endpoints.cs
File metadata and controls
78 lines (67 loc) · 2.81 KB
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
using Microsoft.EntityFrameworkCore;
namespace MinimalBackend;
public static class Endpoints
{
public static void ConfigureEndpoints(this WebApplication app)
{
ConfigureDemoEndpoints(app);
ConfigureProductEndpoints(app);
ConfigureMenuEndpoints(app);
}
private static void ConfigureDemoEndpoints(IEndpointRouteBuilder app)
{
app.MapGet("/demo/foo", () => "bar");
app.MapGet("/demo/add", (int num1, int? num2) => $"Your result: {num1 + num2 ?? 0}");
}
private static void ConfigureMenuEndpoints(IEndpointRouteBuilder app)
{
app.MapGet("/menu", async (DataContext ctx) => await GetMenus(ctx).ToListAsync());
app.MapGet("/menu/{no:int}/total-price", async (DataContext ctx, int no) =>
{
// not handling 404 here to demonstrate a more complex query (see below for status code return)
return await GetMenus(ctx).Where(m => m.MenuNo == no)
.SelectMany(m => m.Products)
.Select(mi => mi.Amount * mi.Product.Price)
.FirstOrDefaultAsync();
});
app.MapDelete("/menu/{no:int}", async (DataContext ctx, int no) =>
{
var menu = await ctx.Menus.FirstOrDefaultAsync(m => m.MenuNo == no);
if (menu is null)
{
return Results.NotFound();
}
ctx.Menus.Remove(menu);
await ctx.SaveChangesAsync();
return Results.NoContent();
});
return;
static IQueryable<Menu> GetMenus(DataContext ctx) =>
ctx.Menus
.Include(m => m.Products)
.ThenInclude(p => p.Product);
}
private static void ConfigureProductEndpoints(IEndpointRouteBuilder app)
{
const string BasePath = "/product";
app.MapGet(BasePath, async (DataContext ctx) => await ctx.Products.ToListAsync());
app.MapGet($"{BasePath}/{{id:int}}", async (DataContext ctx, int id) =>
await ctx.Products.FirstOrDefaultAsync(p => p.Id == id));
app.MapGet($"{BasePath}/type", async (DataContext ctx, ProductTypes type) =>
await ctx.Products.Where(p => p.Type == type).ToListAsync());
app.MapPost(BasePath, async (DataContext ctx, NewProduct newProduct) =>
{
var newEntity = new Product
{
Price = newProduct.Price,
Hot = newProduct.Hot,
Name = newProduct.Name,
Type = newProduct.Type,
Vegetarian = newProduct.Vegetarian,
Ingredients = newProduct.Ingredients.ToList()
};
await ctx.AddAsync(newEntity);
await ctx.SaveChangesAsync();
});
}
}