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); }
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); }