コード例 #1
0
        public IActionResult SaveChanges(int score)
        {
            string userId = GetUserId();

            if (userId != null)
            {
                int testId     = _helper.GetLastTestId();
                int categoryId = _helper.GetLastSubcategoryId();
                int langId     = Helper.GetLanguagesId().Item2;

                using (_db)
                {
                    TestResults oldResults;
                    bool        isDone = _helper.IsTestDoneOnce(userId, testId, langId, categoryId, out oldResults);
                    if (isDone)
                    {
                        int oldScore = oldResults.Result;
                        if (score > oldScore)
                        {
                            oldResults.Result           = score;
                            oldResults.TestDate         = DateTime.Now;
                            _db.Entry(oldResults).State = EntityState.Modified;
                        }
                    }
                    else
                    {
                        TestResults results = new TestResults
                        {
                            TestId     = testId,
                            Result     = score,
                            UserId     = userId,
                            CategoryId = categoryId,
                            LangId     = langId,
                            TestDate   = DateTime.Now
                        };
                        _db.TestResults.Add(results);
                    }
                    _db.SaveChanges();

                    _helper.UpdateTotalScore(userId, langId);
                }
            }
            return(RedirectToAction("Index"));
        }
コード例 #2
0
        public void UpdateTotalScore(string userId, int langId)
        {
            int total = 0;
            List <TestResults> results = _db.TestResults.Where(r => r.UserId == userId && r.LangId == langId).ToList();

            foreach (TestResults testResult in results)
            {
                total += testResult.Result;
            }

            TotalScores totalScores = null;

            try
            {
                totalScores = _db.TotalScores.First(s => s.UserId == userId && s.LangId == langId);
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine("New test result");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            if (totalScores != null)
            {
                totalScores.Total            = total;
                _db.Entry(totalScores).State = EntityState.Modified;
            }
            else
            {
                totalScores        = new TotalScores();
                totalScores.LangId = langId;
                totalScores.UserId = userId;
                totalScores.Total  = total;
                _db.TotalScores.Add(totalScores);
            }

            _db.SaveChanges();
        }