public async Task <IHttpActionResult> UpdateOptionAsync(Guid productId, Guid id, [FromBody] ProductOptionDto updatedProductOption) { if (id == null) { return(BadRequest("An ID must be provided.")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Verify product ID is valid var existingProduct = await _productRepository.GetByIdAsync(productId); if (existingProduct == null) { return(BadRequest("Product doesn't exist.")); } // Map Dto to Model var productOptionToUpdate = new ProductOption(); productOptionToUpdate.MapDto(updatedProductOption, productId); await _productOptionsRepository.UpdateAsync(id, productId, productOptionToUpdate); return(Ok()); }
public async Task <IHttpActionResult> CreateOptionAsync(Guid productId, [FromBody] ProductOptionDto productOption) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Verify product ID is valid var productToAddOptionTo = await _productRepository.GetByIdAsync(productId); if (productToAddOptionTo == null) { return(BadRequest("Product doesn't exist.")); } // Map Dto to Model var productOptionToCreate = new ProductOption(); productOptionToCreate.MapDto(productOption, productId); await _productOptionsRepository.SaveAsync(productId, productOptionToCreate); // Verify save was successful var newProductOption = await _productOptionsRepository.GetByIdAsync(productOptionToCreate.Id, productId); if (newProductOption == null) { return(InternalServerError()); } else { return(Created(new EntityUrlByID(Url.Request.RequestUri, newProductOption.Id).ToString(), newProductOption)); } }