6 Commits

9 changed files with 120 additions and 9 deletions
+3
View File
@@ -55,3 +55,6 @@ nunit-*.xml
.vs/
pizza.db*
# JetBrains Rider
.idea/
+50
View 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; } = "";
}
}
}
@@ -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"
});
}
}
}
+5 -5
View File
@@ -12,16 +12,16 @@ EXPOSE 8081
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 ["PizzaExpress.csproj", "."]
RUN dotnet restore "PizzaExpress.csproj"
COPY . .
WORKDIR "/src/PizzaExpress"
RUN dotnet build "./PizzaExpress.csproj" -c $BUILD_CONFIGURATION -o /app/build
#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
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
+4 -1
View File
@@ -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>
+2
View File
@@ -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
+1 -1
View File
@@ -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
View File
@@ -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 -1
View File
@@ -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"
}
}