/// <summary> /// Converts an instance of Component into an instance of GetComponentModelView. /// </summary> /// <param name="component">Instance of Component.</param> /// <returns>An instance of GetComponentModelView.</returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when the provided instance of Component is null. /// </exception> public static GetComponentModelView fromEntity(Component component, string unit) { if (component == null) { throw new ArgumentNullException(ERROR_NULL_COMPONENT); } GetComponentModelView componentModelView = new GetComponentModelView(); componentModelView.productId = component.complementaryProductId; componentModelView.reference = component.complementaryProduct.reference; componentModelView.designation = component.complementaryProduct.designation; componentModelView.modelFilename = component.complementaryProduct.modelFilename; componentModelView.mandatory = component.mandatory; componentModelView.category = ProductCategoryModelViewService.fromEntityAsBasic(component.complementaryProduct.productCategory); if (component.complementaryProduct.components.Any()) { componentModelView.components = ComponentModelViewService.fromCollection(component.complementaryProduct.components); } //no need to check if the product has materials and measurements, since they're mandatory componentModelView.materials = ProductMaterialModelViewService.fromCollection(component.complementaryProduct.productMaterials); componentModelView.measurements = MeasurementModelViewService.fromCollection(component.complementaryProduct.productMeasurements.Select(pm => pm.measurement), unit); if (component.complementaryProduct.supportsSlots) { componentModelView.slotWidths = ProductSlotWidthsModelViewService.fromEntity(component.complementaryProduct.slotWidths, unit); } /*Skip converting Restrictions if the Component has none, * since null GetAllRestrictionsModelView won't be serialized */ if (component.restrictions.Any()) { componentModelView.restrictions = RestrictionModelViewService.fromCollection(component.restrictions); } return(componentModelView); }
/// <summary> /// Creates a model view with a product information. /// </summary> /// <param name="product">Product with the product being created the model view.</param> /// <param name="unit">Unit to which all the dimension data will be converted to.</param> /// <returns>GetProductModelView with the product information model view</returns> /// <exception cref="System.ArgumentNullException">Thrown when the provided instance of Product is null.</exception> public static GetProductModelView fromEntity(Product product, string unit) { if (product == null) { throw new ArgumentNullException(ERROR_NULL_PRODUCT); } GetProductModelView productModelView = new GetProductModelView(); productModelView.productId = product.Id; productModelView.reference = product.reference; productModelView.designation = product.designation; productModelView.modelFilename = product.modelFilename; productModelView.category = ProductCategoryModelViewService.fromEntityAsBasic(product.productCategory); if (product.components.Any()) { productModelView.components = ComponentModelViewService.fromCollection(product.components); } //no need to check if the product has materials and measurements, since they're mandatory productModelView.materials = ProductMaterialModelViewService.fromCollection(product.productMaterials); productModelView.measurements = MeasurementModelViewService.fromCollection(product.productMeasurements.Select(pm => pm.measurement), unit); if (product.supportsSlots) { productModelView.slotWidths = ProductSlotWidthsModelViewService.fromEntity(product.slotWidths, unit); } return(productModelView); }
/// <summary> /// Retrieves a ModelView representation of the instance of ProductCategory with a matching business identifier (name). /// </summary> /// <param name="name">Business identifier (name).</param> /// <returns>ModelView representation of the instance of ProductCategory with a matching business identifier.</returns> public GetProductCategoryModelView findByName(string name) { ProductCategory category = PersistenceContext.repositories().createProductCategoryRepository().find(name); if (category == null) { //category might not exist throw new ArgumentException(ERROR_CATEGORY_NOT_FOUND_NAME); } return(ProductCategoryModelViewService.fromEntity(category)); }
/// <summary> /// Retrieves a ModelView representation of the instance of ProductCategory with a matching database identifier. /// </summary> /// <param name="id">Database identifier.</param> /// <returns>ModelView representation of the ProductCategory with the matching database identifier /// or null if no ProductCategory with a matching id was found.</returns> public GetProductCategoryModelView findByDatabaseId(long id) { ProductCategory category = PersistenceContext.repositories().createProductCategoryRepository().find(id); if (category == null) { //category might not exist throw new ArgumentException(ERROR_CATEGORY_NOT_FOUND_ID); } return(ProductCategoryModelViewService.fromEntity(category)); }
/// <summary> /// Retrieves all instances of ProductCategory that are leaves. /// </summary> /// <returns>GetAllProductCategoriesModelView with data regarding all of the leaf ProductCategory.</returns> /// <exception cref="ResourceNotFoundException">Throw when no leaf ProductCategory is found.</exception> public GetAllProductCategoriesModelView findLeaves() { IEnumerable <ProductCategory> leaves = PersistenceContext.repositories().createProductCategoryRepository().findLeaves(); if (!leaves.Any()) { throw new ResourceNotFoundException(ERROR_NO_CATEGORIES_FOUND); } return(ProductCategoryModelViewService.fromCollection(leaves)); }
/// <summary> /// Retrieves all instances of ProductCategory that are currently present within the repository. /// </summary> /// <returns>Returns a list with ModelViews of all the instances of ProductCategory in the repository. </returns> public GetAllProductCategoriesModelView findAllCategories() { IEnumerable <ProductCategory> categories = PersistenceContext.repositories().createProductCategoryRepository().findAll(); //check if any categories have been added if (!categories.Any()) { throw new ArgumentException(ERROR_NO_CATEGORIES_FOUND); } return(ProductCategoryModelViewService.fromCollection(categories)); }
/// <summary> /// Adds a new ProductCategory to the repository. /// </summary> /// <param name="modelView">ModelView containing ProductCategory information.</param> /// <returns>Returns the added ProductCategory's ModelView.</returns> public GetProductCategoryModelView addProductCategory(AddProductCategoryModelView modelView) { ProductCategoryRepository repository = PersistenceContext.repositories().createProductCategoryRepository(); ProductCategory category = new ProductCategory(modelView.name); category = repository.save(category); //category was not able to be added (probably due to a violation of business identifiers) if (category == null) { throw new ArgumentException(ERROR_UNABLE_TO_ADD_CATEGORY); } return(ProductCategoryModelViewService.fromEntity(category)); }
/// <summary> /// Removes a ProductCategory from the repository. /// </summary> /// <param name="id">Database identifier of the ProductCategory to be removed.</param> /// <returns>A ModelView representation of the removed ProductCategory.</returns> public GetProductCategoryModelView removeProductCategory(long id) { ProductCategoryRepository categoryRepository = PersistenceContext.repositories().createProductCategoryRepository(); ProductCategory category = categoryRepository.find(id); //category does not exist if (category == null) { throw new ArgumentException(ERROR_CATEGORY_NOT_FOUND_ID); } category = categoryRepository.remove(category); return(ProductCategoryModelViewService.fromEntity(category)); }
/// <summary> /// Retrieves all instances of ProductCategory that are subcategories of the ProductCategory with the given identifier. /// </summary> /// <param name="parentId">Parent ProductCategory's database identifier.</param> /// <returns>Returns a list with ModelViews of all the instances of ProductCategory that are subcategories of the given ProductCategory.</returns> public GetAllProductCategoriesModelView findAllSubCategories(long parentId) { ProductCategoryRepository repository = PersistenceContext.repositories().createProductCategoryRepository(); ProductCategory parentCategory = repository.find(parentId); if (parentCategory == null) { throw new ArgumentException(ERROR_PARENT_NOT_FOUND); } IEnumerable <ProductCategory> subCategories = repository.findSubCategories(parentCategory); //check if any categories have been added if (!subCategories.Any()) { throw new ArgumentException(ERROR_NO_CATEGORIES_FOUND); } return(ProductCategoryModelViewService.fromCollection(subCategories)); }
/// <summary> /// Updates a ProductCategory with a given database identifier with the data in given ModelView. /// </summary> /// <param name="id">ProductCategory's database identifier.</param> /// <param name="modelView">ModelView containing the data being updated.</param> /// <returns>A ModelView with the updated ProductCategory data.</returns> public GetProductCategoryModelView updateProductCategory(long id, UpdateProductCategoryModelView modelView) { ProductCategoryRepository repository = PersistenceContext.repositories().createProductCategoryRepository(); ProductCategory category = repository.find(id); string newName = modelView.name; if (!category.changeName(newName)) { throw new ArgumentException(ERROR_INVALID_NAME); } category = repository.update(category); if (category == null) { throw new ArgumentException(ERROR_DUPLICATE_NAME); } return(ProductCategoryModelViewService.fromEntity(category)); }