public IActionResult Save([FromForm] LaboratoryGetRequesetModel model) { var res = _labService.SaveLaboratory(model); return(Ok(res)); }
public int SaveLaboratory(LaboratoryGetRequesetModel model) { LaboratoryDataRequesetModel resData = new LaboratoryDataRequesetModel(); if (model.Data != null) { resData = model.GetLaboratory(); } else { throw new Exception("DATA IS NULL"); } if (model != null) { if (model.LabPhotoFileData != null) { if (resData.LaboratoryPhotoName != null) { string path = Path.Combine(Environment.CurrentDirectory, "images", resData.LaboratoryPhotoName); File.Delete(path); } resData.LaboratoryPhotoName = UploadImageHelper.UploadFile(model.LabPhotoFileData); } if (model.LabPhotoFileData != null) { if (resData.LocationPhotoName != null) { string path = Path.Combine(Environment.CurrentDirectory, "images", resData.LocationPhotoName); File.Delete(path); } resData.LocationPhotoName = UploadImageHelper.UploadFile(model.LocationPhotoFileData); } } var labRes = new Laboratory { Id = resData.Id, Title = resData.Title, FieldOfStudy = resData.FieldOfStudy, Accreditation = resData.Accreditation, PositionLaboratory = resData.PositionLaboratory, Address = resData.Address, Office = resData.Office, LocationPhotoName = resData.LocationPhotoName, LaboratoryPhotoName = resData.LaboratoryPhotoName, DepartmentId = resData.DepartmentId, LaboratoryStatusId = resData.StatusId, DirectorEmployeeId = resData.DirectorEmployeeId }; Db.Laboratories.InsertOrUpdate(labRes); Db.Complete(); var emp = Db.Laboratories.GetEmployeeByLaboratoryId(labRes.Id); if (emp != null) { var empDel = emp.Where(e => !resData.EmployeeIds.Contains(e.EmployeeId)); Db.LaboratoryEmployees.RemoveRange(empDel); } if (resData.EmployeeIds != null && resData.EmployeeIds.Count() > 0) { var empIns = resData.EmployeeIds.Where(e => !emp.Select(em => em.EmployeeId).Contains(e)); var empRes = empIns.Select(c => new LaboratoryEmployee() { EmployeeId = c, LaboratoryId = labRes.Id }); Db.LaboratoryEmployees.InsertOrUpdateRange(empRes); } var eq = Db.Laboratories.GetEqiupmentByLaboratoryId(labRes.Id); if (eq != null) { var eqDel = eq.Where(e => !resData.EqiupmentIds.Contains(e.EquipmentId)); Db.LaboratoryEqiupments.RemoveRange(eqDel); } if (resData.EqiupmentIds != null && resData.EqiupmentIds.Count() > 0) { var eqIns = resData.EqiupmentIds.Where(e => !eq.Select(em => em.EquipmentId).Contains(e)); var eqiupRes = eqIns.Select(c => new LaboratoryEqiupment() { EquipmentId = c, LaboratoryId = labRes.Id }); Db.LaboratoryEqiupments.InsertOrUpdateRange(eqiupRes); } var srv = Db.Laboratories.GetServiceByLaboratoryId(labRes.Id); if (srv != null) { var srvDel = srv.Where(s => !resData.EmployeeIds.Contains(s.Id)); Db.LaboratoryServices.RemoveRange(srvDel); } if (resData.Services != null && resData.Services.Count() > 0) { var serviceRes = resData.Services.Select(c => new EFData.Entity.LaboratoryService() { Id = c.Id, Laboratory = labRes, Title = c.Title }); Db.LaboratoryServices.InsertOrUpdateRange(serviceRes); } var lp = Db.Laboratories.GetProjectByLaboratoryId(labRes.Id); if (lp != null) { var lpDel = lp.Where(e => !resData.ProjectIds.Contains(e.ProjectId)); Db.LaboratoryProjects.RemoveRange(lpDel); } var lpIns = resData.ProjectIds.Where(e => !lp.Select(q => q.ProjectId).Contains(e)); if (resData.ProjectIds != null && resData.ProjectIds.Count() > 0) { var lpRes = lpIns.Select(c => new LaboratoryProject() { ProjectId = c, LaboratoryId = labRes.Id }); Db.LaboratoryProjects.InsertOrUpdateRange(lpRes); } Db.Complete(); return(labRes.Id); }