Exemplo n.º 1
0
        public async Task Delete(string id)
        {
            ShopApp.Models.Product product = this.dbContext.Products.FirstOrDefault(p => p.Id == id);

            // we delete the product only if it already exists
            if (product != null)
            {
                this.dbContext.Products.Remove(product);
                await this.dbContext.SaveChangesAsync();
            }
        }
Exemplo n.º 2
0
        public async Task Edit(ProductBaseInputModel model)
        {
            ShopApp.Models.Product productEntity = this.dbContext.Products
                                                   .FirstOrDefault(product => product.Id == ((ProductEditModel)model).Id);

            if (productEntity == null)
            {
                throw new InvalidOperationException("Invalid product id!");
            }

            productEntity.Name        = model.Name;
            productEntity.Price       = model.Price;
            productEntity.Description = model.Description;
            productEntity.CoverUrl    = model.CoverUrl;
            productEntity.CategoryId  = model.CategoryId;

            await this.dbContext.SaveChangesAsync();
        }
Exemplo n.º 3
0
        public async Task <ProductBaseInputModel> Create(ProductBaseInputModel model)
        {
            ShopApp.Models.Product productEntity = new ShopApp.Models.Product
            {
                Id          = Guid.NewGuid().ToString(),
                Name        = model.Name,
                AddedOn     = DateTime.UtcNow,
                Description = model.Description,
                CoverUrl    = model.CoverUrl,
                Price       = model.Price,
                CategoryId  = model.CategoryId
            };

            this.dbContext.Products.Add(productEntity);
            await this.dbContext.SaveChangesAsync();

            return(model);
        }
Exemplo n.º 4
0
        public async Task DecrementProductStockCount(string id, int quantity)
        {
            bool productExists = this.Exists(id);

            if (productExists)
            {
                ShopApp.Models.Product product = this.dbContext.Products.FirstOrDefault(p => p.Id == id);

                if (product.StockCount - quantity < 0)
                {
                    throw new InvalidOperationException($"Not enough quantity of {product.Name} product.");
                }

                product.StockCount = product.StockCount - quantity;
                await this.dbContext.SaveChangesAsync();
            }
            else
            {
                throw new InvalidOperationException($"Invalid product id.");
            }
        }