Controller and service update for saving recipes

This commit is contained in:
2026-03-05 20:39:33 +00:00
parent b127c4c8e5
commit 0da6c0e8cb
2 changed files with 28 additions and 10 deletions

View File

@@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Seasoned.Backend.Services; using Seasoned.Backend.Services;
using Seasoned.Backend.DTOs; using Seasoned.Backend.DTOs;
using Seasoned.Backend.Data;
using Seasoned.Backend.Models;
namespace Seasoned.Backend.Controllers; namespace Seasoned.Backend.Controllers;
@@ -9,10 +11,12 @@ namespace Seasoned.Backend.Controllers;
public class RecipeController : ControllerBase public class RecipeController : ControllerBase
{ {
private readonly IRecipeService _recipeService; private readonly IRecipeService _recipeService;
private readonly ApplicationDbContext _context;
public RecipeController(IRecipeService recipeService) public RecipeController(IRecipeService recipeService, ApplicationDbContext context)
{ {
_recipeService = recipeService; _recipeService = recipeService;
_context = context;
} }
[HttpPost("upload")] [HttpPost("upload")]
@@ -27,14 +31,28 @@ public class RecipeController : ControllerBase
return Ok(result); return Ok(result);
} }
[HttpGet] [HttpPost("save")]
public async Task<IActionResult> GetRecipes() public async Task<IActionResult> SaveRecipe([FromBody] RecipeResponseDto recipeDto)
{ {
// This assumes your DbContext is injected as _context if (recipeDto == null)
var recipes = await _context.Recipes {
.OrderByDescending(r => r.CreatedAt) return BadRequest("Invalid recipe data.");
.ToListAsync(); }
return Ok(recipes); var recipe = new Recipe
{
Title = recipeDto.Title,
Description = recipeDto.Description,
Ingredients = recipeDto.Ingredients,
Instructions = recipeDto.Instructions,
CreatedAt = DateTime.UtcNow
};
_context.Recipes.Add(recipe);
await _context.SaveChangesAsync();
return Ok(new { message = "Recipe saved!" });
} }
} }

View File

@@ -18,7 +18,7 @@ public class RecipeService : IRecipeService
public async Task<RecipeResponseDto> ParseRecipeImageAsync(IFormFile image) public async Task<RecipeResponseDto> ParseRecipeImageAsync(IFormFile image)
{ {
var googleAI = new GoogleAI(_apiKey); var googleAI = new GoogleAI(_apiKey);
var model = googleAI.GenerativeModel("gemini-2.5-flash"); var model = googleAI.GenerativeModel("gemini-3.1-flash-lite-preview");
using var ms = new MemoryStream(); using var ms = new MemoryStream();
await image.CopyToAsync(ms); await image.CopyToAsync(ms);