public async Task <IActionResult> PostItemToStore([FromBody] APIItem item, CancellationToken ct)
        {
            //TODO: Check to make sure the store is trying to add the item
            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 storeUser   = await _storeUserRepository.GetEntityAsync(userId, ct);

            if (storeUser == null)
            {
                return(BadRequest(new { error = "Not a store, can't add items unless you are a store." }));
            }

            if (storeUser.HomeStoreId == 0)
            {
                return(BadRequest(new { error = "You are a store, but haven't created your homestore yet." }));
            }

            ItemEntity itemEntity = new ItemEntity
            {
                Name = item.Name,
                SpoonacularProductId = item.SpoonId
            };

            ItemStoreLinkEntity linkEntity = new ItemStoreLinkEntity
            {
                InStock     = item.InStock,
                StockAmount = item.StockAmount,
                StoreId     = storeUser.HomeStoreId,
                Price       = item.Price
            };

            bool itemAdded = await _itemRepository.AddEntityAsync(itemEntity, ct);

            if (itemAdded)
            {
                linkEntity.ItemId = itemEntity.Id;
                itemAdded         = await _itemStoreLinkRepository.AddEntityAsync(linkEntity, ct);

                if (itemAdded)
                {
                    return(Ok());
                }

                return(BadRequest());
            }

            return(BadRequest());
        }
        public async Task <ItemStoreLinkEntity> UpdateEntity(ItemStoreLinkEntity entity, CancellationToken ct)
        {
            try
            {
                _dbContext.ItemStoreLinks.Update(entity);
                await _dbContext.SaveChangesAsync();

                return(entity);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
        public async Task <bool> AddEntityAsync(ItemStoreLinkEntity entity, CancellationToken ct)
        {
            try
            {
                await _dbContext.ItemStoreLinks.AddAsync(entity);

                await _dbContext.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
        private async Task <Item> GetFullItemInfo(ItemStoreLinkEntity itemStoreLink, ItemEntity itemEntity, CancellationToken ct)
        {
            SpoonProductInformation spoonItem = GetSpoonItem(itemEntity.SpoonacularProductId);

            var department = await _departmentRepository.GetEntityAsync(itemStoreLink.DepartmentId, ct);

            var lowerDepartment = await _lowerDepartmentRepository.GetEntityAsync(itemStoreLink.LowerDepartmentId, ct);

            var aisle = await _aisleRepository.GetEntityAsync(itemStoreLink.AisleId, ct);

            var section = await _sectionRepository.GetEntityAsync(itemStoreLink.SectionId, ct);

            var shelf = await _shelfRepository.GetEntityAsync(itemStoreLink.ShelfId, ct);

            var slot = await _shelfSlotsRepository.GetEntityAsync(itemStoreLink.SlotId, ct);

            Item item = new Item
            {
                Id                 = itemEntity.Id,
                LinkId             = itemStoreLink.Id,
                Image              = "image.jpg",
                Name               = itemEntity.Name,
                Price              = itemStoreLink.Price,
                InStock            = itemStoreLink.InStock,
                StockAmount        = itemStoreLink.StockAmount,
                DepartmentId       = itemStoreLink.DepartmentId,
                Department         = department.Name,
                LowerDepartmentId  = itemStoreLink.LowerDepartmentId,
                LowerDepartment    = lowerDepartment.Name,
                AisleId            = itemStoreLink.AisleId,
                Aisle              = aisle.Name,
                SectionId          = itemStoreLink.SectionId,
                Section            = section.Name,
                ShelfId            = itemStoreLink.ShelfId,
                Shelf              = shelf.ShelfNumber.ToString(),
                SlotId             = itemStoreLink.SlotId,
                Slot               = slot.SlotOnShelf.ToString(),
                ProductInformation = spoonItem
            };

            if (spoonItem != null)
            {
                item.Image = spoonItem.images.First();
            }

            return(item);
        }
Exemplo n.º 5
0
        //TODO: convert all this stuff and get the image from spoonacular
        private async Task <ShoppingListItem> ConvertToShoppingListItem(ItemShoppingListLinkEntity itemShoppingListLink, int storeId, CancellationToken ct)
        {
            /*
             *     public class ShoppingListItem
             *     {
             *      public int Id { get; set; }
             *
             *      public string Image { get; set; }
             *
             *      public string Name { get; set; }
             *
             *      public decimal Price { get; set; }
             *
             *      public bool InStock { get; set; }
             *
             *      public int StockAmount { get; set; }
             *
             *      public string Aisle { get; set; }
             *
             *      public string Section { get; set; }
             *
             *      public int QuantityBought { get; set; }
             *     }
             *
             */

            ItemEntity itemEntity = await _itemRepository.GetEntityAsync(itemShoppingListLink.ItemId, ct);

            ItemStoreLinkEntity itemStoreLink = await _itemStoreLinkRepository.GetEntityAsync(itemEntity.Id, storeId, ct);

            ShoppingListItem shoppingListItem = new ShoppingListItem
            {
                LinkId       = itemShoppingListLink.Id,
                Name         = itemEntity.Name,
                Price        = itemStoreLink.Price,
                InStock      = itemStoreLink.InStock,
                StockAmount  = itemStoreLink.StockAmount,
                ItemQuantity = itemShoppingListLink.ItemQuantity
            };

            return(shoppingListItem);
        }
        public async Task <ItemStoreLinkEntity> GetEntityAsync(int itemId, int storeId, CancellationToken ct)
        {
            try
            {
                ItemStoreLinkEntity itemStoreLink = await _dbContext.ItemStoreLinks.FirstOrDefaultAsync(link => link.ItemId == itemId && link.StoreId == storeId);

                if (itemStoreLink != null)
                {
                    return(itemStoreLink);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                return(null);
            }
        }