UI/logic updates, tests added, backend updated

This commit is contained in:
2026-03-18 06:46:45 +00:00
parent b80d2a7379
commit 251e3c5821
27 changed files with 2113 additions and 1142 deletions

View File

@@ -35,7 +35,6 @@ public class RecipeController : ControllerBase
return Ok(result);
}
[Authorize]
[HttpPost("save")]
public async Task<IActionResult> SaveRecipe([FromBody] RecipeResponseDto recipeDto)
{
@@ -49,7 +48,7 @@ public class RecipeController : ControllerBase
var recipe = new Recipe
{
Title = recipeDto.Title,
Icon = recipeDto.Icon,
ImageUrl = recipeDto.ImageUrl,
Ingredients = recipeDto.Ingredients,
Instructions = recipeDto.Instructions,
CreatedAt = DateTime.UtcNow,
@@ -62,7 +61,6 @@ 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)
{
@@ -79,15 +77,19 @@ public class RecipeController : ControllerBase
existingRecipe.Title = updatedRecipe.Title;
existingRecipe.Ingredients = updatedRecipe.Ingredients;
existingRecipe.Instructions = updatedRecipe.Instructions;
if (!string.IsNullOrEmpty(updatedRecipe.ImageUrl))
{
existingRecipe.ImageUrl = updatedRecipe.ImageUrl;
}
await _context.SaveChangesAsync();
return Ok(new { message = "Recipe updated successfully!" });
}
[Authorize]
[HttpGet("my-collection")]
public async Task<ActionResult<IEnumerable<Recipe>>> GetMyRecipes()
public async Task<ActionResult<IEnumerable<Recipe>>> GetMyRecipes()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

View File

@@ -3,7 +3,7 @@ namespace Seasoned.Backend.DTOs;
public class RecipeResponseDto
{
public string Title { get; set; } = string.Empty;
public string Icon { get; set; } = "mdi-silverware-fork-knife";
public string? ImageUrl { get; set; }
public List<string> Ingredients { get; set; } = new();
public List<string> Instructions { get; set; } = new();

View File

@@ -13,8 +13,8 @@ using Seasoned.Backend.Data;
namespace Seasoned.Backend.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20260311160009_AddRecipeFields")]
partial class AddRecipeFields
[Migration("20260318044626_ChangeIconToImageUrl")]
partial class ChangeIconToImageUrl
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@@ -234,8 +234,7 @@ namespace Seasoned.Backend.Migrations
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("Icon")
.IsRequired()
b.Property<string>("ImageUrl")
.HasColumnType("text");
b.PrimitiveCollection<List<string>>("Ingredients")

View File

@@ -8,7 +8,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Seasoned.Backend.Migrations
{
/// <inheritdoc />
public partial class AddRecipeFields : Migration
public partial class ChangeIconToImageUrl : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
@@ -168,7 +168,7 @@ namespace Seasoned.Backend.Migrations
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Title = table.Column<string>(type: "text", nullable: false),
Icon = table.Column<string>(type: "text", nullable: false),
ImageUrl = table.Column<string>(type: "text", nullable: true),
Ingredients = table.Column<List<string>>(type: "text[]", nullable: false),
Instructions = table.Column<List<string>>(type: "text[]", nullable: false),
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),

View File

@@ -231,8 +231,7 @@ namespace Seasoned.Backend.Migrations
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("Icon")
.IsRequired()
b.Property<string>("ImageUrl")
.HasColumnType("text");
b.PrimitiveCollection<List<string>>("Ingredients")

View File

@@ -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 Icon { get; set; } = "mdi-silverware-fork-knife";
public string? ImageUrl { get; set; }
public List<string> Ingredients { get; set; } = new();
public List<string> Instructions { get; set; } = new();
public DateTime CreatedAt { get; set; }

View File

@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Configuration;
namespace Seasoned.Backend.Services;
@@ -18,6 +19,11 @@ public class RecipeService : IRecipeService
public async Task<RecipeResponseDto> ParseRecipeImageAsync(IFormFile image)
{
if (image == null || image.Length == 0)
{
return new RecipeResponseDto { Title = "Error: No image provided" };
}
var googleAI = new GoogleAI(_apiKey);
var model = googleAI.GenerativeModel("gemini-3.1-flash-lite-preview");
@@ -27,21 +33,15 @@ 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.
Return ONLY a raw JSON string.
DO NOT include markdown formatting.
JSON structure:
{
""title"": ""string"",
""icon"": ""string"",
""ingredients"": [""string"", ""string""],
""instructions"": [""string"", ""string""]
""title"": ""string"",
""ingredients"": [""string"", ""string""],
""instructions"": [""string"", ""string""]
}";
var generationConfig = new GenerationConfig {
ResponseMimeType = "application/json",
Temperature = 0.1f
@@ -54,24 +54,21 @@ public class RecipeService : IRecipeService
var response = await model.GenerateContent(request);
string rawText = response.Text?.Trim() ?? "";
int start = rawText.IndexOf('{');
int end = rawText.LastIndexOf('}');
string cleanJson = CleanJsonResponse(rawText);
if (start == -1 || end == -1)
if (string.IsNullOrEmpty(cleanJson))
{
return new RecipeResponseDto { Title = "Error" };
return new RecipeResponseDto { Title = "Error: Invalid AI Response" };
}
string cleanJson = rawText.Substring(start, (end - start) + 1);
try
{
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var result = JsonSerializer.Deserialize<RecipeResponseDto>(cleanJson, options);
if (result != null && string.IsNullOrEmpty(result.Icon))
if (result != null)
{
result.Icon = "mdi-silverware-fork-knife";
result.ImageUrl = $"data:{image.ContentType};base64,{base64Image}";
}
return result ?? new RecipeResponseDto { Title = "Empty Response" };
@@ -79,8 +76,6 @@ public class RecipeService : IRecipeService
catch (JsonException ex)
{
Console.WriteLine($"Raw AI Output: {rawText}");
Console.WriteLine($"Failed to parse JSON: {ex.Message}");
return new RecipeResponseDto { Title = "Parsing Error" };
}
}
@@ -108,7 +103,6 @@ public class RecipeService : IRecipeService
""reply"": ""A friendly, thematic response from the Chef."",
""recipe"": {
""title"": ""string"",
""icon"": ""string (must be a valid mdi- icon name)"",
""ingredients"": [""string"", ""string""],
""instructions"": [""string"", ""string""]
}
@@ -125,11 +119,15 @@ public class RecipeService : IRecipeService
var request = new GenerateContentRequest(fullPrompt, generationConfig);
var response = await model.GenerateContent(request);
string rawText = response.Text ?? "";
string jsonToParse = CleanJsonResponse(rawText);
if (string.IsNullOrEmpty(jsonToParse))
return new ChefConsultResponseDto { Reply = "The Chef is at a loss for words. Try rephrasing?" };
try
{
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
string jsonToParse = response.Text ?? "{ \"reply\": \"The chef is speechless. Try again?\" }";
var result = JsonSerializer.Deserialize<ChefConsultResponseDto>(jsonToParse, options);
return result ?? new ChefConsultResponseDto { Reply = "Chef is a bit confused!" };
@@ -139,4 +137,12 @@ public class RecipeService : IRecipeService
return new ChefConsultResponseDto { Reply = "The kitchen is a mess right now. Try again?" };
}
}
internal string CleanJsonResponse(string rawText)
{
int start = rawText.IndexOf('{');
int end = rawText.LastIndexOf('}');
if (start == -1 || end == -1) return string.Empty;
return rawText.Substring(start, (end - start) + 1);
}
}