public ActionResult addCatalogueCollection(long commercialCatalogueId, [FromBody] AddCatalogueCollectionModelView addCatalogueCollectionModelView) { if (addCatalogueCollectionModelView == null) { return(BadRequest(new SimpleJSONMessageService(INVALID_REQUEST_BODY_MESSAGE))); } try { addCatalogueCollectionModelView.commercialCatalogueId = commercialCatalogueId; GetCommercialCatalogueModelView commercialCatalogueModelView = new core.application.CommercialCatalogueController() .addCatalogueCollection(addCatalogueCollectionModelView); return(Ok(commercialCatalogueModelView)); } 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> /// Creates an instance of CatalogueCollection with the data in the given AddCatalogueCollectionModelView. /// </summary> /// <param name="addCatalogueCollectionModelView">AddCatalogueCollectionModelView with the CatalogueCollection's data.</param> /// <returns>An instance of CatalogueCollection.</returns> /// <exception cref="System.ArgumentException"> /// Thrown when no CustomizedProductCollection or CustomizedProduct could be found with the provided identifiers. /// </exception> public static CatalogueCollection create(AddCatalogueCollectionModelView addCatalogueCollectionModelView) { CustomizedProductCollectionRepository collectionRepository = PersistenceContext.repositories().createCustomizedProductCollectionRepository(); CustomizedProductCollection customizedProductCollection = collectionRepository.find(addCatalogueCollectionModelView.customizedProductCollectionId); if (customizedProductCollection == null) { throw new ArgumentException(string.Format(CUSTOMIZED_PRODUCT_COLLECTION_NOT_FOUND, addCatalogueCollectionModelView.customizedProductCollectionId)); } CatalogueCollection catalogueCollection = null; //check if any customized product was defined if (addCatalogueCollectionModelView.customizedProductIds.Any()) { IEnumerable <long> customizedProductIds = addCatalogueCollectionModelView.customizedProductIds.Select(cp => cp.customizedProductId).ToList(); List <CustomizedProduct> customizedProducts = new List <CustomizedProduct>(); foreach (long customizedProductId in customizedProductIds) { CustomizedProduct customizedProduct = customizedProductCollection.collectionProducts .Where(cp => cp.customizedProductId == customizedProductId) .Select(cp => cp.customizedProduct).SingleOrDefault(); if (customizedProduct == null) { throw new ArgumentException(string.Format( CUSTOMIZED_PRODUCT_NOT_FOUND_IN_COLLECTION, customizedProductId, addCatalogueCollectionModelView.customizedProductCollectionId) ); } customizedProducts.Add(customizedProduct); } catalogueCollection = new CatalogueCollection(customizedProductCollection, customizedProducts); } else { catalogueCollection = new CatalogueCollection(customizedProductCollection); } return(catalogueCollection); }
/// <summary> /// Adds a CatalogueCollection to a CommercialCatalogue. /// </summary> /// <param name="addCatalogueCollectionModelView">AddCatalogueCollectionModelView with the data used for creating a CatalogueCollection.</param> /// <returns>GetCommercialCatalogueModelView representing the updated CommercialCatalogue.</returns> /// <exception cref="ResourceNotFoundException"> /// Thrown when no CommercialCatalogue could be found with the provided identifier. /// </exception> /// <exception cref="System.ArgumentException"> /// Thrown when no CustomizedProductCollection or CustomizedProduct could be found with the provided identifiers. /// </exception> public GetCommercialCatalogueModelView addCatalogueCollection(AddCatalogueCollectionModelView addCatalogueCollectionModelView) { CommercialCatalogueRepository catalogueRepository = PersistenceContext.repositories().createCommercialCatalogueRepository(); CommercialCatalogue commercialCatalogue = catalogueRepository.find(addCatalogueCollectionModelView.commercialCatalogueId); if (commercialCatalogue == null) { throw new ResourceNotFoundException(string.Format( CATALOGUE_NOT_FOUND_BY_ID, addCatalogueCollectionModelView.commercialCatalogueId )); } CatalogueCollection catalogueCollection = CreateCatalogueCollectionService.create(addCatalogueCollectionModelView); commercialCatalogue.addCollection(catalogueCollection); commercialCatalogue = catalogueRepository.update(commercialCatalogue); return(CommercialCatalogueModelViewService.fromEntity(commercialCatalogue)); }