/// <summary>
 /// Finds all products in the repository.
 /// </summary>
 /// <returns>HTTP Response 404 Not Found if no products are found;
 /// HTTP Response 200 Ok with the info of all products in JSON format </returns>
 private ActionResult findAll()
 {
     try {
         GetAllProductsModelView allProductsModelView = new core.application.ProductController().findAllProducts();
         return(Ok(allProductsModelView));
     } catch (ResourceNotFoundException e) {
         return(NotFound(new SimpleJSONMessageService(e.Message)));
     } catch (Exception) {
         return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
     }
 }
 public ActionResult findProductComponents(long productId, [FromQuery] FindComponentsOptions groupBy)
 {
     try {
         FindComponentsModelView findComponentsModel = new FindComponentsModelView();
         findComponentsModel.fatherProductId = productId;
         findComponentsModel.option          = groupBy;
         GetAllComponentsModelView allComponentsByCategory = new core.application.ProductController().findProductComponents(findComponentsModel);
         return(Ok(allComponentsByCategory));
     } catch (ResourceNotFoundException e) {
         return(NotFound(new SimpleJSONMessageService(e.Message)));
     } catch (Exception) {
         return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
     }
 }
 /// <summary>
 /// Finds a Product with a matching reference.
 /// </summary>
 /// <param name="fetchProductDTO"></param>
 /// <returns></returns>
 private ActionResult findByReference(FetchProductDTO fetchProductDTO)
 {
     try {
         GetProductModelView getProductModelView = new core.application.ProductController().findProduct(fetchProductDTO);
         return(Ok(getProductModelView));
     } catch (ResourceNotFoundException e) {
         return(NotFound(new SimpleJSONMessageService(e.Message)));
     } catch (ArgumentException e) {
         //this exception may occur if the specified unit does not exist
         return(BadRequest(new SimpleJSONMessageService(e.Message)));
     } catch (Exception) {
         return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
     }
 }
        public ActionResult findMeasurementRestrictions(long productId, long measurementId)
        {
            FindMeasurementModelView productMeasurementMV = new FindMeasurementModelView();

            productMeasurementMV.productId     = productId;
            productMeasurementMV.measurementId = measurementId;
            try {
                GetAllRestrictionsModelView restrictionModelViews = new core.application.ProductController().findMeasurementRestrictions(productMeasurementMV);
                return(Ok(restrictionModelViews));
            } catch (ResourceNotFoundException e) {
                return(NotFound(new SimpleJSONMessageService(e.Message)));
            } catch (Exception) {
                return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
            }
        }
 public ActionResult findProductComponent(long productId, long componentId, [FromQuery] string unit)
 {
     try {
         FindComponentModelView findComponentModelView = new FindComponentModelView();
         findComponentModelView.fatherProductId = productId;
         findComponentModelView.childProductId  = componentId;
         findComponentModelView.unit            = unit;
         GetComponentModelView componentModelView = new core.application.ProductController().findProductComponent(findComponentModelView);
         return(Ok(componentModelView));
     } catch (ResourceNotFoundException e) {
         return(NotFound(new SimpleJSONMessageService(e.Message)));
     } catch (Exception) {
         return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
     }
 }
        public ActionResult findProductMaterials(long productId, [FromQuery] bool pricedMaterialsOnly)
        {
            FetchProductDTO fetchProductDTO = new FetchProductDTO();

            fetchProductDTO.id = productId;
            fetchProductDTO.pricedMaterialsOnly = pricedMaterialsOnly;
            try {
                GetAllMaterialsModelView allMaterialsModelView = new core.application.ProductController().findProductMaterials(fetchProductDTO);
                return(Ok(allMaterialsModelView));
            } catch (ResourceNotFoundException e) {
                return(NotFound(new SimpleJSONMessageService(e.Message)));
            } catch (Exception) {
                return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
            }
        }
        public ActionResult addProduct([FromBody] AddProductModelView addProductMV)
        {
            if (addProductMV == null)
            {
                return(BadRequest(new SimpleJSONMessageService(INVALID_PRODUCT_DATA)));
            }

            try {
                GetProductModelView createdProductMV = new core.application.ProductController().addProduct(addProductMV);
                return(CreatedAtRoute("GetProduct", new { id = createdProductMV.productId }, createdProductMV));
            } catch (ArgumentException e) {
                return(BadRequest(new SimpleJSONMessageService(e.Message)));
            } catch (Exception) {
                return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
            }
        }
        public ActionResult findComponentRestrictions(long parentProductId, long complementaryProductId)
        {
            FindComponentModelView componentModelView = new FindComponentModelView();

            componentModelView.fatherProductId = parentProductId;
            componentModelView.childProductId  = complementaryProductId;

            try {
                GetAllRestrictionsModelView restrictionModelViews = new core.application.ProductController().findComponentRestrictions(componentModelView);
                return(Ok(restrictionModelViews));
            } catch (ResourceNotFoundException e) {
                return(NotFound(new SimpleJSONMessageService(e.Message)));
            } catch (Exception) {
                return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
            }
        }
        public ActionResult findProductSlotWidths(long productId, [FromQuery] string unit)
        {
            FetchProductDTO fetchProductDTO = new FetchProductDTO();

            fetchProductDTO.id = productId;
            fetchProductDTO.productDTOOptions.requiredUnit = unit;
            try {
                GetProductSlotWidthsModelView productSlotWidthsMV = new core.application.ProductController().findProductSlotWidths(fetchProductDTO);
                return(Ok(productSlotWidthsMV));
            } catch (ResourceNotFoundException e) {
                return(NotFound(new SimpleJSONMessageService(e.Message)));
            } catch (InvalidOperationException e) {
                return(BadRequest(new SimpleJSONMessageService(e.Message)));
            } catch (Exception) {
                return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
            }
        }
        public ActionResult findById(long id, [FromQuery] string unit)
        {
            FetchProductDTO fetchProductDTO = new FetchProductDTO();

            fetchProductDTO.id = id;
            fetchProductDTO.productDTOOptions.requiredUnit = unit;
            try {
                GetProductModelView getProductModelView = new core.application.ProductController().findProduct(fetchProductDTO);
                return(Ok(getProductModelView));
            } catch (ResourceNotFoundException e) {
                return(NotFound(new SimpleJSONMessageService(e.Message)));
            } catch (ArgumentException e) {
                //this exception may occur if the specified unit does not exist
                return(BadRequest(new SimpleJSONMessageService(e.Message)));
            } catch (Exception) {
                return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
            }
        }
        public ActionResult updateProductProperties(long id, [FromBody] UpdateProductPropertiesModelView updateProductPropertiesModelView)
        {
            if (updateProductPropertiesModelView == null)
            {
                return(BadRequest(new SimpleJSONMessageService(INVALID_PRODUCT_DATA)));
            }

            updateProductPropertiesModelView.id = id;
            try {
                GetProductModelView updatedProductMV = new core.application.ProductController().updateProductProperties(updateProductPropertiesModelView);
                return(Ok(updatedProductMV));
            } 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)));
            }
        }
        public ActionResult addMaterialToProduct(long id, [FromBody] AddProductMaterialModelView addMaterialToProductMV)
        {
            if (addMaterialToProductMV == null)
            {
                return(BadRequest(new SimpleJSONMessageService(INVALID_MATERIAL_DATA)));
            }

            addMaterialToProductMV.productId = id;
            try {
                GetProductModelView productModelView = new core.application.ProductController().addMaterialToProduct(addMaterialToProductMV);
                return(Created(Request.Path, productModelView));
            } 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)));
            }
        }
        public ActionResult addMeasurementToProduct(long productId, [FromBody] AddMeasurementModelView measurementModelView)
        {
            if (measurementModelView == null)
            {
                return(BadRequest(new SimpleJSONMessageService(INVALID_MEASUREMENT_DATA)));
            }

            measurementModelView.productId = productId;

            try {
                GetProductModelView productModelView = new core.application.ProductController().addMeasurementToProduct(measurementModelView);
                return(Created(Request.Path, productModelView));
            } 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)));
            }
        }
        public ActionResult findProductMeasurements(long productId, [FromQuery] string unit)
        {
            FetchProductDTO fetchProductDTO = new FetchProductDTO()
            {
                id = productId
            };

            fetchProductDTO.productDTOOptions.requiredUnit = unit;
            try {
                GetAllMeasurementsModelView allMeasurementsModelView =
                    new core.application.ProductController().findProductMeasurements(fetchProductDTO);
                return(Ok(allMeasurementsModelView));
            } catch (ArgumentException e) {
                return(BadRequest(new SimpleJSONMessageService(e.Message)));
            } catch (ResourceNotFoundException e) {
                return(NotFound(new SimpleJSONMessageService(e.Message)));
            } catch (Exception) {
                return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
            }
        }
        public ActionResult addRestrictionToProductComponent(long productID, long componentID, [FromBody] AddRestrictionModelView restrictionMV)
        {
            if (restrictionMV == null)
            {
                return(BadRequest(new SimpleJSONMessageService(INVALID_RESTRICTION_DATA)));
            }

            AddComponentRestrictionModelView addRestrictionToProductComponentDTO = new AddComponentRestrictionModelView();

            addRestrictionToProductComponentDTO.fatherProductId = productID;
            addRestrictionToProductComponentDTO.childProductId  = componentID;
            addRestrictionToProductComponentDTO.restriction     = restrictionMV;
            try {
                GetProductModelView appliedRestrictionModelView = new core.application.ProductController().addRestrictionToProductComponent(addRestrictionToProductComponentDTO);
                return(Created(Request.Path, appliedRestrictionModelView));
            } 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)));
            }
        }
        public ActionResult addRestrictionToProductMaterial(long productId, long materialId, [FromBody] AddRestrictionModelView restrictionMV)
        {
            if (restrictionMV == null)
            {
                return(BadRequest(new SimpleJSONMessageService(INVALID_RESTRICTION_DATA)));
            }

            AddProductMaterialRestrictionModelView addRestrictionToProductMaterialMV = new AddProductMaterialRestrictionModelView();

            addRestrictionToProductMaterialMV.productId   = productId;
            addRestrictionToProductMaterialMV.materialId  = materialId;
            addRestrictionToProductMaterialMV.restriction = restrictionMV;

            try {
                GetProductModelView productModelView = new core.application.ProductController().addRestrictionToProductMaterial(addRestrictionToProductMaterialMV);
                return(Created(Request.Path, productModelView));
            } 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)));
            }
        }