public async Task <IActionResult> PostShoppingList([FromBody] APIShoppingList shoppingList, CancellationToken ct) { if (!ModelState.IsValid) { return(BadRequest(new { error = "Model state is not valid" })); } 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) { ShoppingListEntity list = new ShoppingListEntity { Name = shoppingList.Name, ShoppingUserId = shoppingUser.Id, StoreId = shoppingUser.HomeStoreId, TimeOfCreation = DateTimeOffset.UtcNow }; var listAdded = await _shoppingListRepository.AddShoppingList(list, ct); if (listAdded.Item1) { shoppingUser.ShoppingLists.Add(list); var user = await _shoppingUserRepository.UpdateEntity(shoppingUser, ct); return(Ok(new ShoppingList { Id = list.Id, Items = new List <ShoppingListItem>(), Name = list.Name, TimeOfCreation = list.TimeOfCreation, TotalCost = 0, TotalItems = 0 })); } else { return(BadRequest(new { error = listAdded.Item2 })); } } return(BadRequest(new { error = "Could not find shopping user" })); }
public async Task <IActionResult> ChangeHomeStore(int id, CancellationToken ct) { 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 store = await _storeRepository.GetEntityAsync(id, ct); shoppingUser.HomeStoreId = store.Id; var updatedUser = await _shoppingUserRepository.UpdateEntity(shoppingUser, ct); if (updatedUser.HomeStoreId != store.Id) { return(BadRequest("Error updating users store")); } AddressEntity addressEntity = await _addressRepository.GetEntityAsync(store.AddressId, ct); Store mappedStore = Mapper.Map <Store>(store); Address address = Mapper.Map <Address>(addressEntity); mappedStore.Address = address; StoreMapEntity map = await _storeMapRepository.GetEntityAsync(store.StoreMapId, ct); StoreMap newMap = Mapper.Map <StoreMap>(map); mappedStore.StoreMap = newMap; var lists = await _shoppingListRepository.GetAllShoppingListsForUserForACertainStore(shoppingUser.Id, mappedStore.Id, ct); List <ShoppingList> newlist = new List <ShoppingList>(); //sle = ShoppingListEntity foreach (var sle in lists) { ShoppingList sl = new ShoppingList { Name = sle.Name, //Store = mappedStore, TimeOfCreation = sle.TimeOfCreation, Id = sle.Id }; var itemListLinkEntities = await _itemListLinkRepository.GetAllByShoppingListId(sle.Id, ct); foreach (var itemlistlink in itemListLinkEntities) { ShoppingListItem item = await GetFullItemInfo(itemlistlink, sle.StoreId, ct); sl.Items.Add(item); } sl.TotalCost = sl.Items.Select(i => i.Price * i.ItemQuantity).ToList().Sum(); sl.TotalItems = sl.Items.Select(i => i.ItemQuantity).ToList().Sum(); newlist.Add(sl); } var updatedUserApp = new ShoppingUser { FullName = $"{shoppingUser.FirstName} {shoppingUser.LastName}", Email = shoppingUser.Email, HomeStore = mappedStore, ShoppingLists = newlist, Role = "Shopping" }; return(Ok(updatedUserApp)); }