public async Task <Dal.Entity.Product> AddProduct(Dal.Entity.Product product) { //If product is invalid, throw service exceptions ValidateProduct(product); await GenerateProductSlug(product); await _unitOfWork.ProductRepository.Add(product); await _unitOfWork.SaveChangesAsync(); return(await GetById(product.Id)); }
private async Task GenerateProductSlug(Dal.Entity.Product product) { var generatedSlug = SlugExtenstions.ToUrlSlug(product.Title); var slug = generatedSlug; if (await _unitOfWork.ProductRepository.GetAllAsQueryable().AnyAsync(p => p.Slug == generatedSlug)) { var existsSlugCount = await _unitOfWork.ProductRepository.GetAllAsQueryable().CountAsync(p => p.Slug == generatedSlug); slug = SlugExtenstions.ToUrlSlug($"{product.Title}-{existsSlugCount}"); } product.Slug = slug; }
private void ValidateProduct(Dal.Entity.Product product) { if (product == null) { throw new ArgumentNullException(nameof(product)); } if (!product.Images.Any()) { throw new ServiceException("Product must have at least one image"); } if (!product.ProductVariants.Any()) { throw new ServiceException("Product must have at least one variant"); } }
public async Task <Dal.Entity.Product> UpdateProduct(Dal.Entity.Product product) { var currentProduct = await GetById(product.Id); //Generate slug if title is changed if (currentProduct.Title != product.Title || string.IsNullOrEmpty(currentProduct.Slug)) { await GenerateProductSlug(product); currentProduct.Slug = product.Slug; } currentProduct.CategoryId = product.CategoryId; currentProduct.Title = product.Title; currentProduct.Description = product.Description; currentProduct.Vendor = product.Vendor; currentProduct.Updated = DateTimeOffset.Now; //---Update images--- //Add new images var addedImages = product.Images.Where(image => currentProduct.Images.All(currentProductImage => currentProductImage.Name != image.Name)) .ToList(); //Remove, removed images currentProduct.Images.RemoveAll(image => product.Images.All(updateProductImage => updateProductImage.Name != image.Name)); currentProduct.Images.AddRange(addedImages); //---Update product variants--- //Add new product variants currentProduct.ProductVariants.AddRange(product.ProductVariants.Where(productVariants => productVariants.Id == 0)); //Remove, removed product variants currentProduct.ProductVariants.RemoveAll(productVariant => product.Id != 0 && product.ProductVariants.All(updatedProductVariant => updatedProductVariant.Id != productVariant.Id)); //Update product attributes currentProduct.ProductSpecificationAttributes.AddRange(product.ProductSpecificationAttributes.Where( attributeMapping => currentProduct.ProductSpecificationAttributes.All( currentAttributeMapping => attributeMapping.ProductAttributeValueId != currentAttributeMapping.ProductAttributeValueId))); currentProduct.ProductSpecificationAttributes.RemoveAll( attributeMapping => product.ProductSpecificationAttributes.All( currentAttributeMapping => attributeMapping.ProductAttributeValueId != currentAttributeMapping.ProductAttributeValueId)); //Update product variants currentProduct.ProductVariants.ForEach(productVariant => { var updatedProductVariant = product.ProductVariants.First(variant => variant.Id == productVariant.Id); productVariant.Price = updatedProductVariant.Price; productVariant.DefaultImage = updatedProductVariant.DefaultImage; productVariant.Barcode = updatedProductVariant.Barcode; productVariant.Sku = updatedProductVariant.Sku; productVariant.CompareAtPrice = updatedProductVariant.CompareAtPrice; productVariant.Quantity = updatedProductVariant.Quantity; //Add new attribute mappings productVariant.ProductVariantSpecificationAttributeMappings.AddRange(updatedProductVariant.ProductVariantSpecificationAttributeMappings.Where( attributeMapping => productVariant.ProductVariantSpecificationAttributeMappings.All( currentAttributeMapping => attributeMapping.ProductAttributeValueId != currentAttributeMapping.ProductAttributeValueId))); //Remove, removed attribute mappings productVariant.ProductVariantSpecificationAttributeMappings.RemoveAll( attributeMapping => updatedProductVariant.ProductVariantSpecificationAttributeMappings.All( currentAttributeMapping => attributeMapping.ProductAttributeValueId != currentAttributeMapping.ProductAttributeValueId)); }); //If product is invalid, throw service exceptions ValidateProduct(currentProduct); _unitOfWork.ProductRepository.Update(currentProduct); await _unitOfWork.SaveChangesAsync(); return(await GetById(currentProduct.Id)); }