public ActionResult <ProductForPostRequestDto> UpdateProduct(int productId, ProductForPostRequestDto product) { if (productId == 0) { return(BadRequest()); } if (productId != product.ProductId) { return(BadRequest()); } var productToUpdate = _productService.GetProductById(productId); var attributes = new List <ProductAttribute>(); if (product.ProductAttributesDto.Count > 0) { productToUpdate.ProductAttributes.Clear(); foreach (var item in product.ProductAttributesDto) { var attribute = new ProductAttribute(); if (item.AttributeId > 0 && productToUpdate.ProductAttributes.Any(c => c.AttributeId == item.AttributeId)) { var attr = productToUpdate.ProductAttributes.FirstOrDefault(c => c.AttributeId == item.AttributeId); attr.AttributeLookup = new ProductAttributeLookup { AttributeName = item.AttributeName, ProdCatId = product.ProdCatId, }; attr.AttributeId = item.AttributeId; attr.AttributeValue = item.AttributeValue; attr.ProductId = item.ProductId; attr.Product = productToUpdate; } else { var attributeLookup = new ProductAttributeLookup { AttributeName = item.AttributeName, ProdCatId = product.ProdCatId, }; attribute.AttributeId = item.AttributeId; attribute.AttributeLookup = attributeLookup; attribute.AttributeValue = item.AttributeValue; attribute.Product = productToUpdate; attribute.ProductId = item.ProductId; productToUpdate.ProductAttributes.Add(attribute); } } } _productService.UpdateProduct(productToUpdate); return(Ok(product)); }
public void Update(ProductAttributeLookup attributeLookup) { _dbContext.Entry(attributeLookup).State = EntityState.Modified; }
public void Remove(ProductAttributeLookup attributeLookup) { _dbContext.ProductAttributeLookups.Remove(attributeLookup); }
public void Add(ProductAttributeLookup attributeLookup) { _dbContext.ProductAttributeLookups.Add(attributeLookup); }
public int Update(ProductAttributeLookup attributeLookup) { _unitOfWork.AttributesRepo.Update(attributeLookup); return(_unitOfWork.Commit()); }
public void Remove(ProductAttributeLookup attributeLookup) { _unitOfWork.AttributesRepo.Remove(attributeLookup); _unitOfWork.Commit(); }
public int Add(ProductAttributeLookup attributeLookup) { _unitOfWork.AttributesRepo.Add(attributeLookup); return(_unitOfWork.Commit()); }
public MappingProfile() { // Domain to DTO CreateMap <Product, ProductForPostRequestDto>().ForMember( dto => dto.ProductAttributesDto, opt => opt.MapFrom(x => x.ProductAttributes.Select(y => new ProductAttributesDTO { AttributeId = y.AttributeId, AttributeValue = y.AttributeValue, ProductId = y.ProductId, AttributeName = y.AttributeLookup.AttributeName }).ToList())); CreateMap <ProductCategory, ProductCategoryDto>(); CreateMap <ProductAttributeLookup, ProductAttributesDTO>(); CreateMap <Product, ProductDto>(); // DTO to Domain CreateMap <ProductForPostRequestDto, Product>().AfterMap((s, d) => { var attributes = new List <ProductAttribute>(); var lookups = new List <ProductAttributeLookup>(); foreach (var item in s.ProductAttributesDto) { var attribute = new ProductAttribute { AttributeId = item.AttributeId, AttributeValue = item.AttributeValue, ProductId = item.ProductId, Product = new Product { ProdCatId = s.ProdCatId, ProdDescription = s.ProdDescription, ProdName = s.ProdName, ProductId = s.ProductId } }; var lookup = new ProductAttributeLookup { AttributeId = item.AttributeId, AttributeName = item.AttributeName, ProdCatId = s.ProdCatId }; attribute.AttributeLookup = lookup; attributes.Add(attribute); } // d.ProductCategory = new ProductCategory { CategoryName = s.ProductCategory.CategoryName, ProdCatId = s.ProductCategory.ProdCatId }; d.ProductAttributes = attributes; }); CreateMap <ProductCategoryDto, ProductCategory>(); CreateMap <ProductDto, Product>(); CreateMap <ProductAttributesDTO, ProductAttributeLookup>() .ForMember(mem => mem.ProductAttributes, source => source.MapFrom(x => new ProductAttribute { AttributeId = x.AttributeId, AttributeValue = x.AttributeValue, AttributeLookup = new ProductAttributeLookup { AttributeName = x.AttributeName, AttributeId = x.AttributeId } })); }