public async Task <IActionResult> AddVehicleAccidentAsync([FromBody] AddVehicleAccidentManagementAc addVehicleAccident) { var user = await _userManager.FindByNameAsync(User.Identity.Name); return(Ok(await _vehicleAccidentManagementRepository.AddVehicleAccidentAsync(addVehicleAccident, user))); }
/// <summary> /// Method to add vehicle accident - SS /// </summary> /// <param name="addVehicleAccident">vehicle accident</param> /// <param name="loggedInUser">logged in user</param> /// <returns>response</returns> public async Task <VehicleAccidentManagementResponse> AddVehicleAccidentAsync(AddVehicleAccidentManagementAc addVehicleAccident, ApplicationUser loggedInUser) { if (string.IsNullOrEmpty(addVehicleAccident.Code.Trim())) { return new VehicleAccidentManagementResponse() { HasError = true, Message = "Code can't be empty", ErrorType = VehicleAccidentManagementResponseType.Code } } ; else { var instituteId = await _instituteUserMappingHelperService.GetUserCurrentSelectedInstituteIdAsync(loggedInUser.Id, true); if (!await _iMSDbContext.VehicleMasters.AnyAsync(x => x.Id == addVehicleAccident.VehicleId && x.InstituteId == instituteId)) { return new VehicleAccidentManagementResponse() { HasError = true, Message = "Vehicle not found", ErrorType = VehicleAccidentManagementResponseType.VehicleId } } ; else { if (!await _iMSDbContext.DriverMasters.AnyAsync(x => x.Id == addVehicleAccident.DriverId && x.InstituteId == instituteId)) { return new VehicleAccidentManagementResponse() { HasError = true, Message = "Driver not found", ErrorType = VehicleAccidentManagementResponseType.DriverId } } ; else { if (await _iMSDbContext.VehicleAccidents.AnyAsync(x => x.Code.ToLowerInvariant() == addVehicleAccident.Code.ToLowerInvariant() && x.Vehicle.InstituteId == instituteId)) { return new VehicleAccidentManagementResponse() { HasError = true, Message = "Vehicle accident code already exist. Please use unique one", ErrorType = VehicleAccidentManagementResponseType.Code } } ; else { var accident = new VehicleAccident() { AccidentDate = addVehicleAccident.AccidentDate, Address = addVehicleAccident.Address, Code = addVehicleAccident.Code, CreatedOn = DateTime.UtcNow, DriverId = addVehicleAccident.DriverId, EstimateCost = addVehicleAccident.EstimateCost, UpdatedById = loggedInUser.Id, UpdatedOn = DateTime.UtcNow, VehicleId = addVehicleAccident.VehicleId }; _iMSDbContext.VehicleAccidents.Add(accident); await _iMSDbContext.SaveChangesAsync(); return(new VehicleAccidentManagementResponse() { HasError = false, Message = "Vehicle accident added successfully" }); } } } } }