public static VotingClass AddPoll(VotingClass poll, Guid memberId) { try { var member = MemberCache.GetMemberDisplay(memberId); var dc = new ManagementContext(); RDN.Library.DataModels.Controls.Voting.VotingV2 voting = new DataModels.Controls.Voting.VotingV2(); voting.IsDeleted = false; voting.IsPublic = poll.IsPublic; voting.IsPollAnonymous = poll.IsPollAnonymous; voting.LeagueOwner = dc.Leagues.Where(x => x.LeagueId == member.CurrentLeagueId).FirstOrDefault(); voting.Description = poll.Description; voting.Title = poll.Title; voting.IsOpenToLeague = poll.IsOpenToLeague; for (int i = 0; i < poll.Questions.Count; i++) { VotingQuestion question = new VotingQuestion(); question.Question = poll.Questions[i].Question; question.QuestionSortId = i; question.QuestionType = (int)poll.Questions[i].QuestionType; for (int j = 0; j < poll.Questions[i].Answers.Count; j++) { if (!String.IsNullOrEmpty(poll.Questions[i].Answers[j].Answer)) { VotingAnswer answer = new VotingAnswer(); answer.Answer = poll.Questions[i].Answers[j].Answer; answer.Question = question; question.Answers.Add(answer); } } voting.Questions.Add(question); } //List<Guid> listOfGuids = new List<Guid>(); foreach (var mem in poll.Voters) { voting.Voters.Add(new VotingVoters() { HasVoted = false, Member = dc.Members.Where(x => x.MemberId == mem.MemberId).FirstOrDefault() }); } dc.VotingV2.Add(voting); int c = dc.SaveChanges(); poll.VotingId = voting.VotingId; if (poll.BroadcastPoll) { var mems = MemberCache.GetLeagueMembers(memberId, member.CurrentLeagueId); for (int j = 0; j < poll.Voters.Count; j++) { if (memberId != poll.Voters[j].MemberId) { var m = mems.Where(x => x.MemberId == poll.Voters[j].MemberId).FirstOrDefault(); SendEmailAboutNewPoll(poll.LeagueId, poll.VotingId, member.DerbyName, m.DerbyName, m.UserId); } } } List<Guid> memIds = poll.Voters.Select(x => x.MemberId).ToList(); var fact = new MobileNotificationFactory() .Initialize("New Poll Created:", poll.Title, Mobile.Enums.NotificationTypeEnum.NewPollCreated) .AddId(poll.VotingId) .AddMembers(memIds) .SendNotifications(); } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: poll.ToMemberIds); } return poll; }
public static bool UpdatePollMobileAPI(VotingClass poll) { var dc = new ManagementContext(); try { Guid lId = new Guid(poll.LeagueId); var voting = dc.VotingV2.Where(x => x.LeagueOwner.LeagueId == lId && x.VotingId == poll.VotingId).FirstOrDefault(); voting.Title = poll.Title; voting.Description = poll.Description; voting.IsOpenToLeague = poll.IsOpenToLeague; ////question got deleted. //shouldn't be allowed to delete. //if (poll.Questions.Count != voting.Questions.Count) //{ // for (int k = 0; k < voting.Questions.Count; k++) // { // var aTemp = poll.Questions.Where(x => x.QuestionId == voting.Questions[k].QuestionId).FirstOrDefault(); // if (aTemp == null) // voting.Questions[k].IsRemoved = true; // } //} for (int i = 0; i < poll.Questions.Count; i++) { var temp = voting.Questions.Where(x => x.QuestionId == poll.Questions[i].QuestionId).FirstOrDefault(); if (temp == null) { temp = new VotingQuestion(); temp.Question = poll.Questions[i].Question; temp.QuestionSortId = voting.Questions.Count; temp.QuestionType = (int)poll.Questions[i].QuestionType; for (int j = 0; j < poll.Questions[i].Answers.Count; j++) { if (!String.IsNullOrEmpty(poll.Questions[i].Answers[j].Answer)) { VotingAnswer answer = new VotingAnswer(); answer.Answer = poll.Questions[i].Answers[j].Answer; answer.Question = temp; temp.Answers.Add(answer); } } voting.Questions.Add(temp); } else { temp.Question = poll.Questions[i].Question; temp.QuestionSortId = i; temp.QuestionType = (int)poll.Questions[i].QuestionType; //answer got deleted if (poll.Questions[i].Answers.Count != temp.Answers.Count) { for (int k = 0; k < temp.Answers.Count; k++) { var aTemp = poll.Questions[i].Answers.Where(x => x.AnswerId == temp.Answers[k].AnswerId).FirstOrDefault(); if (aTemp == null) temp.Answers[k].WasRemoved = true; } } for (int j = 0; j < poll.Questions[i].Answers.Count; j++) { var tempAnswer = temp.Answers.Where(x => x.AnswerId == poll.Questions[i].Answers[j].AnswerId).FirstOrDefault(); if (tempAnswer == null) { if (!String.IsNullOrEmpty(poll.Questions[i].Answers[j].Answer)) { tempAnswer = new VotingAnswer(); tempAnswer.Answer = poll.Questions[i].Answers[j].Answer; tempAnswer.Question = temp; temp.Answers.Add(tempAnswer); } } else { tempAnswer.Answer = poll.Questions[i].Answers[j].Answer; } } } } int c = dc.SaveChanges(); return c > 0; } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } return false; }