public async Task <IActionResult> Update(int ideaID, string comment)
        {
            var loggedInUserID = _userManager.GetUserId(HttpContext.User);

            var amendment = new Amendment();

            //Create the amendment
            amendment.IdeaID      = ideaID;
            amendment.Comment     = comment;
            amendment.UserID      = new Guid(loggedInUserID);
            amendment.DateCreated = DateTime.UtcNow;
            _context.Add(amendment);
            await _context.SaveChangesAsync();

            //Give the amendment author 100 participation points
            var loggedInUser = _context.Users.Where(user => user.Id == loggedInUserID).FirstOrDefault();

            loggedInUser.ParticipationPoints += 100;
            _context.Update(loggedInUser);
            await _context.SaveChangesAsync();

            //Get and return amendments
            //Retrieve amendments for submitted ideas
            List <AmendmentPresentationViewModel> amendmentViewModel = new List <AmendmentPresentationViewModel>();

            var amendments = _context.Amendment.Where(a => a.IdeaID == ideaID);

            foreach (Amendment amendmentVal in amendments)
            {
                ApplicationUser amendmentAuthor = _context.Users.Where(user => user.Id == amendment.UserID.ToString()).FirstOrDefault();

                var amendmentPresentation = new AmendmentPresentationViewModel
                {
                    AuthorFirstName = amendmentAuthor.FirstName,
                    AuthorLastName  = amendmentAuthor.LastName,
                    Comment         = amendmentVal.Comment,
                    PostingDate     = amendmentVal.DateCreated
                };

                amendmentViewModel.Add(amendmentPresentation);
            }
            return(PartialView("_AmendmentPartial", amendmentViewModel));
        }
        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));
        }