/// <summary> /// Updates a customized product collection's basic information /// </summary> /// <param name="modelView">model view with the updates for a customized product collection</param> /// <returns>Updated customized product collection or throws an exception if an error occurs</returns> public static CustomizedProductCollection update(UpdateCustomizedProductCollectionModelView modelView) { CustomizedProductCollectionRepository customizedProductCollectionRepository = PersistenceContext.repositories() .createCustomizedProductCollectionRepository(); CustomizedProductCollection customizedProductCollection = customizedProductCollectionRepository.find(modelView.customizedProductCollectionId); checkIfCustomizedProductCollectionWasFound( customizedProductCollection, modelView.customizedProductCollectionId ); customizedProductCollection.changeName(modelView.name); CustomizedProductCollection updatedCustomizedProductCollection = customizedProductCollectionRepository.update(customizedProductCollection); checkIfUpdatedCustomizedProductCollectionWasSaved( updatedCustomizedProductCollection, modelView.customizedProductCollectionId ); return(updatedCustomizedProductCollection); }
/// <summary> /// Deletes a customized product from a customized product collection /// </summary> /// <param name="modelView"> model view with the necessary information to perform the request</param> public static void delete(DeleteCustomizedProductFromCustomizedProductCollectionModelView modelView) { CustomizedProductCollectionRepository customizedProductCollectionRepository = PersistenceContext.repositories() .createCustomizedProductCollectionRepository(); CustomizedProductCollection customizedProductCollection = customizedProductCollectionRepository.find(modelView.customizedProductCollectionId); checkIfCustomizedProductCollectionWasFound(customizedProductCollection, modelView.customizedProductCollectionId); CustomizedProduct customizedProduct = PersistenceContext.repositories() .createCustomizedProductRepository() .find(modelView.customizedProductId); if (customizedProduct == null) { throw new ArgumentException( string.Format( UNABLE_TO_FIND_CUSTOMIZED_PRODUCT, modelView.customizedProductId) ); } customizedProductCollection.removeCustomizedProduct(customizedProduct); customizedProductCollectionRepository.update(customizedProductCollection); }
/// <summary> /// Deactivates a customized product collection /// </summary> /// <param name="modelView"> model view with the necessary information to deactivate a customized product collection</param> public static void deactivate(DeleteCustomizedProductCollectionModelView modelView) { CustomizedProductCollectionRepository customizedProductCollectionRepository = PersistenceContext.repositories() .createCustomizedProductCollectionRepository(); CustomizedProductCollection customizedProductCollection = customizedProductCollectionRepository.find(modelView.customizedProductCollectionId); checkIfCustomizedProductCollectionWasFound(customizedProductCollection, modelView.customizedProductCollectionId); customizedProductCollectionRepository.remove(customizedProductCollection); }
/// <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> /// Transforms a CommercialCatalogueDTO into a CommercialCatalogue. /// </summary> /// <param name="commercialCatalogueDTO">CommercialCatalogueDTO with the CommercialCatalogue data.</param> /// <returns>Transformed CommercialCatalogue built with CommercialCatalogueDTO info.</returns> public static CommercialCatalogue transform(CommercialCatalogueDTO commercialCatalogueDTO) { string reference = commercialCatalogueDTO.reference; string designation = commercialCatalogueDTO.designation; CommercialCatalogue newComCatalogue = null; //if no collections are specified, build a catalogue with just the given reference and designation. if (commercialCatalogueDTO.catalogueCollectionDTOs != null) { CustomizedProductCollectionRepository collectionRepository = PersistenceContext.repositories().createCustomizedProductCollectionRepository(); CustomizedProductRepository customizedProductRepository = PersistenceContext.repositories().createCustomizedProductRepository(); //the specified collections should have id's and not the whole collection information List <CatalogueCollection> catalogueCollections = new List <CatalogueCollection>(); foreach (CatalogueCollectionDTO collectionDTO in commercialCatalogueDTO.catalogueCollectionDTOs) { CatalogueCollection catalogueCollection = null; CustomizedProductCollection collection = collectionRepository.find(collectionDTO.customizedProductCollectionDTO.id); //the collection was not found if (collection == null) { throw new ArgumentException(ERROR_CUSTOMIZED_PRODUCT_COLLECTION_NOT_FOUND); } //if no products are specified, then add all contained in the collection if (collectionDTO.customizedProductDTOs == null) { catalogueCollection = new CatalogueCollection(collection); } else { List <CustomizedProduct> customizedProducts = new List <CustomizedProduct>(); foreach (CustomizedProductDTO customizedProductDTO in collectionDTO.customizedProductDTOs) { CustomizedProduct customizedProduct = customizedProductRepository.find(customizedProductDTO.id); //the product was not found if (customizedProduct == null) { throw new ArgumentException(ERROR_CUSTOMIZED_PRODUCT_NOT_FOUND); } customizedProducts.Add(customizedProduct); } catalogueCollection = new CatalogueCollection(collection, customizedProducts); } catalogueCollections.Add(catalogueCollection); } newComCatalogue = new CommercialCatalogue(reference, designation, catalogueCollections); } else { newComCatalogue = new CommercialCatalogue(reference, designation); } return(newComCatalogue); }