public void DeleteUserVote(PollChoiceRecord choice, string user) { var votes = _voteRepository.Fetch(v => v.PollChoiceRecord.Id == choice.Id && v.Username == user); foreach (var v in votes) // there can be only one.. { _voteRepository.Delete(v); } }
public void CreateChoice(PollChoiceRecord choice) { _choiceRepository.Create(choice); _resultRepository.Create(new PollResultRecord { Count = 0, PollChoiceRecord = choice }); }
public ActionResult CreatePoll(PollViewModel poll) { // Create a new Poll and Choices with values posted var newPoll = Services.ContentManager.New("Poll"); var newPollPart = newPoll.As <PollPart>(); if (!ModelState.IsValid) { Services.TransactionManager.Cancel(); Services.Notifier.Information(T("Der opstod en fejl ved validering af data - kontakt evt. [email protected]")); return(View("EditorTemplates/Parts/PollForm", poll)); } if (ModelState.IsValid) { // Set title for item - important! newPollPart.As <TitlePart>().Title = poll.Question; newPollPart.Question = poll.Question; newPollPart.OpenDateUtc = poll.Open; newPollPart.CloseDateUtc = poll.Close; newPollPart.MaxVotes = poll.MaxVotes; newPollPart.IsShown = poll.Shown; newPollPart.ShowVotingUI = poll.ShowVotingUI; newPollPart.IsMusicPoll = poll.MusicPoll; newPollPart.IsMusicPollInTestMode = poll.MusicPollInTestMode; foreach (ChoiceEntry c in poll.Choices) { if (c.Choice.Answer != null && c.Choice.Answer.Length > 0) { var choice = new PollChoiceRecord { Answer = c.Choice.Answer, PollPartRecord = newPollPart.Record }; _pollService.CreateChoice(choice); } } } // Persist new poll content item (publish) Services.ContentManager.Create(newPoll); Services.Notifier.Information(T("Din afstemning er nu oprettet")); // var model = new PollViewModel(newPollPart, poll.Choices); // return View("Parts/Poll", model); return(RedirectToAction("Index")); }
public void DeleteChoice(PollChoiceRecord choice) { var results = _resultRepository.Fetch(r => r.PollChoiceRecord.Id == choice.Id); var votes = _voteRepository.Fetch(v => v.PollChoiceRecord.Id == choice.Id); foreach (var r in results) { _resultRepository.Delete(r); } foreach (var v in votes) { _voteRepository.Delete(v); } _choiceRepository.Delete(choice); }
public void Import(Stream stream) { using (StreamReader reader = new StreamReader(stream)) { XmlSerializer serializer = new XmlSerializer(typeof(PollEntry)); var entry = serializer.Deserialize(reader) as PollEntry; if (entry != null) { var p = _contentManager.Create <PollPart>("Poll", poll => { poll.Record.Question = entry.Question; poll.Record.OpenDateUtc = entry.OpenDateUtc; poll.Record.CloseDateUtc = entry.CloseDateUtc; poll.Record.MaxVotes = entry.MaxVotes; poll.Record.Shown = entry.IsShown; poll.Record.MusicPoll = entry.IsMusicPoll; poll.Record.ShowVotingUI = entry.ShowVotingUI; }); foreach (var choice in entry.Choices) { PollChoiceRecord c = new PollChoiceRecord { Answer = choice.Answer, PollPartRecord = p.Record }; _choiceRepository.Create(c); var result = new PollResultRecord { Count = choice.Count, PollChoiceRecord = c }; _resultRepository.Create(result); } } } }
public void EditSong(SongPart part, SongViewModel model) { if (part == null) { throw new ArgumentNullException("part"); } if (model == null) { throw new ArgumentNullException("model"); } var song = new SongPartRecord { Id = part.Record.ContentItemRecord.Id, SongTitle = model.SongTitle, SongAuthor = model.SongAuthor, SongUrl = model.SongUrl, SongDescription = model.SongDescription, ContentItemRecord = part.Record.ContentItemRecord }; switch (model.SongCategory) { case ("Original"): // "1" { song.SongCategory = SongCategory.Original; } break; case ("Cowrite"): // "2" { song.SongCategory = SongCategory.Cowrite; } break; case ("IntOriginal"): // "3" { song.SongCategory = SongCategory.IntOriginal; } break; case ("IntCowrite"): // "4" { song.SongCategory = SongCategory.IntCowrite; } break; default: song.SongCategory = SongCategory.Original; break; } // slet den gamle Poll Choice Record var oldPollChoiceRecord = _choiceRepository.Fetch(r => r.Id == part.PollChoiceRecord_Id).FirstOrDefault(); if (oldPollChoiceRecord != null) { _pollService.DeleteChoice(oldPollChoiceRecord); } var pollId = Convert.ToInt32(model.SelectedPollId); var choice = new PollChoiceRecord { Answer = song.SongTitle, PollPartRecord = _pollService.GetPoll(pollId) }; _pollService.CreateChoice(choice); // First, get all the poll choices in an ordered list. // then, set PollChoiceRecord_Id to the last choice item in poll, the newest addition. Best we can do.. var choices = _pollService.GetOrderedChoices(pollId); if (choices.Count() >= 1) { song.PollChoiceRecord_Id = choices.Last().Id; } UpdateSong(song); }
public void EditPoll(PollPart part, PollViewModel model) { if (part == null) { throw new ArgumentNullException("part"); } if (model == null) { throw new ArgumentNullException("model"); } var poll = new PollPartRecord { Id = part.Record.ContentItemRecord.Id, Question = model.Question, OpenDateUtc = model.Open, CloseDateUtc = model.Close, MaxVotes = model.MaxVotes, Shown = model.Shown, MusicPoll = model.MusicPoll, ShowVotingUI = model.ShowVotingUI, ContentItemRecord = part.Record.ContentItemRecord }; UpdatePoll(poll); foreach (var entry in model.Choices) { switch (entry.Action) { case "Alter": var choice = new PollChoiceRecord { Id = entry.Choice.Id, Answer = entry.Choice.Answer, PollPartRecord = part.Record }; UpdateChoice(choice); break; case "Create": if (entry.Choice.Answer != null && entry.Choice.Answer.Length > 0) { choice = new PollChoiceRecord { Answer = entry.Choice.Answer, PollPartRecord = part.Record }; CreateChoice(choice); } break; } } var choices = GetChoices(poll.Id).ToList(); var newChoices = model.Choices.Select(c => choices.FirstOrDefault(x => x.Id == c.Choice.Id || x.Answer == c.Choice.Answer)); var toDelete = choices.Except(newChoices); foreach (var choice in toDelete) { DeleteChoice(choice); } }
public void UpdateChoice(PollChoiceRecord choice) { _choiceRepository.Update(choice); }
public ActionResult Create(SongViewModel song) { // Check if Music Poll is in test mode, allowing any number of song entries per user // If Poll is not in Test Mode (checkbox not checked in admin panel) allow only one song entry per user per poll var pollNo = song.SelectedPollId; if (!_pollService.IsMusicPollInTestMode(pollNo)) { var existingSongsInPoll = _songService.GetSongsForPoll(pollNo); if (existingSongsInPoll.Count() >= 1) { foreach (var s in existingSongsInPoll) { if (s.SongEditorUserName == Services.WorkContext.CurrentUser.UserName) { Services.Notifier.Information(T("Du har allerede nomineret en sang til denne afstemning - slet den venligst igen, hvis du ønsker at nominere på ny.")); return(Redirect("~/Endpoint")); } } } } // Create song with link to Poll and Choice Id var newSong = Services.ContentManager.New("Song"); var newSongPart = newSong.As <SongPart>(); if (!ModelState.IsValid) { Services.TransactionManager.Cancel(); Services.Notifier.Information(T("Der opstod en fejl ved validering af sangens data - kontakt evt. [email protected]")); return(View("EditorTemplates/Parts/Song", song)); } if (ModelState.IsValid) { newSongPart.SongTitle = song.SongTitle; newSongPart.SongAuthor = song.SongAuthor; // special handling of form input (embedded soundcloud html / long youtube url's etc.) // @ will escape html chars, ostensibly allowing for embedding soundcloud links //if (song.SongUrl.Contains("iframe")) //{ // newSongPart.SongUrl = HttpUtility.HtmlEncode(@song.SongUrl); //} if (song.SongUrl.Contains("youtube.com/watch?v=")) { newSongPart.SongUrl = song.SongUrl.Replace("watch?v=", "embed/"); } else { newSongPart.SongUrl = song.SongUrl; } newSongPart.SongThumbnailUrl = song.SongThumbnailUrl; newSongPart.SongDescription = song.SongDescription; newSongPart.SongEditorUserName = Services.WorkContext.CurrentUser.UserName; newSongPart.SongEditorUserItemId = Services.WorkContext.CurrentUser.Id; newSongPart.CreatedDateUtc = DateTime.UtcNow; newSongPart.SubmittedDateUtc = DateTime.UtcNow; newSongPart.Votes = 0; newSongPart.Shown = true; // Set title for item - important! newSongPart.As <TitlePart>().Title = song.SongTitle; // now, create a choice record to latch on to var pollId = Convert.ToInt32(song.SelectedPollId); var choice = new PollChoiceRecord { Answer = song.SongTitle + " (" + song.SongAuthor + ")", PollPartRecord = _pollService.GetPoll(pollId) }; _pollService.CreateChoice(choice); // set PollChoiceRecord_Id to the last choice item in poll, the newest addition. Best we can do.. var choices = _pollService.GetChoices(pollId); if (choices.Count() >= 1) { // re-order the random shuffle, so the last in sequence is indeed the last in sequence choices = choices.OrderBy(c => c.Id).AsQueryable <PollChoiceRecord>(); newSongPart.PollChoiceRecord_Id = choices.Last().Id; } // Catch song category choice from chosen radio button vlaue switch (HttpContext.Request.Params.Get("SongCategory")) { case ("Original"): { newSongPart.SongCategory = SongCategory.Original; } break; case ("Cowrite"): { newSongPart.SongCategory = SongCategory.Cowrite; } break; case ("IntOriginal"): { newSongPart.SongCategory = SongCategory.IntOriginal; } break; case ("IntCowrite"): { newSongPart.SongCategory = SongCategory.IntCowrite; } break; default: newSongPart.SongCategory = SongCategory.Original; break; } } // Persist new song content item (publish) Services.ContentManager.Create(newSong); var pollTitle = _pollService.GetPollTitle(pollNo); Services.Notifier.Information(T("Din sang er nu tilmeldt " + pollTitle)); var model = new SongViewModel(newSongPart); return(View("Parts/Song", model)); }
public ActionResult EditFromAdminPanel(SongViewModel song) { var part = new SongPartRecord { Id = song.SongPartRecordId, SongTitle = song.SongTitle, SongAuthor = song.SongAuthor, SongUrl = song.SongUrl, SongDescription = song.SongDescription, SongThumbnailUrl = song.SongThumbnailUrl, PollChoiceRecord_Id = song.PollChoiceRecord_Id, Shown = true, SongEditorUserName = song.SongEditorUserName, SongEditorUserItemId = song.SongEditorUserItemId, SubmittedDateUtc = DateTime.UtcNow }; // Catch song category choice from chosen radio button value switch (HttpContext.Request.Params.Get("SongCategory")) { case ("Original"): { part.SongCategory = SongCategory.Original; } break; case ("Cowrite"): // "2" { part.SongCategory = SongCategory.Cowrite; } break; case ("IntOriginal"): // "3" { part.SongCategory = SongCategory.IntOriginal; } break; case ("IntCowrite"): // "4" { part.SongCategory = SongCategory.IntCowrite; } break; default: part.SongCategory = SongCategory.Original; break; } // slet den gamle Poll Choice Record var oldPollChoiceRecord = _choiceRepository.Fetch(c => c.Id == song.PollChoiceRecord_Id).FirstOrDefault(); if (oldPollChoiceRecord != null) { _pollService.DeleteChoice(oldPollChoiceRecord); } // opret ny poll choice record, linked til den valgte poll id fra selectlisten var pollId = Convert.ToInt32(song.SelectedPollId); var choice = new PollChoiceRecord { Answer = song.SongTitle + " (" + song.SongAuthor + ")", PollPartRecord = _pollService.GetPoll(pollId) }; _pollService.CreateChoice(choice); // set PollChoiceRecord_Id to the last choice item in poll, the newest addition. Best we can do.. var choices = _pollService.GetChoices(pollId); if (choices.Count() >= 1) { // re-order the random shuffle, so the last in sequence is indeed the last in sequence choices = choices.OrderBy(c => c.Id).AsQueryable <PollChoiceRecord>(); part.PollChoiceRecord_Id = choices.Last().Id; } _songService.UpdateSong(part); var songContentItem = _contentManager.Get(part.Id, VersionOptions.Latest); var songPart = songContentItem.As <SongPart>(); var model = new SongViewModel(songPart); Services.Notifier.Information(T("Sangen er nu redigeret")); // return View("EditorTemplates/Parts/SongForm", model); // return View("Parts/Song", model); return(RedirectToAction("SongIndex", "Admin")); // var songViewModel = Shape.Parts_Song(part); // return new ShapeResult(this, songViewModel(typeof(SongViewModel))); //return Redirect("~/Afstemninger/Liste"); }