UI + backend update
This commit is contained in:
@@ -5,7 +5,6 @@ using Seasoned.Backend.Data;
|
||||
using System.Security.Claims;
|
||||
using Seasoned.Backend.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Seasoned.Backend.Controllers;
|
||||
@@ -49,7 +48,7 @@ public class RecipeController : ControllerBase
|
||||
var recipe = new Recipe
|
||||
{
|
||||
Title = recipeDto.Title,
|
||||
Description = recipeDto.Description,
|
||||
Icon = recipeDto.Icon,
|
||||
Ingredients = recipeDto.Ingredients,
|
||||
Instructions = recipeDto.Instructions,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
@@ -62,6 +61,29 @@ public class RecipeController : ControllerBase
|
||||
return Ok(new { message = "Recipe saved to your collection!" });
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpPut("update/{id}")]
|
||||
public async Task<IActionResult> UpdateRecipe(int id, [FromBody] Recipe updatedRecipe)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
|
||||
var existingRecipe = await _context.Recipes
|
||||
.FirstOrDefaultAsync(r => r.Id == id && r.UserId == userId);
|
||||
|
||||
if (existingRecipe == null)
|
||||
{
|
||||
return NotFound("Recipe not found or you do not have permission to edit it.");
|
||||
}
|
||||
|
||||
existingRecipe.Title = updatedRecipe.Title;
|
||||
existingRecipe.Ingredients = updatedRecipe.Ingredients;
|
||||
existingRecipe.Instructions = updatedRecipe.Instructions;
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return Ok(new { message = "Recipe updated successfully!" });
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("my-collection")]
|
||||
public async Task<ActionResult<IEnumerable<Recipe>>> GetMyRecipes()
|
||||
@@ -75,5 +97,4 @@ public class RecipeController : ControllerBase
|
||||
|
||||
return Ok(myRecipes);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,7 @@ namespace Seasoned.Backend.DTOs;
|
||||
public class RecipeResponseDto
|
||||
{
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public string Icon { get; set; } = "mdi-silverware-fork-knife";
|
||||
public List<string> Ingredients { get; set; } = new();
|
||||
public List<string> Instructions { get; set; } = new();
|
||||
}
|
||||
@@ -3,7 +3,7 @@ namespace Seasoned.Backend.Models;
|
||||
public class Recipe {
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public string Icon { get; set; } = "mdi-silverware-fork-knife";
|
||||
public List<string> Ingredients { get; set; } = new();
|
||||
public List<string> Instructions { get; set; } = new();
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Seasoned.Backend.Services;
|
||||
|
||||
public class RecipeService : IRecipeService
|
||||
@@ -18,6 +19,7 @@ public class RecipeService : IRecipeService
|
||||
public async Task<RecipeResponseDto> ParseRecipeImageAsync(IFormFile image)
|
||||
{
|
||||
var googleAI = new GoogleAI(_apiKey);
|
||||
|
||||
var model = googleAI.GenerativeModel("gemini-3.1-flash-lite-preview");
|
||||
|
||||
using var ms = new MemoryStream();
|
||||
@@ -25,16 +27,19 @@ public class RecipeService : IRecipeService
|
||||
var base64Image = Convert.ToBase64String(ms.ToArray());
|
||||
|
||||
var prompt = @"Extract the recipe details from this image.
|
||||
|
||||
RULES FOR THE 'icon' FIELD:
|
||||
1. Select a valid 'Material Design Icon' name (e.g., 'mdi-pasta', 'mdi-bread-slice', 'mdi-muffin', 'mdi-pizza').
|
||||
2. If the recipe type is ambiguous or you cannot find a specific matching icon, you MUST return 'mdi-silverware-fork-knife'.
|
||||
|
||||
IMPORTANT: Return ONLY a raw JSON string.
|
||||
DO NOT include markdown formatting (no ```json).
|
||||
DO NOT include any text before or after the JSON.
|
||||
All property names and string values MUST be enclosed in double quotes.
|
||||
DO NOT include markdown formatting.
|
||||
JSON structure:
|
||||
{
|
||||
""title"": ""string"",
|
||||
""description"": ""string"",
|
||||
""ingredients"": [""string"", ""string""],
|
||||
""instructions"": [""string"", ""string""]
|
||||
""title"": ""string"",
|
||||
""icon"": ""string"",
|
||||
""ingredients"": [""string"", ""string""],
|
||||
""instructions"": [""string"", ""string""]
|
||||
}";
|
||||
|
||||
var config = new GenerationConfig {
|
||||
@@ -43,7 +48,8 @@ public class RecipeService : IRecipeService
|
||||
};
|
||||
|
||||
var request = new GenerateContentRequest(prompt, config);
|
||||
await Task.Run(() => request.AddMedia(base64Image, "image/png"));
|
||||
|
||||
await Task.Run(() => request.AddMedia(base64Image, image.ContentType ?? "image/png"));
|
||||
|
||||
var response = await model.GenerateContent(request);
|
||||
string rawText = response.Text?.Trim() ?? "";
|
||||
@@ -53,7 +59,7 @@ public class RecipeService : IRecipeService
|
||||
|
||||
if (start == -1 || end == -1)
|
||||
{
|
||||
return new RecipeResponseDto { Title = "Error", Description = "AI failed to generate a valid JSON block." };
|
||||
return new RecipeResponseDto { Title = "Error" };
|
||||
}
|
||||
|
||||
string cleanJson = rawText.Substring(start, (end - start) + 1);
|
||||
@@ -62,6 +68,12 @@ public class RecipeService : IRecipeService
|
||||
{
|
||||
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
var result = JsonSerializer.Deserialize<RecipeResponseDto>(cleanJson, options);
|
||||
|
||||
if (result != null && string.IsNullOrEmpty(result.Icon))
|
||||
{
|
||||
result.Icon = "mdi-silverware-fork-knife";
|
||||
}
|
||||
|
||||
return result ?? new RecipeResponseDto { Title = "Empty Response" };
|
||||
}
|
||||
catch (JsonException ex)
|
||||
@@ -69,10 +81,7 @@ public class RecipeService : IRecipeService
|
||||
Console.WriteLine($"Raw AI Output: {rawText}");
|
||||
Console.WriteLine($"Failed to parse JSON: {ex.Message}");
|
||||
|
||||
return new RecipeResponseDto {
|
||||
Title = "Parsing Error",
|
||||
Description = "The AI response was malformed. Check logs."
|
||||
};
|
||||
return new RecipeResponseDto { Title = "Parsing Error" };
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user