public async Task <IActionResult> PostItemToList([FromBody] ItemListLink link, CancellationToken ct) { if (!ModelState.IsValid) { return(BadRequest()); } var currentUser = HttpContext.User; var userclaim = currentUser.Claims.First(); var userId = Guid.Parse(userclaim.Value); var shoppingUser = await _shoppingUserRepository.GetEntityAsync(userId, ct); if (shoppingUser == null) { return(BadRequest("You are not a shopping user")); } ItemShoppingListLinkEntity itemlistlink = new ItemShoppingListLinkEntity { ItemId = link.ItemId, ShoppingListId = link.ListId, ItemQuantity = link.ItemQuantity }; // Look up item in the store and make sure it is there. var item = await _itemStoreLinkRepository.GetEntityAsync(link.ItemId, shoppingUser.HomeStoreId, ct); if (item == null) { return(BadRequest("Item is not in the store")); } var shoppingList = await _shoppingListRepository.GetEntityAsync(link.ListId, ct); if (shoppingList.StoreId != shoppingUser.HomeStoreId) { return(BadRequest("List was not created for your current HomeStore")); } var linkAdded = await _itemListLinkRepository.AddNewLink(itemlistlink, ct); //var linksToShoppingList = await _itemListLinkRepository.GetAllByShoppingListId(link.ListId, ct); ShoppingListItem newShoppingListItem = new ShoppingListItem { LinkId = linkAdded.Id, }; if (linkAdded != null) { return(Ok(newShoppingListItem)); } return(BadRequest()); }
public async Task <IActionResult> UpdateLink(int linkId, int newQuantity, CancellationToken ct) { if (!ModelState.IsValid) { return(BadRequest()); } var currentUser = HttpContext.User; var userclaim = currentUser.Claims.First(); var userId = Guid.Parse(userclaim.Value); var shoppingUser = await _shoppingUserRepository.GetEntityAsync(userId, ct); if (shoppingUser == null) { return(BadRequest("You are not a shopping user")); } var link = await _itemListLinkRepository.GetShoppingItem(linkId, ct); if (link == null) { return(BadRequest(new { error = $"Item does not exist in the current store" })); } link.ItemQuantity = newQuantity; var updatedLink = await _itemListLinkRepository.UpdateEntity(link, ct); ItemListLink newlink = new ItemListLink { ItemId = updatedLink.ItemId, ItemQuantity = updatedLink.ItemQuantity, ListId = updatedLink.ShoppingListId }; if (newQuantity == updatedLink.ItemQuantity) { return(Ok(newlink)); } else { return(BadRequest()); } }