public async Task <bool> UpdateAsync(ProductModifyRequest request) { var modifiedDate = DateTimeOffset.UtcNow; request.UpdatedDate = modifiedDate; var isUpdated = await _productRepository.UpdateAsync(request); if (!isUpdated) { return(false); } await _productPictureRepository.UpdateAsync(new ProductPicturesModifyRequest { ProductId = request.Id, CreatedById = request.CreatedById, UpdatedById = request.UpdatedById, CreatedDate = modifiedDate, UpdatedDate = modifiedDate, Pictures = request.Pictures }); // Delete all product attributes in cases no product attributes from the request if (request.ProductAttributes == null || !request.ProductAttributes.Any()) { await _productAttributeRepository.DeleteAttributeRelationByProductIdAsync(request.Id); return(isUpdated); } var groupOfAttributes = new List <ProductAttributeRelationRequest>(); foreach (var attributeRelation in request.ProductAttributes) { var existAttribute = groupOfAttributes.FirstOrDefault(x => x.ProductAttributeId == attributeRelation.ProductAttributeId); if (existAttribute != null) { var attributeValues = existAttribute.AttributeRelationValues.ToList(); attributeValues.AddRange(attributeRelation.AttributeRelationValues); existAttribute.AttributeRelationValues = attributeValues; } else { groupOfAttributes.Add(attributeRelation); } } var productAttributeIds = request.ProductAttributes.Where(x => x.Id != 0).Select(x => x.Id); await _productAttributeRepository.DeleteAttributeRelationNotInIdsAsync(request.Id, productAttributeIds); foreach (var attributeRelation in request.ProductAttributes) { attributeRelation.ProductId = request.Id; var isAttributeRelationExist = attributeRelation.Id != 0 && await _productAttributeRepository.IsAttributeRelationExistAsync(attributeRelation.Id); if (!isAttributeRelationExist) { await _productAttributeRepository.CreateAttributeRelationAsync(attributeRelation); } else { await _productAttributeRepository.UpdateAttributeRelationAsync(attributeRelation); } } return(isUpdated); }