public ActionResponse UpdateGateInfo(WrapperUpdateProject model) { //bool output = false; ActionResponse ar = new ActionResponse(); if (model.estimate != null) { using (DatabaseContext db = new DatabaseContext()) { using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { //Delete Functionality string listRecDel = model.estimate[0].DeletRows; if (listRecDel != null) { string[] ids = listRecDel.Split(','); for (int i = 0; i < ids.Length; i++) { int recid = Convert.ToInt32(ids[i]); List <Estimate> deleteList = db.Estimates.Where(id => id.projectFunctionId == recid).ToList(); foreach (Estimate e in deleteList) { db.Entry(e).State = EntityState.Deleted; db.SaveChanges(); } ProjectFunction pf = db.ProjectFunctions.SingleOrDefault(id => id.ProjFuncId == recid); db.Entry(pf).State = EntityState.Deleted; db.SaveChanges(); } } GateVersion gv = model.gateVersion; // gv.gvId = model.gateVersion.gvId; db.Entry(gv).State = EntityState.Modified; db.SaveChanges(); // List<Estimate> NewEstimate = model.estimate.Where(o => o.projectFunctionId == 0).ToList(); List <Estimate> NewGroupEstimate = model.estimate.GroupBy(x => new { x.ProjFuncId, x.GateversionId, x.FuncId, x.AppId, x.RelId }) .Select(o => o.First()).ToList().Select(o => new Estimate { ProjFuncId = o.ProjFuncId, GateversionId = o.GateversionId, FuncId = o.FuncId, AppId = o.AppId, RelId = o.RelId }).ToList(); foreach (Estimate e in NewGroupEstimate) { ProjectFunction pf = new ProjectFunction(); pf.ProjFuncId = e.ProjFuncId; pf.AppId = e.AppId; pf.RelId = e.RelId; pf.FuncId = e.FuncId; pf.GateversionId = e.GateversionId; db.ProjectFunctions.Add(pf); if (pf.ProjFuncId > 0) { db.Entry(pf).State = EntityState.Modified; } db.SaveChanges(); var resultEstimate = model.estimate.Where(x => x.FuncId == pf.FuncId); foreach (var item2 in resultEstimate) { if (item2.Flag != "X") { Estimate est = new Estimate(); est.projectFunctionId = pf.ProjFuncId; est.estId = item2.estId; est.estDays = item2.estDays; est.phaseCode = item2.phaseCode; est.roleId = item2.roleId; est.estimatedOn = item2.estimatedOn; est.userEstimate = item2.userEstimate; db.Estimates.Add(est); if (est.estId > 0) { if (item2.Flag == "D") { db.Entry(est).State = EntityState.Deleted; } else { db.Entry(est).State = EntityState.Modified; } } db.SaveChanges(); } } } transaction.Commit(); MyLogger.GetInstance().Info("gate info updated successfully.." + model.gateVersion.gvProjectId); ar.IsSuccess = true; } catch (Exception ex) { MyLogger.GetInstance().Error("Error - updating gate info " + model.gateVersion.gvProjectId); MyLogger.GetInstance().Error("Error - updating gate info " + ex.Message); transaction.Rollback(); ar.IsSuccess = false; ar.ErrorMsg = "Database exception"; }; } } } else { ar.IsSuccess = false; ar.ErrorMsg = "Model invalid"; } return(ar); }
public bool AddNewProject(WrapperUpdateProject model) { string msg = ""; bool output = false; using (DatabaseContext db = new DatabaseContext()) { using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { db.Projects.Add(model.project); db.SaveChanges(); model.gateVersion.gvProjectId = model.project.id; db.GateVersions.Add(model.gateVersion); db.SaveChanges(); var result = model.estimate.GroupBy(x => new { x.FuncId, x.AppId, x.RelId }); foreach (var item in result) { // System.Diagnostics.Debug.WriteLine("{0} - {1}", item.Key, item.Count()); // System.Diagnostics.Debug.WriteLine("{0} - {1} - {2}",item.Key.FuncId,item.Key.AppId,item.Key.RelId); ProjectFunction pf = new ProjectFunction(); pf.AppId = item.Key.AppId; pf.RelId = item.Key.RelId; pf.FuncId = item.Key.FuncId; pf.GateversionId = model.gateVersion.gvId; db.ProjectFunctions.Add(pf); db.SaveChanges(); // throw new Exception(); var resultEstimate = model.estimate.Where(x => x.FuncId == pf.FuncId); foreach (var item2 in resultEstimate) { Estimate est = new Estimate(); est.projectFunctionId = pf.ProjFuncId; est.estDays = item2.estDays; est.phaseCode = item2.phaseCode; est.roleId = item2.roleId; est.estimatedOn = item2.estimatedOn; est.userEstimate = item2.userEstimate; db.Estimates.Add(est); db.SaveChanges(); } } transaction.Commit(); MyLogger.GetInstance().Info("project created successfully"); output = true; } catch (Exception ex) { if (ex.InnerException is null) { msg = ex.Message; } else { msg = ex.InnerException.Message; } // System.Diagnostics.Debug.WriteLine(ex.InnerException.Message); // Console.WriteLine(ex.InnerException.Message); // return false; transaction.Rollback(); MyLogger.GetInstance().Error("Error - saving new project" + msg); output = false; } } } return(output); }