public async Task <bool> TrackStatus(int ideaID, bool tracked) { //Retrieve the logged in user's information and idea interaction var loggedInUserID = _userManager.GetUserId(HttpContext.User); var currentUserInteraction = _context.IdeaInteraction.Where( interaction => interaction.UserId.ToString() == loggedInUserID && interaction.IdeaID == ideaID ).FirstOrDefault(); //if there is no interaction, create one if (currentUserInteraction == null) { currentUserInteraction = new IdeaInteraction { IdeaID = ideaID, UserId = new Guid(loggedInUserID), IsTracked = tracked }; _context.IdeaInteraction.Add(currentUserInteraction); } else //update interaction { currentUserInteraction.IsTracked = tracked; _context.IdeaInteraction.Update(currentUserInteraction); } await _context.SaveChangesAsync(); return(true); }
public void CreateIdeaInteraction(IdeaInteraction cmd) { if (cmd == null) { throw new ArgumentNullException(nameof(cmd)); } _access.IdeaInteractions.Add(cmd); }
public async Task <IActionResult> Rate(int ideaID, int rating) { //Get the average rating and if it is tracked var loggedInUserID = _userManager.GetUserId(HttpContext.User); var currentUserInteraction = _context.IdeaInteraction.Where( interaction => interaction.UserId.ToString() == loggedInUserID && interaction.IdeaID == ideaID ).FirstOrDefault(); //If there is no interaction, create one and give points if (currentUserInteraction == null) { currentUserInteraction = new IdeaInteraction { IdeaID = ideaID, UserId = new Guid(loggedInUserID), IsTracked = false, Rating = rating }; _context.Add(currentUserInteraction); await _context.SaveChangesAsync(); //Give particiption points var loggedInUser = _context.Users.Where(user => user.Id == loggedInUserID).FirstOrDefault(); loggedInUser.ParticipationPoints += 50; _context.Update(loggedInUser); _context.SaveChanges(); } else { //If the original rating is null, give the rater participation points if (!(currentUserInteraction.Rating <= 5 && currentUserInteraction.Rating >= 1)) { var loggedInUser = _context.Users.Where(user => user.Id == loggedInUserID).FirstOrDefault(); loggedInUser.ParticipationPoints += 50; _context.Update(loggedInUser); await _context.SaveChangesAsync(); } currentUserInteraction.Rating = rating; _context.Update(currentUserInteraction); await _context.SaveChangesAsync(); } //Calculate the new average rating var ideaInteractions = _context.IdeaInteraction.Where(i => i.IdeaID == ideaID).ToList(); double avgRating = Math.Round(ideaInteractions.Where(i => i.Rating != 0).Select(i => i.Rating).Average(), 1); //Return the new average rating for idea return(Json(avgRating)); }
public async Task <IActionResult> Index(string filterType) { //Create a list to store ideas IQueryable <Idea> ideaQuery; //Create basic model for the ideas to show. Initially set to have no rows List <IdeaPresentationViewModel> ideaViewModel = new List <IdeaPresentationViewModel>(); //Retrieve the logged in user's information var loggedInUserID = _userManager.GetUserId(HttpContext.User); ApplicationUser loggedInUser = _context.Users.Where(user => user.Id == loggedInUserID).FirstOrDefault(); //Retrieve the logged in user's unit information Unit loggedInUserUnit = _context.Unit.Where(unit => unit.ID == loggedInUser.UnitID).FirstOrDefault(); //Create a dictionary to store user information Dictionary <Guid, ApplicationUser> userDictionary = new Dictionary <Guid, ApplicationUser>(); //Create a list to store the filtered idea List <Idea> filteredIdeas = new List <Idea>(); //Determine which ideas the user wants to see switch (filterType) { case "drafts": //Get a model that filters on the user's drafts ideaQuery = _context.Idea.Where(idea => idea.IsDraft && idea.UserID.ToString() == loggedInUserID); //Name the page appropriately ViewBag.PageName = "Drafts"; ViewBag.filterType = "Drafts"; ViewBag.IsDraft = true; break; default: //Get a model that filters on the user's ideas ideaQuery = _context.Idea.Where(idea => !idea.IsDraft && idea.UnitID == loggedInUserUnit.ID); //Name the page appropriately ViewBag.PageName = "Ideas"; ViewBag.filterType = "Ideas"; ViewBag.IsDraft = false; break; } filteredIdeas = ideaQuery.ToList(); //Create the idea presentation viewmodel foreach (Idea idea in filteredIdeas) { //If the user dictionary does not have the idea author, add it if (!userDictionary.ContainsKey(idea.UserID)) { ApplicationUser ideaAuthor = await _context.Users.Where(user => user.Id == idea.UserID.ToString()).FirstOrDefaultAsync(); userDictionary.Add(idea.UserID, ideaAuthor); } //Retrieve amendments for submitted ideas List <AmendmentPresentationViewModel> amendmentViewModel = new List <AmendmentPresentationViewModel>(); //Drafts cannot possibly have amendments yet if (filterType != "drafts") { var amendments = _context.Amendment.Where(amendment => amendment.IdeaID == idea.ID); foreach (Amendment amendment in amendments) { //If the user dictionary does not have the amendment author, add it if (!userDictionary.ContainsKey(amendment.UserID)) { ApplicationUser amendmentAuthor = await _context.Users.Where(user => user.Id == amendment.UserID.ToString()).FirstOrDefaultAsync(); userDictionary.Add(amendment.UserID, amendmentAuthor); } var amendmentPresentation = new AmendmentPresentationViewModel { AuthorFirstName = userDictionary[amendment.UserID].FirstName, AuthorLastName = userDictionary[amendment.UserID].LastName, Comment = amendment.Comment, PostingDate = amendment.DateCreated }; amendmentViewModel.Add(amendmentPresentation); } } //Get the average rating and if it is tracked var ideaInteractions = _context.IdeaInteraction.Where(i => i.IdeaID == idea.ID).ToList(); var currentUserInteraction = ideaInteractions.Where(interaction => interaction.UserId.ToString() == loggedInUserID).FirstOrDefault(); //if there is no interaction, create one if (currentUserInteraction == null) { currentUserInteraction = new IdeaInteraction { IdeaID = idea.ID, UserId = new Guid(loggedInUserID), IsTracked = false }; _context.Add(currentUserInteraction); _context.SaveChanges(); } double avgRating = ideaInteractions.Where(i => i.Rating != 0).Count() == 0 ? -1 : Math.Round(ideaInteractions.Where(i => i.Rating != 0).Select(i => i.Rating).Average(), 1); //Create the idea presentation var ideaPresentation = new IdeaPresentationViewModel { Overview = idea, AverageRating = avgRating, UserRating = currentUserInteraction.Rating, IsTracked = currentUserInteraction.IsTracked, AuthorFirstName = userDictionary[idea.UserID].FirstName, AuthorLastName = userDictionary[idea.UserID].LastName, UnitName = loggedInUserUnit.Name, Amendments = amendmentViewModel }; ideaViewModel.Add(ideaPresentation); } //Send the model to the view and return the view return(View(ideaViewModel)); }
public void UpdateIdeaInteraction(IdeaInteraction IdeaInteraction) { _access.Update(IdeaInteraction); }
public void DeleteIdeaInteraction(IdeaInteraction IdeaInteraction) { _access.Remove(IdeaInteraction); }