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> 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)); }
public async Task <IActionResult> LoginUser([FromBody] LoginUser user, CancellationToken ct) { if (!ModelState.IsValid) { return(BadRequest(new { error = "Model state is not valid" })); } var result = await _baseSignInManager.PasswordSignInAsync(user.Email, user.Password, false, false); if (!result.Succeeded) { return(BadRequest(new { error = $"Unable to sign in user {user.Email}" })); } var userEntity = await _baseUserManager.FindByEmailAsync(user.Email); var rolenames = await _baseUserManager.GetRolesAsync(userEntity) as List <string>; if (rolenames[0] == "Shopping") { try { var shoppingUserEntity = userEntity as ShoppingUserEntity; StoreEntity storeEntity = await _storeRepository.GetEntityAsync(shoppingUserEntity.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; StoreMapEntity map = await _storeMapRepository.GetEntityAsync(storeEntity.StoreMapId, ct); StoreMap newMap = Mapper.Map <StoreMap>(map); mappedStore.StoreMap = newMap; var lists = await _shoppingListRepository.GetAllShoppingListsForUserForACertainStore(shoppingUserEntity.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 shoppingUser = new ShoppingUser { Token = CreateToken(shoppingUserEntity), FullName = $"{shoppingUserEntity.FirstName} {shoppingUserEntity.LastName}", Email = shoppingUserEntity.Email, HomeStore = mappedStore, ShoppingLists = newlist, Role = "Shopping" }; var response = new ObjectResult(shoppingUser) { StatusCode = (int)HttpStatusCode.OK }; Request.HttpContext.Response.Headers.Add("authorization", shoppingUser.Token); return(response); } catch (Exception ex) { Console.WriteLine(ex); } } else if (rolenames[0] == "Store") { var storeUserEntity = userEntity as StoreUserEntity; var storeUser = new StoreUser { Token = CreateToken(storeUserEntity), FullName = $"{storeUserEntity.FirstName}, {storeUserEntity.LastName}", Email = storeUserEntity.Email, Role = "Store" }; if (storeUserEntity.HomeStoreId != 0) { StoreEntity storeEntity = await _storeRepository.GetEntityAsync(storeUserEntity.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; storeUser.Store = mappedStore; } else { storeUser.Store = null; } var response = new ObjectResult(storeUser) { StatusCode = (int)HttpStatusCode.OK }; Request.HttpContext.Response.Headers.Add("authorization", storeUser.Token); return(response); } else if (rolenames[0] == "Admin") { } return(BadRequest(new { error = $"Could not find user with role {rolenames[0]}" })); }