Cristo bastardo

This commit is contained in:
2025-11-17 13:06:14 +01:00
commit e46c78fefd
35 changed files with 1716 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PizzaExpress.Data;
using PizzaExpress.Models;
namespace PizzaExpress.Controllers
{
[ApiController]
[Route("api/pizze")]
public class PizzeController : ControllerBase
{
private readonly PizzaContext _ctx;
public PizzeController(PizzaContext ctx)
{
_ctx = ctx;
}
// GET: /api/pizze
[HttpGet]
[Produces("application/json", "text/xml")]
public async Task<ActionResult<object>> GetPizze()
{
var list = await _ctx.Pizze.AsNoTracking().ToListAsync();
// wrapper come nell'esempio Java { "pizze": [...] }
return Ok(new { pizze = list });
}
// GET: /api/pizze/{id} (JSON o XML in base all'header Accept)
[HttpGet("{id:int}")]
[Produces("application/json", "text/xml")]
public async Task<IActionResult> GetPizzaById(int id)
{
var pizza = await _ctx.Pizze.AsNoTracking().FirstOrDefaultAsync(p => p.Id == id);
if (pizza is null) return NotFound();
return Ok(pizza);
}
// DELETE: /api/pizze/{id}
[HttpDelete("{id:int}")]
public async Task<IActionResult> DeletePizza(int id)
{
var pizza = await _ctx.Pizze.FindAsync(id);
if (pizza is null) return NotFound();
_ctx.Pizze.Remove(pizza);
await _ctx.SaveChangesAsync();
return NoContent();
}
// PUT: /api/pizze/{id} Body: { "prezzo": 4.70 }
[HttpPut("{id:int}")]
public async Task<IActionResult> UpdatePrezzo(int id, [FromBody] PrezzoUpdate body)
{
var pizza = await _ctx.Pizze.FindAsync(id);
if (pizza is null) return NotFound();
pizza.Prezzo = body.Prezzo;
await _ctx.SaveChangesAsync();
return NoContent();
}
// POST: /api/pizze Body: { "id":4, "nome":"della casa", "prezzo": 8.50 }
[HttpPost]
public async Task<IActionResult> AddPizza([FromBody] Pizza p)
{
var exists = await _ctx.Pizze.AnyAsync(x => x.Id == p.Id);
if (exists) return BadRequest("ID già esistente.");
_ctx.Pizze.Add(p);
await _ctx.SaveChangesAsync();
return CreatedAtAction(nameof(GetPizzaById), new { id = p.Id }, p);
}
public class PrezzoUpdate
{
public decimal Prezzo { get; set; }
}
}
}

View File

@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Mvc;
namespace PizzaExpress.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}