public ActionResult updateProductProperties(long id, [FromBody] UpdateProductPropertiesModelView updateProductPropertiesModelView) { if (updateProductPropertiesModelView == null) { return(BadRequest(new SimpleJSONMessageService(INVALID_PRODUCT_DATA))); } updateProductPropertiesModelView.id = id; try { GetProductModelView updatedProductMV = new core.application.ProductController().updateProductProperties(updateProductPropertiesModelView); return(Ok(updatedProductMV)); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (ArgumentException e) { return(BadRequest(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }
/// <summary> /// Updates the properties of a product /// </summary> /// <param name="updateProductPropertiesModelView">UpdateProductPropertiesModelView with the data regarding the product update</param> /// <returns>GetProductModelView with the updated product information</returns> public GetProductModelView updateProductProperties(UpdateProductPropertiesModelView updateProductPropertiesModelView) { ProductRepository productRepository = PersistenceContext.repositories().createProductRepository(); Product productBeingUpdated = productRepository.find(updateProductPropertiesModelView.id); if (productBeingUpdated == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, updateProductPropertiesModelView.id)); } //FetchEnsurance.ensureProductFetchWasSuccessful(productBeingUpdated); bool perfomedAtLeastOneUpdate = false; if (updateProductPropertiesModelView.reference != null) { productBeingUpdated.changeProductReference(updateProductPropertiesModelView.reference); perfomedAtLeastOneUpdate = true; } if (updateProductPropertiesModelView.designation != null) { productBeingUpdated.changeProductDesignation(updateProductPropertiesModelView.designation); perfomedAtLeastOneUpdate = true; } if (updateProductPropertiesModelView.modelFilename != null) { productBeingUpdated.changeModelFilename(updateProductPropertiesModelView.modelFilename); perfomedAtLeastOneUpdate = true; } if (updateProductPropertiesModelView.categoryId.HasValue) { ProductCategory categoryToUpdate = PersistenceContext.repositories().createProductCategoryRepository().find(updateProductPropertiesModelView.categoryId.Value); if (categoryToUpdate == null) { throw new ArgumentException(string.Format(ERROR_UNABLE_TO_FIND_CATEGORY_BY_ID, updateProductPropertiesModelView.categoryId.Value)); } productBeingUpdated.changeProductCategory(categoryToUpdate); perfomedAtLeastOneUpdate = true; } //?Should an error be thrown if no update was performed or should it just carry on? //UpdateEnsurance.ensureAtLeastOneUpdateWasPerformed(perfomedAtLeastOneUpdate); if (!perfomedAtLeastOneUpdate) { throw new ArgumentException(ERROR_NO_UPDATE_PERFORMED); } productBeingUpdated = productRepository.update(productBeingUpdated); //the updated product will only be null, if the reference was changed to match a previosuly added product if (productBeingUpdated == null) { throw new ArgumentException(ERROR_UNABLE_TO_UPDATE_PRODUCT); } //UpdateEnsurance.ensureProductUpdateWasSuccessful(productBeingUpdated); return(ProductModelViewService.fromEntity(productBeingUpdated)); }