Person API calls (#188)
* feat(frontend): person API call - details, combined credits * feat(frontend): add next for error handling + remove conditional * feat(frontend): add status code to next errorpull/190/head
parent
a28a8b37b0
commit
2aefcfdfb9
@ -0,0 +1,131 @@
|
||||
import {
|
||||
TmdbPersonCreditCast,
|
||||
TmdbPersonCreditCrew,
|
||||
TmdbPersonDetail,
|
||||
} from '../api/themoviedb';
|
||||
|
||||
export interface PersonDetail {
|
||||
id: number;
|
||||
name: string;
|
||||
deathday: string;
|
||||
knownForDepartment: string;
|
||||
alsoKnownAs?: string[];
|
||||
gender: number;
|
||||
biography: string;
|
||||
popularity: string;
|
||||
placeOfBirth?: string;
|
||||
profilePath?: string;
|
||||
adult: boolean;
|
||||
imdbId?: string;
|
||||
homepage?: string;
|
||||
}
|
||||
|
||||
export interface PersonCredit {
|
||||
id: number;
|
||||
originalLanguage: string;
|
||||
episodeCount: number;
|
||||
overview: string;
|
||||
originCountry: string[];
|
||||
originalName: string;
|
||||
voteCount: number;
|
||||
name: string;
|
||||
mediaType?: string;
|
||||
popularity: number;
|
||||
creditId: string;
|
||||
backdropPath?: string;
|
||||
firstAirDate: string;
|
||||
voteAverage: number;
|
||||
genreIds?: number[];
|
||||
posterPath?: string;
|
||||
originalTitle: string;
|
||||
video?: boolean;
|
||||
title: string;
|
||||
adult: boolean;
|
||||
releaseDate: string;
|
||||
}
|
||||
|
||||
export interface PersonCreditCast extends PersonCredit {
|
||||
character: string;
|
||||
}
|
||||
|
||||
export interface PersonCreditCrew extends PersonCredit {
|
||||
department: string;
|
||||
job: string;
|
||||
}
|
||||
|
||||
export interface CombinedCredit {
|
||||
id: number;
|
||||
cast: PersonCreditCast[];
|
||||
crew: PersonCreditCrew[];
|
||||
}
|
||||
|
||||
export const mapPersonDetails = (person: TmdbPersonDetail): PersonDetail => ({
|
||||
id: person.id,
|
||||
name: person.name,
|
||||
deathday: person.deathday,
|
||||
knownForDepartment: person.known_for_department,
|
||||
alsoKnownAs: person.also_known_as,
|
||||
gender: person.gender,
|
||||
biography: person.biography,
|
||||
popularity: person.popularity,
|
||||
placeOfBirth: person.place_of_birth,
|
||||
profilePath: person.profile_path,
|
||||
adult: person.adult,
|
||||
imdbId: person.imdb_id,
|
||||
homepage: person.homepage,
|
||||
});
|
||||
|
||||
export const mapCastCredits = (
|
||||
cast: TmdbPersonCreditCast
|
||||
): PersonCreditCast => ({
|
||||
id: cast.id,
|
||||
originalLanguage: cast.original_language,
|
||||
episodeCount: cast.episode_count,
|
||||
overview: cast.overview,
|
||||
originCountry: cast.origin_country,
|
||||
originalName: cast.original_name,
|
||||
voteCount: cast.vote_count,
|
||||
name: cast.name,
|
||||
mediaType: cast.media_type,
|
||||
popularity: cast.popularity,
|
||||
creditId: cast.credit_id,
|
||||
backdropPath: cast.backdrop_path,
|
||||
firstAirDate: cast.first_air_date,
|
||||
voteAverage: cast.vote_average,
|
||||
genreIds: cast.genre_ids,
|
||||
posterPath: cast.poster_path,
|
||||
originalTitle: cast.original_title,
|
||||
video: cast.video,
|
||||
title: cast.title,
|
||||
adult: cast.adult,
|
||||
releaseDate: cast.release_date,
|
||||
character: cast.character,
|
||||
});
|
||||
|
||||
export const mapCrewCredits = (
|
||||
crew: TmdbPersonCreditCrew
|
||||
): PersonCreditCrew => ({
|
||||
id: crew.id,
|
||||
originalLanguage: crew.original_language,
|
||||
episodeCount: crew.episode_count,
|
||||
overview: crew.overview,
|
||||
originCountry: crew.origin_country,
|
||||
originalName: crew.original_name,
|
||||
voteCount: crew.vote_count,
|
||||
name: crew.name,
|
||||
mediaType: crew.media_type,
|
||||
popularity: crew.popularity,
|
||||
creditId: crew.credit_id,
|
||||
backdropPath: crew.backdrop_path,
|
||||
firstAirDate: crew.first_air_date,
|
||||
voteAverage: crew.vote_average,
|
||||
genreIds: crew.genre_ids,
|
||||
posterPath: crew.poster_path,
|
||||
originalTitle: crew.original_title,
|
||||
video: crew.video,
|
||||
title: crew.title,
|
||||
adult: crew.adult,
|
||||
releaseDate: crew.release_date,
|
||||
department: crew.department,
|
||||
job: crew.job,
|
||||
});
|
@ -0,0 +1,43 @@
|
||||
import { Router } from 'express';
|
||||
import next from 'next';
|
||||
import TheMovieDb from '../api/themoviedb';
|
||||
import logger from '../logger';
|
||||
import {
|
||||
mapCastCredits,
|
||||
mapCrewCredits,
|
||||
mapPersonDetails,
|
||||
} from '../models/Person';
|
||||
|
||||
const personRoutes = Router();
|
||||
|
||||
personRoutes.get('/:id', async (req, res, next) => {
|
||||
const tmdb = new TheMovieDb();
|
||||
|
||||
try {
|
||||
const person = await tmdb.getPerson({
|
||||
personId: Number(req.params.id),
|
||||
language: req.query.language as string,
|
||||
});
|
||||
return res.status(200).json(mapPersonDetails(person));
|
||||
} catch (e) {
|
||||
logger.error(e.message);
|
||||
next({ status: 404, message: 'Person not found' });
|
||||
}
|
||||
});
|
||||
|
||||
personRoutes.get('/:id/combined_credits', async (req, res) => {
|
||||
const tmdb = new TheMovieDb();
|
||||
|
||||
const combinedCredits = await tmdb.getPersonCombinedCredits({
|
||||
personId: Number(req.params.id),
|
||||
language: req.query.language as string,
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
cast: combinedCredits.cast.map((result) => mapCastCredits(result)),
|
||||
crew: combinedCredits.crew.map((result) => mapCrewCredits(result)),
|
||||
id: combinedCredits.id,
|
||||
});
|
||||
});
|
||||
|
||||
export default personRoutes;
|
Loading…
Reference in new issue