public async Task<ResponseViewModel> AddNewVehicleFitnessReportRecord(VehicleFitnessReportViewModel vm, string userName) { var response = new ResponseViewModel(); try { var user = _userService.GetUserByUsername(userName); var vfr = vm.ToModel(); vfr.UpdatedBy = user.Id; vfr.CreatedBy = user.Id; _db.VehicleFitnessReports.Add(vfr); await _db.SaveChangesAsync(); response.IsSuccess = true; response.Message = "New Vehicle Fitness Report has been added."; } catch (Exception ex) { response.IsSuccess = false; response.Message = "Operation failed.Please try again."; } return response; }
public async Task <VehicleResponseViewModel> SaveVehicleFitnessReport(VehicleFitnessReportViewModel vm, string userName) { var response = new VehicleResponseViewModel(); try { var user = _userService.GetUserByUsername(userName); var model = _db.VehicleFitnessReports.FirstOrDefault(x => x.Id == vm.Id); if (model == null) { model = vm.ToModel(); model.CreatedBy = user.Id; model.UpdatedBy = user.Id; _db.VehicleFitnessReports.Add(model); response.Message = "New Record has been added."; } else { model.FitnessReportDate = new DateTime(vm.FitnessReportYear, vm.FitnessReportMonth, vm.FitnessReportDay, 0, 0, 0); model.ValidTill = new DateTime(vm.ValidTillYear, vm.ValidTillMonth, vm.ValidTillDay, 0, 0, 0); model.UpdatedBy = user.Id; model.UpdatedOn = DateTime.UtcNow; model.Note = vm.Note; _db.VehicleFitnessReports.Update(model); response.Message = "Record has been updated."; } await _db.SaveChangesAsync(); response.IsSuccess = true; } catch (Exception ex) { response.IsSuccess = false; response.Message = "Operation failed.Please try again."; } return(response); }