private static string ValidateLoadCarrierGroup(LoadCarrierGroupDetailData group) { var errors = new StringBuilder(); if (group.CustomerId == Guid.Empty) { errors.AppendLine("Must have a Customer"); } if (string.IsNullOrEmpty(group.GroupName)) { errors.AppendLine("Must have a name"); } var validOrigin = ValidateLocation(group.OriginCity, group.OriginState, group.OriginCountry); var validDest = ValidateLocation(group.DestinationCity, group.DestinationState, group.DestinationCountry); var validEquipment = (group.LoadCarrierGroupEquipment?.Any()).GetValueOrDefault(); if (!validOrigin && !validDest && !validEquipment) { errors.AppendLine("Must have an Origin, Destination, or Equipment"); } if (errors.Length > 0) { return(errors.ToString()); } return(string.Empty); }
public IActionResult Put([FromBody] LoadCarrierGroupDetailData group) { try { var response = _loadCarrierGroupService.UpdateLoadCarrierGroup(group, _userContext.UserName); if (response.IsSuccess) { return(Success(response.LoadCarrierGroupData)); } else { var problemDetails = new ValidationProblemDetails(response.ModelState) { Title = "Error updating LoadCarrierGroup", Detail = "One or more errors occurred when updating the LoadCarrierGroup. See form for error details", Status = (int)HttpStatusCode.BadRequest, Instance = $"urn:kbxl:error:{Guid.NewGuid()}" }; return(BadRequest(problemDetails)); } } catch (UnauthorizedAccessException ex) { return(Forbidden <LoadCarrierGroupDetailData>(ex)); } }
private void ConvertStatesToAbbreviations(LoadCarrierGroupDetailData loadCarrierGroupData) { if (loadCarrierGroupData != null) { loadCarrierGroupData.OriginState = ConvertStateToAbbreviation(loadCarrierGroupData.OriginState); loadCarrierGroupData.DestinationState = ConvertStateToAbbreviation(loadCarrierGroupData.DestinationState); } }
private static string GetCarrierGroupUniqueConstraintErrorMessage(LoadCarrierGroupDetailData group) { var msg = new StringBuilder("A carrier group 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.LoadCarrierGroupEquipment.Any()) { msg.Append($"Equipment Type(s) - {string.Join(", ", group.LoadCarrierGroupEquipment.Select(lcge => lcge.EquipmentId))}{Environment.NewLine}"); } return(msg.ToString()); }
public SaveLoadCarrierGroupResponse CreateLoadCarrierGroup(LoadCarrierGroupDetailData group, string username) { var response = new SaveLoadCarrierGroupResponse(); try { _securityService.GuardAction(SecurityActions.Loadshop_Ui_System_Shipper_Carrier_Groups_Add_Edit); ConvertStatesToAbbreviations(group); if (group.LoadCarrierGroupId > 0) { response.ModelState.AddModelError($"urn:LoadCarrierGroup", "LoadCarrierGroup should not have an LoadCarrierGroupId assigned when creating."); return(response); } var validationErrorMessage = ValidateLoadCarrierGroup(group); if (!string.IsNullOrWhiteSpace(validationErrorMessage)) { response.ModelState.AddModelError($"urn:LoadCarrierGroup", validationErrorMessage); return(response); } ValidateLoadCarrierGroupCarriers(group.Carriers, group.LoadCarrierGroupId); var dbGroup = _mapper.Map <LoadCarrierGroupEntity>(group); //Map Equipment Types dbGroup.LoadCarrierGroupEquipment = new List <LoadCarrierGroupEquipmentEntity>(); dbGroup.LoadCarrierGroupEquipment.MapList( group.LoadCarrierGroupEquipment, lcgeEntity => lcgeEntity.LoadCarrierGroupEquipmentId, lcgeData => lcgeData.LoadCarrierGroupEquipmentId, _mapper); dbGroup.LoadCarrierGroupCarriers = new List <LoadCarrierGroupCarrierEntity>(); dbGroup.LoadCarrierGroupCarriers.MapList( group.Carriers, lcgcEntity => lcgcEntity.LoadCarrierGroupCarrierId, legcData => legcData.LoadCarrierGroupCarrierId, _mapper); GuardCustomer(dbGroup.CustomerId); if (IsUnique(dbGroup)) { _context.LoadCarrierGroups.Add(dbGroup); //_context.LoadCarrierGroupEquipment.AddRange(dbGroup.LoadCarrierGroupEquipment); _context.SaveChanges(username); response.LoadCarrierGroupData = GetLoadCarrierGroup(dbGroup.LoadCarrierGroupId); } else { response.ModelState.AddModelError($"urn:LoadCarrierGroup", GetCarrierGroupUniqueConstraintErrorMessage(group)); } } catch (DbUpdateException ex) { //TODO: Improve to use sql database conditions if (ex.InnerException != null && ex.InnerException.Message.Contains("Violation of UNIQUE KEY constraint")) { response.ModelState.AddModelError($"urn:LoadCarrierGroup", GetCarrierGroupUniqueConstraintErrorMessage(group)); return(response); } throw; } return(response); }