private async Task <OMDBInfo> GetMovieInfo(string movieTitle) { OMDBInfo result = new OMDBInfo(); try { // Fetch movie info using http://www.omdbapi.com (patreon subscription required to get api key // which is required for poster images) // Example http://www.omdbapi.com/?t=Speed&apikey=YOUR_API_KEY_HERE client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("User-Agent", "CultureCatchup.fun"); var apiUrl = "http://www.omdbapi.com/?t=" + movieTitle + "&apikey=" + _configuration["ApiKeys:OMDBApiKey"]; string movieInfoString = await client.GetStringAsync(apiUrl); result = JsonConvert.DeserializeObject <OMDBInfo>(movieInfoString); } catch (Exception e) { _logger.LogError("Error using OMBD API", e); _logger.LogError(e.ToString()); } return(result); }
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()); }