using Microsoft.AspNetCore.Mvc; using Seasoned.Backend.Services; using Seasoned.Backend.DTOs; 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; [ApiController] [Route("api/recipe")] public class RecipeController : ControllerBase { private readonly IRecipeService _recipeService; private readonly ApplicationDbContext _context; public RecipeController(IRecipeService recipeService, ApplicationDbContext context) { _recipeService = recipeService; _context = context; } [HttpPost("upload")] public async Task> UploadRecipe([FromForm] IFormFile image) { if (image == null || image.Length == 0) { return BadRequest("No image uploaded."); } var result = await _recipeService.ParseRecipeImageAsync(image); return Ok(result); } [Authorize] [HttpPost("save")] public async Task SaveRecipe([FromBody] RecipeResponseDto recipeDto) { if (recipeDto == null) { return BadRequest("Invalid recipe data."); } var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var recipe = new Recipe { Title = recipeDto.Title, Description = recipeDto.Description, Ingredients = recipeDto.Ingredients, Instructions = recipeDto.Instructions, CreatedAt = DateTime.UtcNow, UserId = userId }; _context.Recipes.Add(recipe); await _context.SaveChangesAsync(); return Ok(new { message = "Recipe saved to your collection!" }); } [Authorize] [HttpGet("my-collection")] public async Task>> GetMyRecipes() { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var myRecipes = await _context.Recipes .Where(r => r.UserId == userId) .OrderByDescending(r => r.CreatedAt) .ToListAsync(); return Ok(myRecipes); } }