public IActionResult GetTransportTypeById([FromRoute] int id) { var transportType = transportTypeRepository.GetTransportTypeById(id); return(new OkObjectResult(new { status = "OK", content = new TransportTypeDto { Name = transportType.Name } })); }
public IActionResult CreateNewTransport([FromBody] TransportBodyDto bodyTransport) { if (bodyTransport.MinimalWeight >= bodyTransport.MaximalWeight) { return(new BadRequestObjectResult(new { status = "Minimal weight cannot be greater or equal with minimal weight", content = (string)null })); } var transportType = transportTypeRepository.GetTransportTypeById(bodyTransport.TransportTypeId); if (transportType == null) { return(new BadRequestObjectResult(new { status = "Transport type sent in body doesn't exist", content = (string)null })); } var allTransports = transportRepository.GetTransportsByTransportType(transportType.Id); if (allTransports.FirstOrDefault(t => t.MinimalWeight <= bodyTransport.MinimalWeight && bodyTransport.MinimalWeight <= t.MaximalWeight) is not null) { return(new BadRequestObjectResult(new { status = "Minimal weight you are trying to set is part of existing range", content = (string)null })); } if (allTransports.FirstOrDefault(t => t.MinimalWeight <= bodyTransport.MaximalWeight && bodyTransport.MaximalWeight <= t.MaximalWeight) is not null) { return(new BadRequestObjectResult(new { status = "Maximal weight you are trying to set is part of existing range", content = (string)null })); } double maxWeight = allTransports.Max(t => t.MaximalWeight); var greaterRanges = allTransports.FirstOrDefault(t => t.MaximalWeight >= bodyTransport.MaximalWeight); if (greaterRanges == null && bodyTransport.MinimalWeight != maxWeight + 1) { return(new BadRequestObjectResult(new { status = "Minimal weight of new record must be the same as biggest maximum valule", content = (string)null })); } var newTransport = new Transport() { MaximalWeight = bodyTransport.MaximalWeight, MinimalWeight = bodyTransport.MinimalWeight, Price = bodyTransport.Price, TransportTypeId = transportType.Id, TransportType = transportType }; try { transportRepository.CreateNewTransport(newTransport); logger.Log(LogLevel.Information, contextAccessor.HttpContext.TraceIdentifier, "", "[CreateNewTransport]Successfully created transport with price " + bodyTransport.Price, null); } catch (Exception ex) { logger.Log(LogLevel.Information, contextAccessor.HttpContext.TraceIdentifier, "", "[CreateNewTransport]Creating new transport not successful", ex); return(new BadRequestObjectResult(new { status = "Saving in database not successful", content = (string)null })); } return(new StatusCodeResult(201)); }