2023-06-30 19:50:26 +02:00
|
|
|
using Microsoft.OpenApi.Models;
|
2025-05-13 07:44:16 +02:00
|
|
|
using PizzaStore.DB;
|
2023-06-30 19:50:26 +02:00
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
2025-05-13 07:44:16 +02:00
|
|
|
|
|
|
|
|
// Add services to the container
|
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
2023-06-30 19:50:26 +02:00
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
2025-05-13 07:44:16 +02:00
|
|
|
builder.Services.AddSwaggerGen(options =>
|
2023-06-30 19:50:26 +02:00
|
|
|
{
|
2025-05-13 07:44:16 +02:00
|
|
|
options.SwaggerDoc("v1", new OpenApiInfo
|
|
|
|
|
{
|
|
|
|
|
Title = "PizzaStore API",
|
|
|
|
|
Description = "Making the Pizzas you love",
|
|
|
|
|
Version = "v1",
|
|
|
|
|
Contact = new OpenApiContact
|
|
|
|
|
{
|
|
|
|
|
Name = "Pizza Support",
|
|
|
|
|
Email = "pizza@example.com"
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-06-30 19:50:26 +02:00
|
|
|
});
|
2025-05-13 07:44:16 +02:00
|
|
|
|
2023-06-30 19:50:26 +02:00
|
|
|
var app = builder.Build();
|
2025-05-13 07:44:16 +02:00
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline
|
2023-06-30 19:50:26 +02:00
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
2025-05-13 07:44:16 +02:00
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI(options =>
|
|
|
|
|
{
|
|
|
|
|
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
|
|
|
|
|
options.RoutePrefix = "swagger";
|
|
|
|
|
});
|
2023-06-30 19:50:26 +02:00
|
|
|
}
|
2025-05-13 07:44:16 +02:00
|
|
|
|
2023-06-30 19:50:26 +02:00
|
|
|
app.MapGet("/", () => "Hello World!");
|
2025-05-13 07:44:16 +02:00
|
|
|
|
|
|
|
|
// Define API endpoints with OpenAPI descriptions
|
|
|
|
|
var pizzas = app.MapGroup("/pizzas")
|
|
|
|
|
.WithTags("Pizzas")
|
|
|
|
|
.WithOpenApi();
|
|
|
|
|
|
|
|
|
|
// Get all pizzas
|
|
|
|
|
pizzas.MapGet("/", () => PizzaDB.GetPizzas())
|
|
|
|
|
.WithName("GetAllPizzas")
|
|
|
|
|
.WithSummary("Get all pizzas")
|
|
|
|
|
.WithDescription("Retrieves the complete list of available pizzas");
|
|
|
|
|
|
|
|
|
|
// Get pizza by ID
|
|
|
|
|
pizzas.MapGet("/{id}", (int id) => PizzaDB.GetPizza(id))
|
|
|
|
|
.WithName("GetPizzaById")
|
|
|
|
|
.WithSummary("Get pizza by ID")
|
|
|
|
|
.WithDescription("Gets a specific pizza by its unique identifier")
|
|
|
|
|
.WithOpenApi(operation => {
|
|
|
|
|
operation.Parameters[0].Description = "The unique identifier for the pizza";
|
|
|
|
|
return operation;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Create a new pizza
|
|
|
|
|
pizzas.MapPost("/", (Pizza pizza) => PizzaDB.CreatePizza(pizza))
|
|
|
|
|
.WithName("CreatePizza")
|
|
|
|
|
.WithSummary("Create a new pizza")
|
|
|
|
|
.WithDescription("Adds a new pizza to the menu");
|
|
|
|
|
|
|
|
|
|
// Update a pizza
|
|
|
|
|
pizzas.MapPut("/", (Pizza pizza) => PizzaDB.UpdatePizza(pizza))
|
|
|
|
|
.WithName("UpdatePizza")
|
|
|
|
|
.WithSummary("Update an existing pizza")
|
|
|
|
|
.WithDescription("Updates the details of an existing pizza");
|
|
|
|
|
|
|
|
|
|
// Delete a pizza
|
|
|
|
|
pizzas.MapDelete("/{id}", (int id) => PizzaDB.RemovePizza(id))
|
|
|
|
|
.WithName("DeletePizza")
|
|
|
|
|
.WithSummary("Delete a pizza")
|
|
|
|
|
.WithDescription("Removes a pizza from the menu")
|
|
|
|
|
.WithOpenApi(operation => {
|
|
|
|
|
operation.Parameters[0].Description = "The unique identifier for the pizza to delete";
|
|
|
|
|
return operation;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.Run();
|