public void SaveTestResult([FromBody] TestResults testResult) { if (testResult != null) { testResult.TestDate = DateTime.Now; using (ProductHouseContext db = new ProductHouseContext()) { db.TestResults.Add(testResult); db.SaveChanges(); } } }
public bool ChangeLanguages(string login, int nativeLanguageId, int learningLanguageId) { Users user = GetByEmailOrName(login); if (user == null) { return false; } user.UserSettings.First().NativeLanguageId = nativeLanguageId; user.UserSettings.First().LearningLanguageId = learningLanguageId; db.SaveChanges(); return true; }
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")); }
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(); }
public IActionResult Register([FromBody] Users user) { if (user == null) { return(InvalidClientRequest()); } LoginModel loginModel = new LoginModel { Login = user.Email, Password = user.PasswordHash }; string userId = Guid.NewGuid().ToString("N"); string hashedPassword = _helper.ComputeSha256Hash(user.PasswordHash); UserSettings userSettings = new UserSettings { LearningLanguageId = 0, NativeLanguageId = 0, UserId = userId }; user.Id = userId; user.UserSettings = new List <UserSettings> { userSettings }; user.PasswordHash = hashedPassword; using (ProductHouseContext db = new ProductHouseContext()) { db.Users.Add(user); db.SaveChanges(); } return(Login(loginModel)); }