public bool ChangeRating(string resultId, int newRating) { bool isChange; try { using (TestingSystemContext context = new TestingSystemContext()) { TestResult result = context.Results.FirstOrDefault(r => r.Id.Equals(resultId)); if (result is null) { isChange = false; } else { result.Rating = newRating; context.SaveChanges(); isChange = true; } } } catch (Exception) { isChange = false; } return(isChange); }
public void CreateNewTest(Test test) { try { using (TestingSystemContext context = new TestingSystemContext()) { using (var transaction = context.Database.BeginTransaction()) { try { foreach (Question question in test.Questions) { context.Questions.Add(question); context.SaveChanges(); } context.Tests.Add(test); context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); } } } Dictionary <string, string> tests = SendTestsListToClients(); foreach (IServiceCallback callback in _clientsCallback) { callback.SetTestList(tests); } } catch (Exception) { // ignored } }
public bool RegisterNewUser(User user) { bool isRegister; try { using (TestingSystemContext context = new TestingSystemContext()) { context.Users.Add(user); context.SaveChanges(); } isRegister = true; } catch (Exception) { isRegister = false; } return(isRegister); }
public void EndTesting(TestResult result, string groupId, string subgroupId, string testId) { IServiceCallback callback = Callback; if (callback != null) { try { TestOnline testOnline; bool isFoundTest = _onlineTests.TryGetValue(testId, out testOnline); if (isFoundTest) { if (testOnline.Groups.Count > 0) { Group group = testOnline.Groups.FirstOrDefault(g => g.Id.Equals(groupId)); Subgroup subgroup = group?.Subgroups.FirstOrDefault(s => s.Id.Equals(subgroupId)); if (subgroup != null) { try { subgroup.Timer.Stop(); } catch { // ignored } group.Subgroups.Remove(subgroup); if (group.Subgroups.Count == 0) { testOnline.Groups.Remove(group); } using (TestingSystemContext context = new TestingSystemContext()) { using (var transaction = context.Database.BeginTransaction()) { try { foreach (QuestionWithAnswer question in result.Questions) { context.QuestionWithAnswers.Add(question); context.SaveChanges(); } User user = context.Users.FirstOrDefault(u => u.Id.Equals(result.User.Id)); if (user != null) { result.User = user; } Test test = context.Tests.FirstOrDefault(t => t.Id.Equals(testId)); if (test != null) { result.Test = test; } context.Results.Add(result); context.SaveChanges(); transaction.Commit(); callback.ResultEndTesting(true, "Результаты теста были успешно сохранены!"); } catch (Exception exception) { transaction.Rollback(); callback.ResultEndTesting(false, exception.Message); } } } } } } } catch (Exception exception) { callback.ResultEndTesting(false, exception.Message); } } }