You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.1 KiB
80 lines
2.1 KiB
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.OpenApi.Models;
|
|
using MinimumWebAPIWithEF.DB;
|
|
using MinimumWebAPIWithEF.Models;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
var connnectionString = builder.Configuration.GetConnectionString("Pizzas") ?? "Data Source=Pizzas.db";
|
|
|
|
// Add services to the container.
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(c =>
|
|
{
|
|
c.SwaggerDoc("v1", new OpenApiInfo
|
|
{
|
|
Title = "PizzaStore API",
|
|
Description = "Making the Pizzas you love",
|
|
Version = "v1"
|
|
});
|
|
});
|
|
|
|
//builder.Services.AddDbContext<PizzaDb>(options => options.UseInMemoryDatabase("items"));
|
|
builder.Services.AddSqlite<PizzaDb>(connnectionString);
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(c =>
|
|
{
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "PizzaStore API V1");
|
|
});
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.MapGet("/", () => "Hello Pizza!");
|
|
|
|
// Pizza routing map
|
|
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.MapPost("/pizza", async (PizzaDb db, Pizza pizza) =>
|
|
{
|
|
await db.Pizzas.AddAsync(pizza);
|
|
await db.SaveChangesAsync();
|
|
|
|
return Results.Created($"/pizza/{pizza.Id}", pizza);
|
|
});
|
|
|
|
app.MapPut("/pizza/{id}", async (PizzaDb db, Pizza updatedPizza, int id) =>
|
|
{
|
|
var pizza = await db.Pizzas.FindAsync(id);
|
|
if (pizza is null)
|
|
return Results.NotFound();
|
|
|
|
pizza.Name = updatedPizza.Name;
|
|
pizza.Description = updatedPizza.Description;
|
|
await db.SaveChangesAsync();
|
|
|
|
return Results.NoContent();
|
|
});
|
|
|
|
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.Ok();
|
|
});
|
|
|
|
app.Run();
|
|
|