public Task <ProductImage> AddImage(string name, string description, int productId) { return(_productImageRepository.AddAsync(new ProductImage { ProductId = productId, Name = name, Description = description })); }
public async Task <IActionResult> Update(ProductViewModel model) { if (ModelState.IsValid) { try { List <string> productImageList = ProcessUploadedFile(model); var product = _productRepository.GetProductById(model.Id); PropertyCopy.Copy(model, product); product.PriceAfter = Math.Round((double)((1 - (model.Discount + model.ExtraDiscount) / 100) * model.Price.GetValueOrDefault()), 1, MidpointRounding.AwayFromZero); product.SeoAlias = TextHelper.ToUnsignString(model.ProductName); _productRepository.UpdateAsync(product); await _productRepository.SaveAsync(RequestContext); _cache.Remove("CACHE_MASTER_PRODUCT"); if (productImageList != null && productImageList.Count > 0) { foreach (var image in productImageList) { await _productImageRepository.AddAsync(new ProductImage() { Id = Guid.NewGuid().ToString(), ProductId = model.Id, Url = image, CreateAt = DateTime.UtcNow }); } await _productImageRepository.SaveAsync(RequestContext); } var listProductTag = _productTagRepository.GetProductTagViewModelsByProductId(model.Id).Select(s => s.TagId).ToList(); if (model.TagList != null && model.TagList.Count > 0) { foreach (var item in model.TagList) { if (!listProductTag.Contains(item)) { await _productTagRepository.AddAsync(new ProductTag() { Id = Guid.NewGuid().ToString(), ProductId = model.Id, TagId = item }); } } await _productTagRepository.SaveAsync(RequestContext); if (listProductTag.Count > 0) { foreach (var item in listProductTag) { if (!model.TagList.Contains(item)) { DeleteProductTag(model.Id, item); } } } } else { if (listProductTag.Count > 0) { foreach (var tagId in listProductTag) { DeleteProductTag(model.Id, tagId); } } } } catch (Exception) { SetComboData(); return(View()); } return(RedirectToAction("Index")); } SetComboData(); return(View()); }
public async Task AddProductFileAsync(Guid fileName, int productId) { await _productImageRepository.AddAsync(new ProductImage { Id = fileName, ProductId = productId }); await _dbContext.SaveChangesAsync(); }