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)); } }
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)); } }
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)); } }