-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
56 lines (50 loc) · 1.83 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
using Microsoft.OpenApi.Models;
using PizzaStore.Models;
using System.Net.NetworkInformation;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Http.HttpResults;
var builder = WebApplication.CreateBuilder(args);
//Adding sqlLite to persist data
string connectionString = builder.Configuration.GetConnectionString("Pizzas") ?? "Data Source=Pizzas.db";
builder.Services.AddEndpointsApiExplorer();
// adding the context to the in memory database
// this configure the context class
builder.Services.AddSqlite<PizzaDb>(connectionString);
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "PizzaStore API", Description = "Making the Pizzas you love", Version = "v1" });
});
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "PizzaStore API V1");
});
app.MapGet("/", (PizzaDb db) => db.Pizzas);
app.MapGet("/pizzas", async (PizzaDb db) => await db.Pizzas.ToListAsync());
app.MapGet("/pizza/{id}", async (PizzaDb db, int id) => await db.Pizzas.FindAsync(id));
app.MapPut("/pizza/{id}", async (PizzaDb db, Pizza updatePizza, int id) => {
var pizza = await db.Pizzas.FindAsync(id);
if (pizza is null)
return Results.NotFound();
pizza.Description = updatePizza.Description;
pizza.Name = updatePizza.Name;
await db.SaveChangesAsync();
return Results.NoContent();
});
app.MapPost("/pizza", async (PizzaDb db, Pizza pizza) =>
{
await db.Pizzas.AddAsync(pizza);
await db.SaveChangesAsync();
return Results.Created($"/pizza/{pizza.Id}", pizza);
}
);
app.MapDelete("/pizza/{id}", async (PizzaDb db, int id) => {
var pizza = await db.Pizzas.FindAsync(id);
if (pizza is null)
return Results.NotFound();
db.Pizzas.Remove(pizza);
await db.SaveChangesAsync();
return Results.NoContent();
});
app.Run();