public async Task UpdateScoresByPublicationID(int PublicationID, string UserName) { try { int count = 0; PublicationTemp currentPub = DbSet.Where(p => p.PublicationID == PublicationID) .Include(p => p.AreaScoreTemps) .Include(p => p.AreaScoreTemps.Select(a => a.SubAreaScoreTemps)).FirstOrDefault(); if (currentPub != null) { using (IAreaScoreRepository db = new AreaScoreRepository()) { // save area scores currentPub.AreaScoreTemps.ToList().ForEach(a => { AreaScore currentAreaScore = db.Where(s => s.ReleaseID == currentPub.ReleaseID && s.CPID == currentPub.CPID && s.AreaID == a.AreaID).FirstOrDefault(); if (currentAreaScore != null) { currentAreaScore.Score = a.Score; } else { db.Add(new AreaScore() { AreaID = a.AreaID, ReleaseID = currentPub.ReleaseID, CPID = currentPub.CPID, Score = a.Score }); Task.Run(async() => { count += await db.SaveAsync(null, false, null, null, UserName); }).Wait(); currentAreaScore = db.Where(s => s.AreaID == a.AreaID && s.ReleaseID == currentPub.ReleaseID && s.CPID == currentPub.CPID).FirstOrDefault(); } // save sub area scores using (ISubAreaScoreRepository dbSub = new SubAreaScoreRepository()) { a.SubAreaScoreTemps.ToList().ForEach(s => { SubAreaScore currentSub = dbSub.Where(sub => sub.SubAreaID == s.SubAreaID && sub.AreaScoreID == currentAreaScore.AreaScoreID).FirstOrDefault(); if (currentSub != null) { currentSub.Score = s.Score; } else { dbSub.Add(new SubAreaScore() { SubAreaID = s.SubAreaID, AreaScoreID = currentAreaScore.AreaScoreID, Score = s.Score }); } }); Task.Run(async() => { count += await dbSub.SaveAsync(null, false, null, null, UserName); }).Wait(); } }); } } } catch (Exception) { throw; } }
public async Task <int> SaveAreaScore(StatusVM status) { int count = 0; using (IAreaScoreRepository db = new AreaScoreRepository()) { status.AreaScores.ForEach(a => { var areaScore = db.Where(r => r.ReleaseID == status.ReleaseID && r.AreaID == a.AreaID && r.CPID == status.CPID).FirstOrDefault(); if (areaScore != null)//edit areaScore { if (areaScore.Score != a.Score) { areaScore.Score = a.Score; db.Edit(areaScore); } } else//add areaScore { areaScore = new AreaScore() { ReleaseID = status.ReleaseID, AreaID = a.AreaID, CPID = status.CPID, Score = a.Score }; db.Add(areaScore); } }); count += await db.SaveAsync((WindowsPrincipal)User); using (ISubAreaScoreRepository dbSubAreaScore = new SubAreaScoreRepository()) { status.AreaScores.ForEach(a => { var areaScore = db.Where(r => r.ReleaseID == status.ReleaseID && r.AreaID == a.AreaID && r.CPID == status.CPID).FirstOrDefault(); a.SubAreaScors.ForEach(sas => { var subAreaScore = dbSubAreaScore.Where(s => s.SubAreaID == sas.SubAreaID && s.AreaScoreID == areaScore.AreaScoreID).FirstOrDefault(); if (subAreaScore != null)//edit subAreaScore { if (subAreaScore.Score != sas.Score) { subAreaScore.Score = sas.Score; dbSubAreaScore.Edit(subAreaScore); } } else//add subAreaScore { subAreaScore = new SubAreaScore() { SubAreaID = sas.SubAreaID, AreaScoreID = areaScore.AreaScoreID, Score = sas.Score }; dbSubAreaScore.Add(subAreaScore); } }); }); count += await dbSubAreaScore.SaveAsync((WindowsPrincipal)User); } } return(count); }