public void EditProject(DataModels.iteam_project header, List <DataModels.iteam_project_user> detail, ref string error) { using (var db = new DataContext()) { db.ExecuteTransaction(() => { var project_id = header?.project_id; var project_name = header?.project_name.Trim(); var project_des = header?.project_des; var end_project = header?.end_project; if (string.IsNullOrEmpty(project_name)) { throw new Exception("โปรดกรอกชื่อให้ถูกต้อง"); } var data_db = db.iteam_project.Where(x => x.project_id == project_id).FirstOrDefault(); if (data_db != null) { data_db.project_name = project_name; data_db.project_des = project_des; data_db.end_project = end_project; data_db.edit_dt = DateTime.Now; db.SaveChanges(); if (detail != null && detail.Count() > 0) { foreach (var x in detail) { x.project_id = header.project_id; } db.iteam_project_user.AddRange(detail); db.SaveChanges(); } } }, ref error); } }
public void DeleteProject(DataModels.iteam_project m, ref string error) { using (var db = new DataContext()) { db.ExecuteTransaction(() => { var project_id = m?.project_id; var project_name = m?.project_name; var tasksDB = db.iteam_task.Where(x => x.project_id == project_id).Select(x => x.task_id).ToList(); if (m != null) { db.iteam_project.RemoveRange(db.iteam_project.Where(x => x.project_id == project_id)); db.iteam_project_user.RemoveRange(db.iteam_project_user.Where(x => x.project_id == project_id)); db.iteam_task.RemoveRange(db.iteam_task.Where(x => x.project_id == project_id)); db.iteam_task_user.RemoveRange(db.iteam_task_user.Where(x => tasksDB.Contains(x.task_id))); db.iteam_comment.RemoveRange(db.iteam_comment.Where(x => tasksDB.Contains(x.task_id))); db.iteam_task_lists.RemoveRange(db.iteam_task_lists.Where(x => tasksDB.Contains(x.task_id))); db.SaveChanges(); var history_project = db.iteam_history_project.Where(x => x.project_id == project_id).ToList(); if (history_project != null) { foreach (var h in history_project) { h.active = "N"; h.project_name = project_name; } db.SaveChanges(); } } else { throw new Exception("Error"); } }, ref error); } }
public void NewProject(DataModels.iteam_project header, List <DataModels.iteam_project_user> detail, List <DataModels.iteam_history_project> history, ref string error) { using (var db = new DataContext()) { using (var transaction = db.Database.BeginTransaction()) { try { var project_name = header?.project_name.Trim(); var start_project = header?.start_project; var end_project = header?.end_project; if (string.IsNullOrEmpty(project_name)) { throw new Exception("กรุณากรอกชื่อโปรเจค"); } if (start_project == null || end_project == null) { throw new Exception("กรุณาเลือกวันเริ่มต้นหรือวันสิ้นสุดของโปรเจค"); } header.project_id = (db.iteam_project.Select(x => x.project_id).Max() ?? 0) + 1; header.start_project = start_project?.AddDays(1); header.end_project = end_project?.AddDays(1); header.add_dt = DateTime.Now; header.add_user = auth.user_id; header.status_project = "N"; header.tasks_complete = 0; header.tasks_count = 0; db.iteam_project.Add(header); db.SaveChanges(); if (detail != null && detail.Count() > 0) { foreach (var x in detail) { x.project_id = header.project_id; } db.iteam_project_user.AddRange(detail); db.SaveChanges(); } else { throw new Exception("กรุณาเลือกผู้ใช้งานลงในโปรเจค"); } if (history != null && history.Count() > 0) { foreach (var x in history) { x.project_id = header.project_id; x.project_name = header.project_name; x.read_notify = "N"; x.active = "Y"; x.add_user = auth.user_id; } db.iteam_history_project.AddRange(history); db.SaveChanges(); } transaction.Commit(); } catch (DbEntityValidationException ex) { transaction.Rollback(); error = ex.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage; } catch (Exception ex) { transaction.Rollback(); error = ex.Message; if (ex.InnerException != null) { error = ex.InnerException.GetBaseException().Message; } } } } }