コード例 #1
0
        public async Task <IActionResult> GetAllStores(CancellationToken ct)
        {
            var storeEntities = await _storeRepository.GetAllEntities(ct);

            if (storeEntities.Count == 0)
            {
                return(NotFound());
            }

            var stores = new List <Store>();

            foreach (var entity in storeEntities)
            {
                var addressEntity = await _addressRepository.GetEntityAsync(entity.AddressId, ct);

                Store mappedStore = Mapper.Map <Store>(entity);

                Address address = Mapper.Map <Address>(addressEntity);

                mappedStore.Address = address;

                StoreMapEntity map = await _storeMapRepository.GetEntityAsync(entity.StoreMapId, ct);

                StoreMap newMap = Mapper.Map <StoreMap>(map);

                mappedStore.StoreMap = newMap;
                stores.Add(mappedStore);
            }

            return(Ok(stores));
        }
コード例 #2
0
        public async Task <IActionResult> GetStore(int id, CancellationToken ct)
        {
            StoreEntity storeEntity = await _storeRepository.GetEntityAsync(id, 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;

            return(Ok(mappedStore));
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        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]}" }));
        }