/// <summary> /// Deletes a Restriction from a Product's Measurement. /// </summary> /// <param name="deleteRestrictionFromProductMeasurementMV">DeleteRestrictionFromProductMeasurementModelView with the Product's, the Measurement's and the Restriction's persistence identifier.</param> /// <exception cref="ResourceNotFoundException">Thrown when the Product, the Measurement or the Restriction could not be found.</exception> public void deleteRestrictionFromProductMeasurement(DeleteMeasurementRestrictionModelView deleteRestrictionFromProductMeasurementMV) { ProductRepository productRepository = PersistenceContext.repositories().createProductRepository(); Product product = productRepository.find(deleteRestrictionFromProductMeasurementMV.productId); if (product == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, deleteRestrictionFromProductMeasurementMV.productId)); } Measurement measurement = product.productMeasurements.Where(pm => pm.measurementId == deleteRestrictionFromProductMeasurementMV.measurementId) .Select(pm => pm.measurement).SingleOrDefault(); if (measurement == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_MEASUREMENT_BY_ID, deleteRestrictionFromProductMeasurementMV.measurementId)); } Restriction restriction = measurement.restrictions.Where(r => r.Id == deleteRestrictionFromProductMeasurementMV.restrictionId).SingleOrDefault(); if (restriction == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_RESTRICTION_BY_ID, deleteRestrictionFromProductMeasurementMV.restrictionId)); } measurement.restrictions.Remove(restriction); product = productRepository.update(product); }
public ActionResult deleteRestrictionFromProductMeasurement(long productId, long measurementId, long restrictionId) { DeleteMeasurementRestrictionModelView deleteRestrictionFromProductMeasurementMV = new DeleteMeasurementRestrictionModelView(); deleteRestrictionFromProductMeasurementMV.productId = productId; deleteRestrictionFromProductMeasurementMV.measurementId = measurementId; deleteRestrictionFromProductMeasurementMV.restrictionId = restrictionId; try { new core.application.ProductController().deleteRestrictionFromProductMeasurement(deleteRestrictionFromProductMeasurementMV); return(NoContent()); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }