コード例 #1
0
        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));
        }
コード例 #2
0
        public void CreateNewTransport_MinimalWeightIsPartOfExistingRange_BadRequest()
        {
            TransportBodyDto transportBody = new TransportBodyDto
            {
                MinimalWeight   = 200,
                MaximalWeight   = 1500,
                Price           = 2,
                TransportTypeId = 1
            };

            var response = (BadRequestObjectResult)transportController.CreateNewTransport(transportBody);
            var body     = JsonConvert.DeserializeObject <ResponseObject>(JsonConvert.SerializeObject(response.Value));

            Assert.AreEqual(body.Status, "Minimal weight you are trying to set is part of existing range");
        }
コード例 #3
0
        public void CreateNewTransport_MinimalWeightGreaterThanMaximal_BadRequest()
        {
            TransportBodyDto transportBody = new TransportBodyDto
            {
                MinimalWeight   = 15,
                MaximalWeight   = 2,
                Price           = 2,
                TransportTypeId = 1
            };

            var response = (BadRequestObjectResult)transportController.CreateNewTransport(transportBody);
            var body     = JsonConvert.DeserializeObject <ResponseObject>(JsonConvert.SerializeObject(response.Value));

            Assert.AreEqual(body.Status, "Minimal weight cannot be greater or equal with minimal weight");
        }
コード例 #4
0
        public void CreateNewTransport_MinimalWeightOfNewRecordIsGreaterThanMaximalWeight_BadRequest()
        {
            TransportBodyDto transportBody = new TransportBodyDto
            {
                MinimalWeight   = 3000,
                MaximalWeight   = 3005,
                Price           = 2,
                TransportTypeId = 1
            };

            var response = (BadRequestObjectResult)transportController.CreateNewTransport(transportBody);
            var body     = JsonConvert.DeserializeObject <ResponseObject>(JsonConvert.SerializeObject(response.Value));

            Assert.AreEqual(body.Status, "Minimal weight of new record must be the same as biggest maximum valule");
        }
コード例 #5
0
        public void UpdateTransportDetails_UpdateThroughMultipleRangesRight_BadRequest()
        {
            TransportBodyDto newTransport = new TransportBodyDto
            {
                Id              = 1,
                MinimalWeight   = 300,
                MaximalWeight   = 2000,
                Price           = 18,
                TransportTypeId = 1
            };

            var response = (BadRequestObjectResult)transportController.UpdateTransportDetails(4, newTransport);
            var body     = JsonConvert.DeserializeObject <ResponseObject>(JsonConvert.SerializeObject(response.Value));

            Assert.AreEqual("Updating through multiple ranges not possible", body.Status);
        }
コード例 #6
0
        public void CreateNewTransport_HappyScenario_OK()
        {
            TransportBodyDto transportBody = new TransportBodyDto
            {
                MinimalWeight   = 2001,
                MaximalWeight   = 3000,
                Price           = 20,
                TransportTypeId = 1
            };

            var response   = transportController.CreateNewTransport(transportBody);
            var statusCode = (HttpStatusCode)response
                             .GetType()
                             .GetProperty("StatusCode")
                             .GetValue(response, null);

            Assert.AreEqual(statusCode, HttpStatusCode.Created);
        }
コード例 #7
0
        public void UpdateTransportDetails_HappyScenario_OK()
        {
            TransportBodyDto newTransport = new TransportBodyDto
            {
                Id              = 1,
                MinimalWeight   = 300,
                MaximalWeight   = 2000,
                Price           = 18,
                TransportTypeId = 1
            };

            var response   = transportController.UpdateTransportDetails(3, newTransport);
            var statusCode = (HttpStatusCode)response
                             .GetType()
                             .GetProperty("StatusCode")
                             .GetValue(response, null);

            Assert.AreEqual(statusCode, HttpStatusCode.OK);
        }
コード例 #8
0
        public IActionResult UpdateTransportDetails([FromQuery] int transportId, [FromBody] TransportBodyDto newTrasport)
        {
            var transportType = transportTypeRepository.GetTransportTypeById(newTrasport.TransportTypeId);

            if (transportType == null)
            {
                return(new BadRequestObjectResult(new { status = "Transport type sent in body doesn't exist", content = (string)null }));
            }

            var       allTransports      = transportRepository.GetTransportsByTransportTypeOrderByMinimalWeight(transportType.Id);
            Transport transportForUpdate = null;
            var       availableTransportsExcludingTransportForUpdate = new List <Transport>();

            foreach (var transport in allTransports)
            {
                if (transport.Id == transportId)
                {
                    transportForUpdate = transport;
                }
                else
                {
                    availableTransportsExcludingTransportForUpdate.Add(transport);
                }
            }

            if (transportForUpdate == null)
            {
                return(new BadRequestObjectResult(new { status = "Bad transport id provided", content = (string)null }));
            }

            var previousTransport = availableTransportsExcludingTransportForUpdate.FirstOrDefault(t => t.MaximalWeight == transportForUpdate.MinimalWeight - 1);

            if (previousTransport is not null)
            {
                if (previousTransport.MinimalWeight <= newTrasport.MinimalWeight && newTrasport.MinimalWeight <= previousTransport.MaximalWeight || previousTransport.MaximalWeight <= newTrasport.MinimalWeight)
                {
                    previousTransport.MaximalWeight = newTrasport.MinimalWeight - 1;
                }
                else
                {
                    return(new BadRequestObjectResult(new { status = "Updating through multiple ranges not possible", content = (string)null }));
                }
            }
            else
            {
                newTrasport.MinimalWeight = 0;
            }

            var nextTransport = availableTransportsExcludingTransportForUpdate.FirstOrDefault(t => t.MinimalWeight == transportForUpdate.MaximalWeight + 1);

            if (nextTransport is not null)
            {
                if (nextTransport.MinimalWeight <= newTrasport.MaximalWeight && newTrasport.MaximalWeight <= nextTransport.MaximalWeight || newTrasport.MaximalWeight <= nextTransport.MinimalWeight)
                {
                    nextTransport.MinimalWeight = newTrasport.MaximalWeight + 1;
                }
                else
                {
                    if (newTrasport.MinimalWeight != 0 || newTrasport.MaximalWeight > nextTransport.MaximalWeight)
                    {
                        return(new BadRequestObjectResult(new { status = "Updating through multiple ranges not possible", content = (string)null }));
                    }
                }
            }


            transportForUpdate.Price           = newTrasport.Price;
            transportForUpdate.MinimalWeight   = newTrasport.MinimalWeight;
            transportForUpdate.MaximalWeight   = newTrasport.MaximalWeight;
            transportForUpdate.TransportType   = transportType;
            transportForUpdate.TransportTypeId = transportType.Id;

            try
            {
                if (previousTransport is not null)
                {
                    transportRepository.UpdateTransport(previousTransport);
                }
                if (nextTransport is not null)
                {
                    transportRepository.UpdateTransport(nextTransport);
                }
                transportRepository.UpdateTransport(transportForUpdate);

                logger.Log(LogLevel.Information, contextAccessor.HttpContext.TraceIdentifier, "", "[UpdateTransportDetails]Successfully updated transport with id " + newTrasport.Id, null);
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Information, contextAccessor.HttpContext.TraceIdentifier, "", "[UpdateTransportDetails] Updating transport with id " + newTrasport.Id + " not successful", ex);
                return(new BadRequestObjectResult(new { status = "Saving in database not successful", content = (string)null }));
            }

            return(new OkObjectResult(new { status = "Successfully updated", content = (string)null }));
        }