-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
112 lines (83 loc) · 3.1 KB
/
Program.cs
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
using HttpVerbsApi.DbContexts;
using HttpVerbsApi.Models;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<ProductDbContext>(opt => opt.UseInMemoryDatabase("ProductDb"));
var app = builder.Build();
// Entity Framework In-Memory Initial Data Generation
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<ProductDbContext>();
await context.GenerateInitialData();
}
app.MapMethods("/products", new[] { "OPTIONS" }, (HttpContext context) =>
{
context.Response.Headers.AppendCommaSeparatedValues
("Allow", ["GET, POST, PUT, PATCH, DELETE, OPTIONS"]
);
return Results.NoContent();
});
app.MapGet("/products", async (ProductDbContext db) =>
Results.Ok(await db.Products.ToListAsync()));
app.MapGet("/products/{id}", async (long id, ProductDbContext db) =>
await db.Products.FindAsync(id)
is Product product
? Results.Ok(product)
: Results.NotFound());
app.MapPost("/products", async (Product product, ProductDbContext db) =>
{
bool productExists = await db.Products.AnyAsync(x => x.Id == product.Id);
// HTTP 409 Error Response for the resource already exists
if (productExists) return Results.Conflict();
await db.Products.AddAsync(product);
await db.SaveChangesAsync();
return Results.Created($"/products/{product.Id}", product);
});
app.MapPut("/products/{id}", async (long id, Product product, ProductDbContext db) =>
{
if (string.IsNullOrEmpty(product?.Name)
|| string.IsNullOrEmpty(product?.Description))
{
// HTTP 400 for the invalid request model
return Results.BadRequest();
}
var productEntity = await db.Products.FindAsync(id);
if (productEntity is null) return Results.NotFound();
productEntity.Name = product.Name;
productEntity.Description = product.Description;
await db.SaveChangesAsync();
return Results.NoContent();
});
app.MapPatch("/products/{id}", async (long id, HttpContext httpContext, ProductDbContext db) =>
{
if (!httpContext.Request.HasJsonContentType())
{
Results.BadRequest();
}
JsonPatchDocument? patchDocument;
using (var streamReader = new StreamReader(httpContext.Request.Body))
{
var httpContent = await streamReader.ReadToEndAsync();
patchDocument = JsonConvert.DeserializeObject<JsonPatchDocument>(httpContent);
}
if (patchDocument is null) return Results.BadRequest();
var productEntity = await db.Products.FindAsync(id);
if (productEntity is null) return Results.NotFound();
patchDocument.ApplyTo(productEntity);
await db.SaveChangesAsync();
return Results.NoContent();
});
app.MapDelete("/products/{id}", async (long id, ProductDbContext db) =>
{
if (await db.Products.FindAsync(id) is Product product)
{
db.Products.Remove(product);
await db.SaveChangesAsync();
return Results.NoContent();
}
return Results.NotFound();
});
app.Run();