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> GetHomeStoreShoppingLists(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(new { error = "Not allowed to access shopping lists" })); } var lists = await _shoppingListRepository.GetAllShoppingListsForUserForACertainStore(shoppingUser.Id, shoppingUser.HomeStoreId, ct); List <ShoppingList> usersShoppingLists = new List <ShoppingList>(); foreach (var list in lists) { ShoppingList sl = new ShoppingList(); sl.Id = list.Id; sl.Name = list.Name; sl.TimeOfCreation = list.TimeOfCreation; var itemListLinks = await _itemListLinkRepository.GetAllByShoppingListId(list.Id, ct); //TODO: Look up item in shoppinglist item link repository foreach (var itemlistlink in itemListLinks) { ShoppingListItem item = await GetFullItemInfo(itemlistlink, list.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(); usersShoppingLists.Add(sl); } return(Ok(usersShoppingLists)); }
public async Task <IActionResult> GetShoppingUser(string email, CancellationToken ct) { var user = await _shoppingUserRepository.GetEntityAsync(email, ct); if (user == null) { return(NotFound(new { error = "User Not Found with that email, Create a new user or Try Again!" })); } // Get Home Store Information StoreEntity storeEntity = await _storeRepository.GetEntityAsync(user.HomeStoreId, ct); AddressEntity addressEntity = await _addressRepository.GetEntityAsync(storeEntity.AddressId, ct); Store mappedStore = Mapper.Map <Store>(storeEntity); Address address = Mapper.Map <Address>(addressEntity); mappedStore.Address = address; // Get Shopping Lists //var usersShoppingLists = await _shoppingListRepository.GetAllShoppingListsForUserForACertainStore(user.Id, user.HomeStoreId, ct); List <ShoppingList> list = new List <ShoppingList>(); // sle = ShoppingListEntity foreach (var sle in user.ShoppingLists) { 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 sle.ListItemLinks) { ShoppingListItem item = await GetFullItemInfo(itemlistlink, sle.StoreId, ct); sl.Items.Add(item); } list.Add(sl); } ShoppingUser theShoppingUser = Mapper.Map <ShoppingUser>(user); theShoppingUser.HomeStore = mappedStore; theShoppingUser.ShoppingLists = list; // Grab all of the items for each of the shopping lists return(Ok(theShoppingUser)); }
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)); }