public ActionResult deleteCustomizedProduct(long customizedProductId, [FromHeader(Name = "UserToken")] string userAuthToken) { try { DeleteCustomizedProductModelView deleteCustomizedProductModelView = new DeleteCustomizedProductModelView(); deleteCustomizedProductModelView.customizedProductId = customizedProductId; new core.application.CustomizedProductController().deleteCustomizedProduct(deleteCustomizedProductModelView); return(NoContent()); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (NotAuthorizedException notAuthorizedException) { return(StatusCode(401, new SimpleJSONMessageService(notAuthorizedException.Message))); } catch (ArgumentException e) { return(BadRequest(new SimpleJSONMessageService(e.Message))); } catch (InvalidOperationException e) { return(BadRequest(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }
/// <summary> /// Deletes an instance of CustomizedProduct. /// </summary> /// <param name="deleteCustomizedProductModelView">Instance of DeleteCustomizedProductModelView containing the CustomizedProduct's identifier.</param> /// <exception cref="ResourceNotFoundException">Thrown when no CustomizedProduct could be found with the given identifier.</exception> public void deleteCustomizedProduct(DeleteCustomizedProductModelView deleteCustomizedProductModelView) { CustomizedProductRepository customizedProductRepository = PersistenceContext.repositories().createCustomizedProductRepository(); CustomizedProduct customizedProduct = customizedProductRepository.find(deleteCustomizedProductModelView.customizedProductId); if (customizedProduct == null) { throw new ResourceNotFoundException( string.Format(ERROR_UNABLE_TO_FIND_CUSTOMIZED_PRODUCT_BY_ID, deleteCustomizedProductModelView.customizedProductId) ); } checkUserToken(customizedProduct, deleteCustomizedProductModelView.userAuthToken); //check if it's a sub customized product if (customizedProduct.insertedInSlotId.HasValue) { CustomizedProduct parent = customizedProductRepository.findCustomizedProductBySlot(customizedProduct.insertedInSlot); Slot insertedInSlot = customizedProduct.insertedInSlot; //remove the sub customized product and update parent parent.removeCustomizedProduct(customizedProduct, insertedInSlot); customizedProductRepository.update(parent); } else { //if it's a base product, just deactivate it customizedProductRepository.remove(customizedProduct); } }