Cristo bastardo
This commit is contained in:
@@ -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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// No, qui non troverai IIDX Sparkle Shower
|
||||
// wait for chinese leakers
|
||||
using PizzaExpress.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PizzaExpress.Data
|
||||
{
|
||||
public class PizzaContext : DbContext
|
||||
{
|
||||
public PizzaContext(DbContextOptions<PizzaContext> options)
|
||||
: base(options) { }
|
||||
|
||||
public DbSet<Pizza> Pizze => Set<Pizza>();
|
||||
}
|
||||
|
||||
public static class SeedData
|
||||
{
|
||||
public static void Initialize(PizzaContext ctx)
|
||||
{
|
||||
if (ctx.Pizze.Any()) return;
|
||||
|
||||
ctx.Pizze.AddRange(
|
||||
new Pizza { Id = 1, Nome = "Margherita", Prezzo = 4.50m },
|
||||
new Pizza { Id = 2, Nome = "Prosciutto", Prezzo = 5.00m },
|
||||
new Pizza { Id = 3, Nome = "Capricciosa", Prezzo = 7.00m }
|
||||
);
|
||||
|
||||
ctx.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
# Vedere https://aka.ms/customizecontainer per informazioni su come personalizzare il contenitore di debug e su come Visual Studio usa questo Dockerfile per compilare le immagini per un debug più rapido.
|
||||
|
||||
# Questa fase viene usata durante l'esecuzione da Visual Studio in modalità rapida (impostazione predefinita per la configurazione di debug)
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||
USER app
|
||||
WORKDIR /app
|
||||
EXPOSE 8080
|
||||
EXPOSE 8081
|
||||
|
||||
|
||||
# Questa fase viene usata per compilare il progetto di servizio
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["PizzaExpress/PizzaExpress.csproj", "PizzaExpress/"]
|
||||
RUN dotnet restore "./PizzaExpress/PizzaExpress.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/PizzaExpress"
|
||||
RUN dotnet build "./PizzaExpress.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
# Questa fase viene usata per pubblicare il progetto di servizio da copiare nella fase finale
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./PizzaExpress.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
# Questa fase viene usata nell'ambiente di produzione o durante l'esecuzione da Visual Studio in modalità normale (impostazione predefinita quando non si usa la configurazione di debug)
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "PizzaExpress.dll"]
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace PizzaExpress.Models
|
||||
{
|
||||
public class Pizza
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Nome { get; set; } = string.Empty;
|
||||
public decimal Prezzo { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>d99fa37d-ba8c-4bba-9c32-705cde4708c6</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.10" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<ActiveDebugProfile>http</ActiveDebugProfile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,6 @@
|
||||
@PizzaExpress_HostAddress = http://localhost:5011
|
||||
|
||||
GET {{PizzaExpress_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
@@ -0,0 +1,62 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PizzaExpress.Data;
|
||||
|
||||
namespace PizzaExpress
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy(name: MyAllowSpecificOrigins,
|
||||
policy =>
|
||||
{
|
||||
policy.WithOrigins("http://127.0.0.1:5500",
|
||||
"http://localhost:5500")
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader()
|
||||
.AllowCredentials();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers()
|
||||
.AddXmlSerializerFormatters();
|
||||
|
||||
// DB in memory perch� siamo froci
|
||||
builder.Services.AddDbContext<PizzaContext>(opt =>
|
||||
opt.UseInMemoryDatabase("dbpizze"));
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
using (var porcoidddioooo = app.Services.CreateScope())
|
||||
{
|
||||
var ctx = porcoidddioooo.ServiceProvider.GetRequiredService<PizzaContext>();
|
||||
SeedData.Initialize(ctx);
|
||||
}
|
||||
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseCors(MyAllowSpecificOrigins);
|
||||
app.UseAuthorization();
|
||||
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "http://localhost:5011"
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "https://localhost:7297;http://localhost:5011"
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Container (Dockerfile)": {
|
||||
"commandName": "Docker",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_HTTPS_PORTS": "8081",
|
||||
"ASPNETCORE_HTTP_PORTS": "8080"
|
||||
},
|
||||
"publishAllPorts": true,
|
||||
"useSSL": true
|
||||
}
|
||||
},
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:52949",
|
||||
"sslPort": 44362
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace PizzaExpress
|
||||
{
|
||||
public class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user