using API.BusinessLogic; using API.Models.RecipeModels; using API.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Security.Claims; namespace API.Controllers { [ApiController] [Route("api/[controller]")] public class RecipeController : Controller { private readonly RecipeLogic _recipeLogic; private readonly OpenAiRecipes _openAiRecipes; public RecipeController(RecipeLogic recipeLogic, OpenAiRecipes openAiRecipes) { _recipeLogic = recipeLogic; _openAiRecipes = openAiRecipes; } /// /// Gets the userId from the jwt token amd returns a list of recipes without their ingredients and directions /// /// returns a okobjectresult with a list of recipes if it fails it returns a confliftobjectresult with a message of why it failed [Authorize] [HttpGet("getall")] public async Task ReadRecipes() { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); return await _recipeLogic.GetRecipes(userId); } /// /// Gets a specifik recipe including the ingredients and directions /// /// The recipe that is you want /// returns a okobjectresult with a recipe if it fails it returns a confliftobjectresult with a message of why it failed [Authorize] [HttpGet("get/{recipeId}")] public async Task ReadRecipe(int recipeId) { return await _recipeLogic.GetRecipe(recipeId); } /// /// Creates a recipe and adds it to the users recipes /// /// The recipe to be added /// returns a okobjectresult with a boolean that is true if it fails it returns a confliftobjectresult with a message of why it failed [Authorize] [HttpPost("create")] public async Task CreateRecipe([FromBody] RecipeDTO recipe) { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); return await _recipeLogic.CreateRecipe(recipe, userId); } /// /// Edits a recipe /// /// the edited recipe /// the recipe to be edited /// returns a okobjectresult with a boolean that is true if it fails it returns a confliftobjectresult with a message of why it failed [Authorize] [HttpPut("edit/{recipeId}")] public async Task EditRecipe([FromBody] RecipeDTO recipe, int recipeId) { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); return await _recipeLogic.EditRecipe(recipe, recipeId, userId); } /// /// Deletess a recipe /// /// the id of the recipe to be deleted /// returns a okobjectresult with a boolean that is true if it fails it returns a confliftobjectresult with a message of why it failed [Authorize] [HttpDelete("delete/{recipeId}")] public async Task DeleteRecipe(int recipeId) { return await _recipeLogic.DeleteRecipe(recipeId); } /// /// Generates a recipe using chatgpt /// /// Contains all the infomation that is needed to generate a recipe /// returns a list of generated recipes [Authorize] [HttpPost("chatbot")] public async Task GenerateRecipe([FromBody] GenerateRecipeDTO recipeDTO) { var recipes = await _openAiRecipes.ChatGPT(recipeDTO); if (recipes.Content[0].Text == null || recipes.Content[0].Text == "") { return new ConflictObjectResult(new { message = "Could not connect to chatGPT" }); } return new OkObjectResult(recipes.Content[0].Text); } } }