コード例 #1
0
        public async Task <IActionResult> Edit(ProductAdminEditViewModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            await this.productsService.EditProductAsync(inputModel);

            return(this.RedirectToAction("AllProducts"));
        }
コード例 #2
0
        public async Task EditProductAsync(ProductAdminEditViewModel inputModel)
        {
            if (!this.dbContext.ProductCategories.Any(u => u.Id == inputModel.CategoryId))
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidProductCategoryId, inputModel.CategoryId));
            }

            var product = await this.dbContext.Products
                          .Include(p => p.Category)
                          .Include(p => p.Images)
                          .FirstOrDefaultAsync(a => a.Id == inputModel.Id);

            if (product == null)
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidProductId, inputModel.Id));
            }

            Enum.TryParse(inputModel.Size, true, out ProductSize productSize);

            Enum.TryParse(inputModel.Color, true, out ProductColor productColor);

            Enum.TryParse(inputModel.Sort, true, out ProductSort productSort);

            Enum.TryParse(inputModel.Type, true, out ProductCategoryType productType);

            product.IsDeleted = inputModel.IsDeleted;

            product.DeletedOn = inputModel.DeletedOn;

            product.CreatedOn = inputModel.CreatedOn;

            product.ModifiedOn = inputModel.ModifiedOn;

            product.Name = inputModel.Name;

            product.Price = inputModel.Price;

            product.Description = inputModel.Description;

            product.Rating = inputModel.Rating;

            product.IsSoldOut = inputModel.IsSoldOut;

            product.CategoryId = inputModel.CategoryId;

            product.Category.Name = inputModel.CategoryName;

            product.Category.IsDeleted = inputModel.CategoryIsDeleted;

            product.Size = productSize;

            product.Color = productColor;

            product.Sort = productSort;

            product.Type = productType;

            this.dbContext.Update(product);

            await this.dbContext.SaveChangesAsync();
        }