using API.BusinessLogic; using API.Models.ShoppingListModels; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Security.Claims; namespace API.Controllers { [ApiController] [Route("api/[controller]")] public class ShoppingListController : Controller { private readonly ShoppingListLogic _shoppingListLogic; public ShoppingListController(ShoppingListLogic shoppingListLogic) { _shoppingListLogic = shoppingListLogic; } /// /// Gets the entire shoppinglist connected to the user /// /// returns a list of shoppinglist items if it fails it returns a confliftobjectresult with a message of why it failed [Authorize] [HttpGet("get")] public async Task ReadShoppingList() { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); return await _shoppingListLogic.ReadShoppingList(userId); } /// /// Adds an item to the shopping list /// /// /// [Authorize] [HttpPost("add")] public async Task AddItem([FromBody] ShoppingListItemDTO listItemDTO) { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); return await _shoppingListLogic.AddItemToShoppingList(listItemDTO, userId); } /// /// Checks/Unchecks an item on the shoppinglist /// /// The item to be checked/unchecked /// 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("check")] public async Task CheckItem(int itemId) { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); return await _shoppingListLogic.CheckItemInShoppingList(userId, itemId); } /// /// Edits an item on the shoppinglist /// /// The edited item /// The item 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("update")] public async Task UpdateItem([FromBody] ShoppingListItemDTO listItemDTO, int itemId) { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); return await _shoppingListLogic.UpdateItemInShoppingList(userId, itemId, listItemDTO); } /// /// Deletes an item on the shoppinglist /// /// The item 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")] public async Task DeleteItem(int itemId) { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); return await _shoppingListLogic.DeleteItemInShoppingList(userId, itemId); } /// /// Add an entire recipes ingredients to the shoppinglist /// /// The recipes ingredients 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("recipeadd/{recipeId}")] public async Task AddARecipesItems( int recipeId, [FromBody] string multiplier = "1") { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); return await _shoppingListLogic.AddRecipeToShoppingList(userId, recipeId, multiplier); } } }