Add in-memory database and CRUD routes
This commit is contained in:
parent
bb7df24ab8
commit
6eb0501ef1
|
@ -0,0 +1,42 @@
|
|||
namespace PizzaStore.Db;
|
||||
|
||||
public record Pizza {
|
||||
public int Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public class PizzaDb {
|
||||
private static List<Pizza> _pizzas = [
|
||||
new() { Id = 1, Name = "Montemagno, Pizza shaped like a great mountain" },
|
||||
new() { Id = 2, Name = "The Galloway, Pizza shaped like a submarine, silent but deadly" },
|
||||
new() { Id = 3, Name = "The Noring, Pizza shaped like a Viking helmet, where's the mead" }
|
||||
];
|
||||
|
||||
public static List<Pizza> GetPizzas() {
|
||||
return _pizzas;
|
||||
}
|
||||
|
||||
public static Pizza? GetPizza(int id) {
|
||||
return _pizzas.SingleOrDefault(p => p.Id == id);
|
||||
}
|
||||
|
||||
public static Pizza CreatePizza(Pizza pizza) {
|
||||
_pizzas.Add(pizza);
|
||||
return pizza;
|
||||
}
|
||||
|
||||
public static Pizza UpdatePizza(Pizza pizza) {
|
||||
_pizzas = _pizzas.Select(p => {
|
||||
if (p.Id == pizza.Id) {
|
||||
p.Name = pizza.Name;
|
||||
}
|
||||
return p;
|
||||
}).ToList();
|
||||
|
||||
return pizza;
|
||||
}
|
||||
|
||||
public static void RemovePizza(int id) {
|
||||
_pizzas = _pizzas.FindAll(p => p.Id != id).ToList();
|
||||
}
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
using PizzaStore.Db;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
@ -14,4 +16,10 @@ app.UseSwaggerUI(c => {
|
|||
|
||||
app.MapGet("/", () => "Hello World!");
|
||||
|
||||
app.MapGet("/pizzas/{id}", (int id) => PizzaDb.GetPizza(id));
|
||||
app.MapGet("/pizzas", () => PizzaDb.GetPizzas());
|
||||
app.MapPost("/pizzas", (Pizza pizza) => PizzaDb.CreatePizza(pizza));
|
||||
app.MapPut("/pizzas", (Pizza pizza) => PizzaDb.UpdatePizza(pizza));
|
||||
app.MapDelete("/pizzas/{id}", (int id) => PizzaDb.RemovePizza(id));
|
||||
|
||||
app.Run();
|
||||
|
|
Loading…
Reference in New Issue