Пример #1
0
        public IActionResult CreateMaterial([FromBody] MaterialCreationRequest materialCreationRequest)
        {
            if (materialCreationRequest == null ||
                string.IsNullOrWhiteSpace(materialCreationRequest.Name) ||
                string.IsNullOrWhiteSpace(materialCreationRequest.Manufacturer) ||
                string.IsNullOrWhiteSpace(materialCreationRequest.ManufacturerId) ||
                string.IsNullOrWhiteSpace(materialCreationRequest.Type))
            {
                return(HandleBadRequest("Incomplete or invalid material data submitted for creation."));
            }

            // Check for material type validity
            Plastic plastic = null;

            try
            {
                plastic = PlasticsService.GetPlastic(materialCreationRequest.Type);
            }
            catch (PlasticNotFoundException exception)
            {
                return(HandleBadRequest(exception.Message));
            }

            // Proceed with creation
            try
            {
                Material material = MaterialsService.CreateMaterial(materialCreationRequest.Name, materialCreationRequest.Manufacturer, materialCreationRequest.ManufacturerId, plastic);
                return(Created(GetNewResourceUri(material.Id), material));
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MaterialsController"/> class.
 /// </summary>
 /// <param name="loggerFactory">A factory to create loggers from.</param>
 /// <param name="plasticsService">Grants access to known plastics.</param>
 /// <param name="materialsService">The materials service.</param>
 /// <param name="customMaterialPropService">Grants access to custom material props.</param>
 public MaterialsController(ILoggerFactory loggerFactory,
                            PlasticsService plasticsService,
                            MaterialsService materialsService,
                            CustomMaterialPropService customMaterialPropService)
 {
     Logger                    = loggerFactory.CreateLogger <MaterialsController>();
     PlasticsService           = plasticsService;
     MaterialsService          = materialsService;
     CustomMaterialPropService = customMaterialPropService;
 }
Пример #3
0
        public IActionResult GetMaterials(
            [FromQuery] bool getAll         = false,
            [FromQuery] int page            = 1,
            [FromQuery] int elementsPerPage = 10,
            [FromQuery] string search       = null,
            [FromQuery] string manufacturer = null,
            [FromQuery] string type         = null)
        {
            if (!getAll && (page < 1 || elementsPerPage < 1))
            {
                return(HandleBadRequest("Bad pagination parameters."));
            }

            // Check for material type validity
            Plastic plastic = null;

            if (type != null)
            {
                try
                {
                    plastic = PlasticsService.GetPlastic(type);
                }
                catch (PlasticNotFoundException exception)
                {
                    return(HandleBadRequest(exception.Message));
                }
            }

            // Get, filter and paginate materials
            try
            {
                IEnumerable <Material> materials          = MaterialsService.GetMaterials(search, manufacturer, plastic);
                IEnumerable <Material> paginatedMaterials = materials;
                if (!getAll)
                {
                    paginatedMaterials = materials.Skip((page - 1) * elementsPerPage).Take(elementsPerPage);
                }
                return(Ok(new PaginatedResponse(paginatedMaterials, materials.Count())));
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }
Пример #4
0
        public IActionResult UpdateMaterialMasterData(int id, [FromBody] MaterialUpdateRequest materialUpdateRequest)
        {
            if (materialUpdateRequest == null ||
                string.IsNullOrWhiteSpace(materialUpdateRequest.Name) ||
                string.IsNullOrWhiteSpace(materialUpdateRequest.Manufacturer) ||
                string.IsNullOrWhiteSpace(materialUpdateRequest.ManufacturerId) ||
                string.IsNullOrWhiteSpace(materialUpdateRequest.Type))
            {
                return(HandleBadRequest("Incomplete or invalid material data submitted for update."));
            }

            // Check for material type validity
            Plastic plastic = null;

            try
            {
                plastic = PlasticsService.GetPlastic(materialUpdateRequest.Type);
            }
            catch (PlasticNotFoundException exception)
            {
                return(HandleBadRequest(exception.Message));
            }

            // Proceed with updating
            try
            {
                Material material = MaterialsService.UpdateMaterial(id, materialUpdateRequest.Name, materialUpdateRequest.Manufacturer, materialUpdateRequest.ManufacturerId, plastic);
                return(Ok(material));
            }
            catch (MaterialNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PlasticsController"/> class.
 /// </summary>
 /// <param name="loggerFactory">A factory to create loggers from.</param>
 public PlasticsController(ILoggerFactory loggerFactory, PlasticsService plasticsService)
 {
     Logger          = loggerFactory.CreateLogger <PlasticsController>();
     PlasticsService = plasticsService;
 }