/// <summary> /// Deletes an instance of Restriction from a Product's Component. /// </summary> /// <param name="deleteRestrictionFromProductComponentMV">DeleteRestrictionFromProductComponentDTO with the restriction deletion information</param> /// <exception cref="ResourceNotFoundException">Thrown when either of the Products or the Restriction could not be found.</exception> public void deleteRestrictionFromProductComponent(DeleteComponentRestrictionModelView deleteRestrictionFromProductComponentMV) { ProductRepository productRepository = PersistenceContext.repositories().createProductRepository(); Product productWithComponentBeingDeletedRestriction = productRepository.find(deleteRestrictionFromProductComponentMV.fatherProductId); if (productWithComponentBeingDeletedRestriction == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, deleteRestrictionFromProductComponentMV.fatherProductId)); } Component productComponentBeingDeletedRestriction = productWithComponentBeingDeletedRestriction.components .Where(component => component.complementaryProduct.Id == deleteRestrictionFromProductComponentMV.childProductId).SingleOrDefault(); if (productComponentBeingDeletedRestriction == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, deleteRestrictionFromProductComponentMV.childProductId)); } Restriction restriction = productComponentBeingDeletedRestriction.restrictions .Where(r => r.Id == deleteRestrictionFromProductComponentMV.restrictionId).SingleOrDefault(); if (restriction == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_RESTRICTION_BY_ID, deleteRestrictionFromProductComponentMV.restrictionId)); } productComponentBeingDeletedRestriction.restrictions.Remove(restriction); productWithComponentBeingDeletedRestriction = productRepository.update(productWithComponentBeingDeletedRestriction); }
public ActionResult deleteRestrictionFromProductComponent(long productID, long componentID, long restrictionID) { DeleteComponentRestrictionModelView deleteRestrictionFromProductComponentMV = new DeleteComponentRestrictionModelView(); deleteRestrictionFromProductComponentMV.fatherProductId = productID; deleteRestrictionFromProductComponentMV.childProductId = componentID; deleteRestrictionFromProductComponentMV.restrictionId = restrictionID; try { new core.application.ProductController().deleteRestrictionFromProductComponent(deleteRestrictionFromProductComponentMV); return(NoContent()); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }