public async Task <IActionResult> PostCustomerCartDetail([FromBody] DTOs.CustomerCartDetail detail)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var productInCart = _context.CustomerCartDetail.Find(detail.CustomerCartId, detail.ProductId);

            if (productInCart != null)
            {
                productInCart.Quantity += detail.Quantity;

                _context.Update(productInCart);
            }
            else
            {
                var customerCartDetail = new CustomerCartDetail
                {
                    ProductId      = detail.ProductId,
                    CustomerCartId = detail.CustomerCartId,
                    Price          = detail.Price,
                    DisplayPrice   = detail.DisplayPrice,
                    Quantity       = detail.Quantity
                };

                _context.CustomerCartDetail.Add(customerCartDetail);
            }

            return(await _context.SaveChangesAsync() > 0 ? StatusCode(200) : StatusCode(409));
        }
Esempio n. 2
0
        public async Task <IActionResult> PostProduct([FromBody] AddProductDTO model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var product = new Product
            {
                Name              = model.Name,
                IsHide            = false,
                AddDate           = model.AddDate,
                CategoryId        = model.CategoryId,
                BrandId           = model.BrandId,
                ProductStatusId   = model.ProductStatusId,
                DisplayPrice      = model.DisplayPrice,
                MaximumQuantity   = model.MaximumQuantity,
                ProductThumbImage = model.ThumbImageId,
                CurrentQuantity   = model.Quantity,
                RealPrice         = model.RealPrice,
                ProductLongDesc   = model.ProductLongDesc,
                ProductShortDesc  = model.ProductShortDesc,
                Weight            = model.Weight,
                Slug              = model.Slug
            };

            _context.Product.Add(product);

            if (await _context.SaveChangesAsync() > 0)
            {
//                product.Slug = "/product" + "/p" + product.ProductId + "/" + product.Slug;
                product.Slug = "/product/" + product.Slug + "-p" + product.ProductId;
                _context.Entry(product).State = EntityState.Modified;

                var productImages = new List <ProductImage>();

                foreach (var imageId in model.ProductImageIds)
                {
                    productImages.Add(new ProductImage
                    {
                        GeneralImageId = imageId,
                        ProductId      = product.ProductId
                    });
                }

                _context.ProductImage.AddRange(productImages);

                var brand = await _context.Brand.FindAsync(product.BrandId);

                brand.TotalProduct++;

                _context.Update(brand);

                var category = await _context.Category.FindAsync(product.CategoryId);

                category.TotalProduct++;

                _context.Update(category);

                if (await _context.SaveChangesAsync() > 0)
                {
                    return(StatusCode(201));
                }
            }


            return(StatusCode(424));
        }