public ActionResult <List <MovieWithVoteCount> > GetListWithVotes() { List <Vote> votes = _context.Votes.ToList(); List <Movie> movies = _context.Movies.ToList(); List <MovieWithVoteCount> resultList = new List <MovieWithVoteCount>(); foreach (Movie movie in movies) { var upVoteCount = votes.Where(x => x.MovieId.Equals(movie.Id) && x.UpVote.Equals(true)).ToList().Count(); var downVoteCount = votes.Where(x => x.MovieId.Equals(movie.Id) && x.DownVote.Equals(true)).ToList().Count(); var sum = upVoteCount - downVoteCount; MovieWithVoteCount movieWithVoteCount = new MovieWithVoteCount { MovieTitle = movie.Title, Movie = movie, DownVoteCount = downVoteCount, UpVoteCount = upVoteCount, VoteSum = sum }; resultList.Add(movieWithVoteCount); } return(resultList.OrderByDescending(x => x.VoteSum).ToList()); }
public async Task <ActionResult <List <MovieWithVoteCount> > > GetListWithVotesAndMovieInfo() { var user = await _userManager.GetUserAsync(HttpContext.User); List <Vote> votes = _context.Votes.ToList(); List <Movie> movies = _context.Movies.ToList(); List <MovieWithVoteCount> resultList = new List <MovieWithVoteCount>(); foreach (Movie movie in movies) { bool currentUserUpVoted = false; bool currentUserDownVoted = false; var myVoteForThisMovie = votes.Where(x => x.MovieId.Equals(movie.Id) && x.UserId.Equals(user.Id)).ToList().FirstOrDefault(); if (myVoteForThisMovie != null) { if (myVoteForThisMovie.UpVote) { currentUserUpVoted = true; } if (myVoteForThisMovie.DownVote) { currentUserDownVoted = true; } } var upVoteCount = votes.Where(x => x.MovieId.Equals(movie.Id) && x.UpVote.Equals(true)).ToList().Count(); var downVoteCount = votes.Where(x => x.MovieId.Equals(movie.Id) && x.DownVote.Equals(true)).ToList().Count(); var sum = upVoteCount - downVoteCount; OMDBInfo movieInfo = null; // If we have stored ID, use that. if (!string.IsNullOrEmpty(movie.imdbID)) { movieInfo = await GetMovieInfoById(movie.imdbID); } else { movieInfo = await GetMovieInfo(movie.Title); } MovieWithVoteCount movieWithVoteCount = new MovieWithVoteCount { CurrentUserUpVoted = currentUserUpVoted, CurrentUserDownVoted = currentUserDownVoted, MovieTitle = movie.Title, Movie = movie, DownVoteCount = downVoteCount, UpVoteCount = upVoteCount, VoteSum = sum, MovieInfo = movieInfo }; resultList.Add(movieWithVoteCount); } return(resultList.OrderByDescending(x => x.VoteSum).ToList()); }