public void TestResultComputation() { Assert.IsNull(GetTestSurveyFromDb()); Survey s = CreateTestSurvey(); Survey s2 = CreateTestSurvey(); s2.Question += "2"; using (var db = new TestSurveyContext()) { db.Surveys.Add(s); db.Surveys.Add(s2); db.SaveChanges(); Vote v1 = new Vote(new User() { Code = "User1", Created = DateTime.Now }, s, s.Answers.ElementAt(0)); db.Votes.Add(v1); Vote v2 = new Vote(new User() { Code = "User2", Created = DateTime.Now }, s, s.Answers.ElementAt(2)); db.Votes.Add(v2); Vote v3 = new Vote(new User() { Code = "User3", Created = DateTime.Now }, s, s.Answers.ElementAt(0)); db.Votes.Add(v3); Vote v4 = new Vote(new User() { Code = "User3", Created = DateTime.Now }, s2, s2.Answers.ElementAt(0)); db.Votes.Add(v4); db.SaveChanges(); } using (var db = new TestSurveyContext()) { Result[] res = db.getResultsFor(s); Assert.IsNotNull(res); Assert.IsTrue(res.Length == 2); Assert.IsTrue(res[0].Answer.AnswerText == "Answer 1"); Assert.IsTrue(res[0].Count == 2); Assert.IsTrue(res[1].Answer.AnswerText == "Answer 3"); Assert.IsTrue(res[1].Count == 1); Vote[] votes = db.Votes.Where(v => v.Survey.Id == s.Id || v.Survey.Id == s2.Id).ToArray<Vote>(); foreach (Vote v in votes) { db.Votes.Remove(v); } db.SaveChanges(); } using (var db = new TestSurveyContext()) { db.Surveys.Attach(s); db.Surveys.Remove(s); db.Surveys.Attach(s2); db.Surveys.Remove(s2); db.SaveChanges(); } }
protected async void SaveNewVote () { if (usersVote != null) { // Delete old vote if it exists await api.Backend.DeleteVoteAsync (usersVote); } Vote newVote = new Vote (BackendAuthManager.Instance.User, Survey, UsersAnswer.Answer); await api.Backend.SaveVoteAsync (newVote); usersVote = await Backend.GetVoteForUserAsync (Survey, BackendAuthManager.Instance.User); }
protected async void LoadUsersVote () { usersVote = await Backend.GetVoteForUserAsync (Survey, BackendAuthManager.Instance.User); if (usersVote != null) UsersAnswer = new AnswerViewModel (usersVote.Answer, HighlightBackgroundColor); IsBusy = false; }
public void DeleteVote(Vote vote) { vote.Survey = this.Surveys.Where(s => s.Id == vote.Survey.Id).First<Survey>(); vote.User = this.Users.Where(u => u.Id == vote.User.Id).First<User>(); vote.Answer = this.Answers.Where(a => a.Id == vote.Answer.Id).First<Answer>(); this.Votes.Attach(vote); this.Votes.Remove(vote); this.SaveChanges(); }
public void SaveVote(Vote vote) { this.Surveys.Attach(vote.Survey); this.Users.Attach(vote.User); vote.Answer = vote.Survey.Answers.First<Answer>(a => a.Id == vote.Answer.Id); if (vote.Id == 0) { this.Votes.Add(vote); } else { this.Votes.Attach(vote); } this.SaveChanges(); }
public void TestVoteData() { Assert.IsNull(GetTestSurveyFromDb()); Survey survey = CreateTestSurvey(); using (var db = new TestSurveyContext()) { db.SaveSurvey(survey); Vote v1 = new Vote(new User() { Code = "User1", Created = DateTime.Now }, survey, survey.Answers.ElementAt(0)); db.SaveVote(v1); Vote v2 = new Vote(new User() { Code = "User2", Created = DateTime.Now }, survey, survey.Answers.ElementAt(2)); db.SaveVote(v2); Vote v3 = new Vote(new User() { Code = "User3", Created = DateTime.Now }, survey, survey.Answers.ElementAt(0)); db.SaveVote(v3); db.SaveChanges(); } using (var db = new TestSurveyContext()) { db.Surveys.Attach(survey); var query = db.Votes.Where(v => v.Survey.Id == survey.Id); Assert.IsNotNull(query); Assert.IsTrue(query.Count() == 3); Assert.IsTrue(query.Where(v => v.User.Code == "User3").Count() == 1); foreach(Vote v in query) { db.Votes.Remove(v); } db.Surveys.Remove(survey); db.SaveChanges(); } Assert.IsNull(GetTestSurveyFromDb()); }
public string SaveVote(Vote vote) { try { if (vote == null) { throw new ArgumentNullException(); } using (var db = new SurveyContext()) { db.SaveVote(vote); } return "0"; } catch (Exception e) { return e.Message; } }