public async Task <IActionResult> CreateInstruction([FromBody] SpecialInstructionData instruction)
        {
            try
            {
                var response = await specialInstructionsService.CreateSpecialInstructionAsync(instruction, userContext.UserName);

                if (response.IsSuccess)
                {
                    return(Success(response.SpecialInstructionData));
                }
                else
                {
                    var problemDetails = new ValidationProblemDetails(response.ModelState)
                    {
                        Title    = "Error creating Special Instructions",
                        Detail   = "One or more errors occurred when creating the Special Instruction.  See form for error details",
                        Status   = (int)HttpStatusCode.BadRequest,
                        Instance = $"urn:kbxl:error:{Guid.NewGuid()}"
                    };
                    return(BadRequest(problemDetails));
                }
            }
            catch (UnauthorizedAccessException ex)
            {
                return(Forbidden <LoadCarrierGroupData>(ex));
            }
        }
Пример #2
0
        private static string ValidateSpecialInstruction(SpecialInstructionData instructionData)
        {
            var errors = new StringBuilder();

            if (instructionData.CustomerId == Guid.Empty)
            {
                errors.AppendLine("Must have a Customer");
            }

            if (string.IsNullOrWhiteSpace(instructionData.Name))
            {
                errors.AppendLine("Must have a name");
            }
            var validOrigin    = ValidateLocation(instructionData.OriginCity, instructionData.OriginState, instructionData.OriginCountry);
            var validDest      = ValidateLocation(instructionData.DestinationCity, instructionData.DestinationState, instructionData.DestinationCountry);
            var validEquipment = (instructionData.SpecialInstructionEquipment?.Any()).GetValueOrDefault();

            if (!validOrigin && !validDest && !validEquipment)
            {
                errors.AppendLine("Must have an Origin, Destination, or Equipment");
            }

            if (string.IsNullOrWhiteSpace(instructionData.Comments) ||
                instructionData.Comments.Equals("<p></p>")) // default text for quill editor
            {
                errors.AppendLine("Must have an instruction");
            }

            if (errors.Length > 0)
            {
                return(errors.ToString());
            }
            return(string.Empty);
        }
Пример #3
0
 private void ConvertStatesToAbbreviations(SpecialInstructionData instructionData)
 {
     if (instructionData != null)
     {
         instructionData.OriginState      = ConvertStateToAbbreviation(instructionData.OriginState);
         instructionData.DestinationState = ConvertStateToAbbreviation(instructionData.DestinationState);
     }
 }
Пример #4
0
        private static string GetSpecialInstructionsDupErrorMessage(SpecialInstructionData group)
        {
            var msg = new StringBuilder("A special instruction already exists for:" + Environment.NewLine);

            if (!string.IsNullOrWhiteSpace(group.OriginAddress1))
            {
                msg.Append($"Origin Address1 - {group.OriginAddress1}{Environment.NewLine}");
            }
            if (!string.IsNullOrWhiteSpace(group.OriginCity))
            {
                msg.Append($"Origin City - {group.OriginCity}{Environment.NewLine}");
            }
            if (!string.IsNullOrWhiteSpace(group.OriginState))
            {
                msg.Append($"Origin State - {group.OriginState}{Environment.NewLine}");
            }
            if (!string.IsNullOrWhiteSpace(group.OriginPostalCode))
            {
                msg.Append($"Origin Postal Code - {group.OriginPostalCode}{Environment.NewLine}");
            }
            if (!string.IsNullOrWhiteSpace(group.OriginCountry))
            {
                msg.Append($"Origin Country - {group.OriginCountry}{Environment.NewLine}");
            }

            if (!string.IsNullOrWhiteSpace(group.DestinationAddress1))
            {
                msg.Append($"Destination Address1 - {group.DestinationAddress1}{Environment.NewLine}");
            }
            if (!string.IsNullOrWhiteSpace(group.DestinationCity))
            {
                msg.Append($"Destination City - {group.DestinationCity}{Environment.NewLine}");
            }
            if (!string.IsNullOrWhiteSpace(group.DestinationState))
            {
                msg.Append($"Destination State - {group.DestinationState}{Environment.NewLine}");
            }
            if (!string.IsNullOrWhiteSpace(group.DestinationPostalCode))
            {
                msg.Append($"Destination Postal Code - {group.DestinationPostalCode}{Environment.NewLine}");
            }
            if (!string.IsNullOrWhiteSpace(group.DestinationCountry))
            {
                msg.Append($"Destination Country - {group.DestinationCountry}{Environment.NewLine}");
            }

            if (group.SpecialInstructionEquipment.Any())
            {
                msg.Append($"Equipment Type(s) - {string.Join(", ", group.SpecialInstructionEquipment.Select(lcge => lcge.EquipmentId))}{Environment.NewLine}");
            }
            return(msg.ToString());
        }
