Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b28c9cbb85 | |||
| cb5bfa94f9 | |||
| 2912745a21 | |||
| 69196518cf | |||
| 5e0d1cce20 | |||
| 069b503339 | |||
| 829cfeefcf | |||
| 1f95c0d0b6 | |||
| 94d1796213 | |||
| 9ebc82fe62 |
50
PizzaExpress/API/SpoonacularApi.cs
Normal file
50
PizzaExpress/API/SpoonacularApi.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
namespace PizzaExpress.API
|
||||
{
|
||||
public class SpoonacularApi
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly string _apiKey;
|
||||
|
||||
public SpoonacularApi(HttpClient httpClient, IConfiguration configuration)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
|
||||
_apiKey = configuration["Spoonacular:ApiKey"];
|
||||
}
|
||||
|
||||
public async Task<List<string>> GetIngredientiDaPizzaAsync(string nomePizza)
|
||||
{
|
||||
var searchUrl = $"https://api.spoonacular.com/recipes/complexSearch?query={Uri.EscapeDataString(nomePizza)}&language=it&number=1&apiKey={_apiKey}";
|
||||
// Chiamata GET Ricerca
|
||||
var searchResponse = await _httpClient.GetFromJsonAsync<SearchResponse>(searchUrl);
|
||||
if (searchResponse == null || searchResponse.Results.Count == 0)
|
||||
return new List<string>();
|
||||
// Prendiamo l'ID della prima ricetta trovata
|
||||
int recipeId = searchResponse.Results[0].Id;
|
||||
|
||||
// Query per ottenere gli ingredienti della ricetta
|
||||
var ingredientiUrl = $"https://api.spoonacular.com/recipes/{recipeId}/ingredientWidget.json?language=it&apiKey={_apiKey}";
|
||||
// Chiamata GET Ingredienti
|
||||
var ingredientiResponse = await _httpClient.GetFromJsonAsync<IngredientiResponse>(ingredientiUrl);
|
||||
|
||||
return ingredientiResponse.Ingredients.Select(i => i.Name).ToList();
|
||||
}
|
||||
|
||||
private class SearchResponse
|
||||
{
|
||||
public List<SearchResult> Results { get; set; } = new();
|
||||
}
|
||||
private class SearchResult
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
private class IngredientiResponse
|
||||
{
|
||||
public List<Ingrediente> Ingredients { get; set; } = new();
|
||||
}
|
||||
private class Ingrediente
|
||||
{
|
||||
public string Name { get; set; } = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
33
PizzaExpress/Controllers/IngredientiController.cs
Normal file
33
PizzaExpress/Controllers/IngredientiController.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using PizzaExpress.API;
|
||||
|
||||
namespace PizzaExpress.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/ingredienti")]
|
||||
public class IngredientiController : ControllerBase
|
||||
{
|
||||
private readonly SpoonacularApi _spoonacularApi;
|
||||
|
||||
public IngredientiController(SpoonacularApi spoonacularApi)
|
||||
{
|
||||
this._spoonacularApi = spoonacularApi;
|
||||
}
|
||||
|
||||
[HttpGet("da-pizza")]
|
||||
public async Task<IActionResult> GetIngredientiDaPizza([FromQuery] string nome)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(nome))
|
||||
return BadRequest("Nome pizza mancante");
|
||||
|
||||
var ingredienti = await _spoonacularApi.GetIngredientiDaPizzaAsync(nome);
|
||||
|
||||
return Ok(new
|
||||
{
|
||||
pizza = nome,
|
||||
ingredienti = ingredienti,
|
||||
fonte = "Spoonacular"
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,8 @@ namespace PizzaExpress.Controllers
|
||||
{
|
||||
var list = await _ctx.Pizze.AsNoTracking().ToListAsync();
|
||||
// wrapper come nell'esempio Java { "pizze": [...] }
|
||||
return Ok(new { pizze = list });
|
||||
//return Ok(new { pizze = list });
|
||||
return Ok(list);
|
||||
}
|
||||
|
||||
// GET: /api/pizze/{id} (JSON o XML in base all'header Accept)
|
||||
@@ -46,13 +47,18 @@ namespace PizzaExpress.Controllers
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
// PUT: /api/pizze/{id} Body: { "prezzo": 4.70 }
|
||||
// PUT: /api/pizze/{id}
|
||||
[HttpPut("{id:int}")]
|
||||
public async Task<IActionResult> UpdatePrezzo(int id, [FromBody] PrezzoUpdate body)
|
||||
public async Task<IActionResult> UpdatePizza(int id, [FromBody] Pizza body)
|
||||
{
|
||||
var pizza = await _ctx.Pizze.FindAsync(id);
|
||||
if (pizza is null) return NotFound();
|
||||
pizza.Nome = body.Nome;
|
||||
pizza.Prezzo = body.Prezzo;
|
||||
pizza.Categoria = body.Categoria;
|
||||
pizza.Note = body.Note;
|
||||
pizza.Tavolo = body.Tavolo;
|
||||
pizza.Stato = body.Stato;
|
||||
await _ctx.SaveChangesAsync();
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
public int Id { get; set; }
|
||||
public string Nome { get; set; } = string.Empty;
|
||||
public decimal Prezzo { get; set; }
|
||||
public string Categoria { get; set; } = string.Empty;
|
||||
public string Note { get; set; } = string.Empty;
|
||||
public int Tavolo { get; set; }
|
||||
public string Stato { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<ActiveDebugProfile>https</ActiveDebugProfile>
|
||||
<ActiveDebugProfile>http</ActiveDebugProfile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PizzaExpress.Data;
|
||||
using PizzaExpress.API;
|
||||
|
||||
namespace PizzaExpress
|
||||
{
|
||||
@@ -33,6 +34,7 @@ namespace PizzaExpress
|
||||
// Creazione del contesto con DB SQLite
|
||||
builder.Services.AddDbContext<PizzaContext>(opt =>
|
||||
opt.UseSqlite("Data Source=pizza.db"));
|
||||
builder.Services.AddHttpClient<SpoonacularApi>();
|
||||
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "http://localhost:5011"
|
||||
"applicationUrl": "http://localhost:5000"
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
@@ -18,7 +18,7 @@
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "https://localhost:7297;http://localhost:5011"
|
||||
"applicationUrl": "https://localhost:5001"
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
|
||||
@@ -10,5 +10,15 @@
|
||||
"http://127.0.0.1:5500",
|
||||
"http://localhost:5500"
|
||||
]
|
||||
},
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
"Http": {
|
||||
"Url": "http://localhost:5000"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Spoonacular": {
|
||||
"ApiKey": "YOUR_SPOONACULAR_API_KEY"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,5 +11,15 @@
|
||||
"http://localhost:5500"
|
||||
]
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
"Http": {
|
||||
"Url": "http://0.0.0.0:5000"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Spoonacular": {
|
||||
"ApiKey": "YOUR_SPOONACULAR_API_KEY"
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user