public void Update(List <InspectionViewModel> newInspection) { var inspections = _inspectionRepository.GetAll().Where(x => x.ObjectI.Equals(newInspection[0].ObjectI)); var maxLicenseCode = 0; if (inspections.Any()) { maxLicenseCode = Convert.ToInt32(inspections.Max(x => x.InspectionC)); } //xoa foreach (var inspection in inspections) { if (newInspection.Any(item => item.InspectionC == inspection.InspectionC && item.ObjectI == inspection.ObjectI) == false) { _inspectionRepository.Delete(inspection); } } //update foreach (var item in newInspection) { if (item.InspectionC == -1) { continue; } if (item.InspectionC == 0) { var addItem = new Inspection_M() { DisplayLineNo = item.DisplayLineNo, InspectionC = ++maxLicenseCode, InspectionN = item.InspectionN, NoticeNo = item.NoticeNo, ObjectI = item.ObjectI }; _inspectionRepository.Add(addItem); } else { var updateInspection = inspections.FirstOrDefault(x => x.InspectionC == item.InspectionC && x.ObjectI == item.ObjectI); if (updateInspection != null) { updateInspection.DisplayLineNo = item.DisplayLineNo; updateInspection.InspectionN = item.InspectionN; updateInspection.NoticeNo = item.NoticeNo; _inspectionRepository.Update(updateInspection); } } } SaveInspection(); }
public async Task <IActionResult> Post(Inspection inspection) { try { var response = await _repository.Add(inspection); if (response != 0) { return(Ok("Added successfully")); } else { return(BadRequest("An error ocurred, contact IT Staff")); } }catch (Exception e) { //Log error Log.Error(e.Message); Log.Error(e.StackTrace); return(BadRequest("An error ocurred, contact IT Staff")); } }