Пример #5
0
        public async Task <SaveSpecialInstructionResponse> CreateSpecialInstructionAsync(SpecialInstructionData instruction, string username)
        {
            var response = new SaveSpecialInstructionResponse();

            if (instruction == null)
            {
                response.ModelState.AddModelError($"{ErrorPrefix}", "Special Instruction is requred");
                return(response);
            }

            ConvertStatesToAbbreviations(instruction);
            if (instruction.SpecialInstructionId > 0)
            {
                response.ModelState.AddModelError($"{ErrorPrefix}", "Special Instruction should not have an Id assigned when creating.");
                return(response);
            }

            var validationErrorMessage = ValidateSpecialInstruction(instruction);

            if (!string.IsNullOrWhiteSpace(validationErrorMessage))
            {
                response.ModelState.AddModelError($"{ErrorPrefix}", validationErrorMessage);
                return(response);
            }

            instruction.Comments = _htmlSanitizer.Sanitize(instruction.Comments);

            var dbGroup = _mapper.Map <SpecialInstructionEntity>(instruction);

            //Map Equipment Types
            dbGroup.SpecialInstructionEquipment = new List <SpecialInstructionEquipmentEntity>();
            dbGroup.SpecialInstructionEquipment.MapList(instruction.SpecialInstructionEquipment, lcgeEntity => lcgeEntity.SpecialInstructionEquipmentId, lcgeData => lcgeData.SpecialInstructionEquipmentId, _mapper);

            GuardCustomer(dbGroup.CustomerId);

            var dup = await CheckIfDuplicateExists(dbGroup);

            if (dup)
            {
                response.ModelState.AddModelError($"{ErrorPrefix}", GetSpecialInstructionsDupErrorMessage(instruction));
                return(response);
            }

            _context.SpecialInstructions.Add(dbGroup);
            await _context.SaveChangesAsync(username);

            response.SpecialInstructionData = await GetSpecialInstructionAsync(dbGroup.SpecialInstructionId);

            return(response);
        }
Пример #6
0
        public async Task <SaveSpecialInstructionResponse> UpdateSpecialInstructionAsync(SpecialInstructionData instruction, string username)
        {
            var response = new SaveSpecialInstructionResponse();

            ConvertStatesToAbbreviations(instruction);

            if (instruction == null || instruction.SpecialInstructionId <= 0)
            {
                response.ModelState.AddModelError($"{ErrorPrefix}", "Special Instruction should have an Id assigned when updating.");
                return(response);
            }

            var validationErrorMessage = ValidateSpecialInstruction(instruction);

            if (!string.IsNullOrWhiteSpace(validationErrorMessage))
            {
                response.ModelState.AddModelError($"{ErrorPrefix}", validationErrorMessage);
                return(response);
            }

            var dbGroup = await _context.SpecialInstructions
                          .Include(x => x.SpecialInstructionEquipment)
                          .Where(x => x.SpecialInstructionId == instruction.SpecialInstructionId)
                          .SingleOrDefaultAsync();

            if (dbGroup == null)
            {
                response.ModelState.AddModelError($"{ErrorPrefix}", "Special Instruction not found");
                return(response);
            }

            GuardCustomer(dbGroup.CustomerId);

            instruction.Comments = _htmlSanitizer.Sanitize(instruction.Comments);

            _mapper.Map(instruction, dbGroup);

            //Update Load Carrier Group Equipment Entity
            if (dbGroup.SpecialInstructionEquipment != null)
            {
                dbGroup.SpecialInstructionEquipment.MapList(
                    instruction.SpecialInstructionEquipment,
                    lcgeEntity => lcgeEntity.SpecialInstructionEquipmentId,
                    legeData => legeData.SpecialInstructionEquipmentId,
                    _mapper);
            }

            var dup = await CheckIfDuplicateExists(dbGroup);

            if (dup)
            {
                response.ModelState.AddModelError($"{ErrorPrefix}", GetSpecialInstructionsDupErrorMessage(instruction));
                return(response);
            }

            await _context.SaveChangesAsync(username);

            response.SpecialInstructionData = await GetSpecialInstructionAsync(dbGroup.SpecialInstructionId);

            return(response);
        }