public ActionResponse GateRemove(int pid, int gid, int gl) { ActionResponse ar = new ActionResponse(); using (DatabaseContext db = new DatabaseContext()) { using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { GateVersion gv = db.GateVersions.Where(x => x.gvProjectId == pid && x.gvGateId == gid && x.gvLine == gl).First(); List <ProjectFunction> listfunc = db.ProjectFunctions.Where(x => x.GateversionId == gv.gvId).ToList(); foreach (var item in listfunc) { List <Estimate> listEstimate = db.Estimates.Where(x => x.projectFunctionId == item.ProjFuncId).ToList(); foreach (var obj in listEstimate) { db.Entry(obj).State = EntityState.Deleted; db.SaveChanges(); } db.Entry(item).State = EntityState.Deleted; db.SaveChanges(); } db.Entry(gv).State = EntityState.Deleted; db.SaveChanges(); List <EstimateAttachment> listattachmets = db.EstimateAttachments. Where(x => x.projectId == pid && x.gateId == gid && x.lineNo == gl).ToList(); foreach (var item in listattachmets) { db.Entry(item).State = EntityState.Deleted; db.SaveChanges(); } var count = db.GateVersions.Count(t => t.gvProjectId == pid); if (count == 0) { Project prj = db.Projects.Where(x => x.id == pid).First(); db.Entry(prj).State = EntityState.Deleted; db.SaveChanges(); } transaction.Commit(); ar.SuccessMsg = "Gate remove successfully"; ar.IsSuccess = true; MyLogger.GetInstance().Info(ar.SuccessMsg); } catch (Exception ex) { transaction.Rollback(); ar.IsSuccess = false; if (ex.InnerException.Message != null) { ar.ErrorMsg = ex.InnerException.Message; } else { ar.ErrorMsg = ex.Message; } MyLogger.GetInstance().Error("Error - Gate removal " + pid); MyLogger.GetInstance().Error(ar.ErrorMsg); } } } return(ar); }
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); }