Beispiel #1
0
        public IActionResult DeleteTransport([FromQuery] int transportId)
        {
            var allTransports = transportRepository.GetAllTransports();
            var transport     = allTransports.FirstOrDefault(transport => transport.Id == transportId);

            if (transport is null)
            {
                return(new BadRequestObjectResult(new { status = "Bad transport id provided", content = (string)null }));
            }
            var transportsWithParticularType = allTransports.Where(t => t.TransportTypeId == transport.TransportTypeId);

            var nextTransport = transportsWithParticularType.FirstOrDefault(t => t.MinimalWeight == transport.MaximalWeight + 1);

            if (nextTransport is not null)
            {
                return(new BadRequestObjectResult(new { status = "Deleting of the range with the biggest values is only allowed", content = (string)null }));
            }

            try
            {
                transportRepository.RemoveTransport(transport);
                logger.Log(LogLevel.Information, contextAccessor.HttpContext.TraceIdentifier, "", "[DeleteTransport]Successfully deleted transport with id " + transport.Id, null);
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Information, contextAccessor.HttpContext.TraceIdentifier, "", "[DeleteTransport] Deletion for transport with id " + transport.Id + " not successful", ex);
                return(new BadRequestObjectResult(new { status = "Saving in dabase not successful", content = (string)null }));
            }

            return(new OkObjectResult(new { status = "Transport successfully deleted", content = (string)null }));
        }