first push
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
using MovieActorLookup.Models;
|
||||
using MovieActorLookup.Models.Movies;
|
||||
using MovieActorLookup.Models.Movie.Movie;
|
||||
using RestSharp;
|
||||
using System.Text.Json;
|
||||
|
||||
public class MovieService
|
||||
{
|
||||
private readonly string _baseUrl;
|
||||
private readonly string _apiKey;
|
||||
private readonly string _bearerToken;
|
||||
|
||||
public MovieService(string baseUrl, string apiKey, string bearerToken)
|
||||
{
|
||||
_baseUrl = baseUrl;
|
||||
_apiKey = apiKey;
|
||||
_bearerToken = bearerToken;
|
||||
}
|
||||
|
||||
public async Task<List<Movie>> GetMoviesByTitle(string title)
|
||||
{
|
||||
var client = new RestClient("https://api.themoviedb.org/3/");
|
||||
|
||||
var request = new RestRequest("search/movie")
|
||||
.AddQueryParameter("query", title)
|
||||
.AddQueryParameter("include_adult", "false")
|
||||
.AddQueryParameter("language", "en-US")
|
||||
.AddQueryParameter("page", "1");
|
||||
|
||||
request.AddHeader("accept", "application/json");
|
||||
request.AddHeader("Authorization", $"Bearer {_bearerToken}");
|
||||
|
||||
var response = await client.GetAsync(request);
|
||||
|
||||
var movies = new List<Movie>();
|
||||
if (response.IsSuccessful)
|
||||
{
|
||||
var movieResponse = JsonSerializer.Deserialize<MovieResponse>(response.Content);
|
||||
if (movieResponse != null && movieResponse.Results.Any())
|
||||
{
|
||||
foreach (var apiMovie in movieResponse.Results)
|
||||
{
|
||||
var movie = new Movie
|
||||
{
|
||||
Id = apiMovie.Id,
|
||||
Title = apiMovie.Title,
|
||||
OriginalTitle = apiMovie.OriginalTitle,
|
||||
Overview = apiMovie.Overview,
|
||||
Adult = apiMovie.Adult,
|
||||
BackdropPath = apiMovie.BackdropPath != null ? $"https://image.tmdb.org/t/p/w500{apiMovie.BackdropPath}" : null,
|
||||
GenreIds = apiMovie.GenreIds,
|
||||
OriginalLanguage = apiMovie.OriginalLanguage,
|
||||
Popularity = apiMovie.Popularity,
|
||||
PosterPath = apiMovie.PosterPath != null ? $"https://image.tmdb.org/t/p/w500{apiMovie.PosterPath}" : null,
|
||||
ReleaseDate = apiMovie.ReleaseDate,
|
||||
Video = apiMovie.Video,
|
||||
VoteAverage = apiMovie.VoteAverage,
|
||||
VoteCount = apiMovie.VoteCount
|
||||
};
|
||||
movies.Add(movie);
|
||||
}
|
||||
}
|
||||
}
|
||||
return movies;
|
||||
}
|
||||
|
||||
public async Task<Credits> GetMovieCreditsByMovieId(int movieId)
|
||||
{
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest($"movie/{movieId}/credits")
|
||||
.AddQueryParameter("language", "en-US");
|
||||
|
||||
request.AddHeader("accept", "application/json");
|
||||
request.AddHeader("Authorization", $"Bearer {_bearerToken}");
|
||||
|
||||
var response = await client.GetAsync(request);
|
||||
|
||||
if (response.IsSuccessful)
|
||||
{
|
||||
var credits = JsonSerializer.Deserialize<Credits>(response.Content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
return credits;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Error retrieving movie credits: " + response.ErrorMessage);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ActorMovieFilmography> GetActorMovieCreditsById(int personId)
|
||||
{
|
||||
var client = new RestClient(_baseUrl);
|
||||
var request = new RestRequest($"person/{personId}/movie_credits")
|
||||
.AddQueryParameter("language", "en-US");
|
||||
|
||||
request.AddHeader("accept", "application/json");
|
||||
request.AddHeader("Authorization", $"Bearer {_bearerToken}");
|
||||
|
||||
var response = await client.GetAsync(request);
|
||||
|
||||
if (response.IsSuccessful)
|
||||
{
|
||||
// Deserialize the response into the ActorFilmography class
|
||||
var actorFilmography = JsonSerializer.Deserialize<ActorMovieFilmography>(response.Content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
return actorFilmography;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Error retrieving actor's movie credits: " + response.ErrorMessage);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Movie> GetMovieDetailsById(int movieId)
|
||||
{
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest($"movie/{movieId}")
|
||||
.AddQueryParameter("language", "en-US");
|
||||
|
||||
request.AddHeader("accept", "application/json");
|
||||
request.AddHeader("Authorization", $"Bearer {_bearerToken}");
|
||||
|
||||
var response = await client.GetAsync(request);
|
||||
|
||||
if (response.IsSuccessful)
|
||||
{
|
||||
var movie = JsonSerializer.Deserialize<Movie>(response.Content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
return movie;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Error retrieving movie details: " + response.ErrorMessage);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
using MovieActorLookup.Models;
|
||||
using MovieActorLookup.Models.TvShows;
|
||||
using RestSharp;
|
||||
using System.Text.Json;
|
||||
|
||||
public class ShowService
|
||||
{
|
||||
private readonly string _baseUrl;
|
||||
private readonly string _apiKey;
|
||||
private readonly string _bearerToken;
|
||||
|
||||
public ShowService(string baseUrl, string apiKey, string bearerToken)
|
||||
{
|
||||
_baseUrl = baseUrl;
|
||||
_apiKey = apiKey;
|
||||
_bearerToken = bearerToken;
|
||||
}
|
||||
|
||||
public async Task<List<TvShow>> GetShowsByTitle(string title)
|
||||
{
|
||||
var client = new RestClient("https://api.themoviedb.org/3/");
|
||||
var request = new RestRequest("search/tv")
|
||||
.AddQueryParameter("query", title)
|
||||
.AddQueryParameter("include_adult", "false")
|
||||
.AddQueryParameter("language", "en-US")
|
||||
.AddQueryParameter("page", "1");
|
||||
|
||||
request.AddHeader("accept", "application/json");
|
||||
request.AddHeader("Authorization", $"Bearer {_bearerToken}");
|
||||
|
||||
var response = await client.GetAsync(request);
|
||||
var shows = new List<TvShow>();
|
||||
|
||||
if (response.IsSuccessful)
|
||||
{
|
||||
var showResponse = JsonSerializer.Deserialize<TvResponse>(response.Content);
|
||||
if (showResponse != null && showResponse.Results.Any())
|
||||
{
|
||||
foreach (var show in showResponse.Results)
|
||||
{
|
||||
shows.Add(new TvShow
|
||||
{
|
||||
Id = show.Id,
|
||||
Name = show.Name,
|
||||
Overview = show.Overview,
|
||||
PosterPath = show.PosterPath != null ? $"https://image.tmdb.org/t/p/w500{show.PosterPath}" : null,
|
||||
BackdropPath = show.BackdropPath != null ? $"https://image.tmdb.org/t/p/w500{show.BackdropPath}" : null,
|
||||
FirstAirDate = show.FirstAirDate,
|
||||
Popularity = show.Popularity,
|
||||
VoteAverage = show.VoteAverage
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return shows;
|
||||
}
|
||||
|
||||
public async Task<Credits> GetShowCreditsById(int showId)
|
||||
{
|
||||
var client = new RestClient(_baseUrl);
|
||||
var request = new RestRequest($"tv/{showId}/credits")
|
||||
.AddQueryParameter("language", "en-US");
|
||||
|
||||
request.AddHeader("accept", "application/json");
|
||||
request.AddHeader("Authorization", $"Bearer {_bearerToken}");
|
||||
|
||||
var response = await client.GetAsync(request);
|
||||
|
||||
if (response.IsSuccessful)
|
||||
{
|
||||
return JsonSerializer.Deserialize<Credits>(response.Content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<MovieActorLookup.Models.TvShow.TvShow.ActorTvFilmography> GetActorTvCreditsById(int personId)
|
||||
{
|
||||
var client = new RestClient(_baseUrl);
|
||||
var request = new RestRequest($"person/{personId}/tv_credits")
|
||||
.AddQueryParameter("language", "en-US");
|
||||
|
||||
request.AddHeader("accept", "application/json");
|
||||
request.AddHeader("Authorization", $"Bearer {_bearerToken}");
|
||||
|
||||
var response = await client.GetAsync(request);
|
||||
|
||||
if (response.IsSuccessful)
|
||||
{
|
||||
return JsonSerializer.Deserialize<MovieActorLookup.Models.TvShow.TvShow.ActorTvFilmography>(response.Content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<TvShow> GetShowDetailsById(int showId)
|
||||
{
|
||||
var client = new RestClient(_baseUrl);
|
||||
var request = new RestRequest($"tv/{showId}")
|
||||
.AddQueryParameter("language", "en-US");
|
||||
|
||||
request.AddHeader("accept", "application/json");
|
||||
request.AddHeader("Authorization", $"Bearer {_bearerToken}");
|
||||
|
||||
var response = await client.GetAsync(request);
|
||||
|
||||
if (response.IsSuccessful)
|
||||
{
|
||||
return JsonSerializer.Deserialize<TvShow>(response.Content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user