Skip to content

Commit

Permalink
feat: When viewing a top billed tv series cast you can now get the ep…
Browse files Browse the repository at this point in the history
…isodeCount. Also improved how gender values are mapped to support all genders available.
  • Loading branch information
AlexMachin1997 committed Jun 30, 2024
1 parent 26ed478 commit 4df634f
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 88 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"source.fixAll.eslint": "explicit"
},
"editor.cursorSmoothCaretAnimation": "on",
"editor.fontSize": 30,
"editor.fontSize": 20,

"editor.minimap.enabled": false,
"editor.tabSize": 2,

Expand Down
25 changes: 12 additions & 13 deletions src/entertainment/entertainment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { firstValueFrom } from 'rxjs';

import {
ICreditsQueryResponse,
IKeywordsQueryResponse,
IReviewQuery,
IVdoesQueryResponse
} from './entertainment';
import {
BelongsToCollection,
Cast,
Crew,
ENTERTAINMENT_TYPES,
GENDER,
Keyword,
Review,
Social,
Video
} from 'src/graphql.schema';
} from '../graphql.schema';
import {
TheOpenMovieDatabaseBelongsToCollection,
TheOpenMovieDatabaseSpokenLanguages
} from 'src/movie/movie';
import { SocialsService } from 'src/socials/socials.service';
import { UtilsService } from 'src/utils/utils.service';

import {
ICreditsQueryResponse,
IKeywordsQueryResponse,
IReviewQuery,
IVdoesQueryResponse
} from './entertainment';
} from '../movie/movie';
import { SocialsService } from '../socials/socials.service';
import { UtilsService } from '../utils/utils.service';

// eslint-disable-next-line @typescript-eslint/naming-convention
interface IEntertainmentCommonArguments {
Expand Down Expand Up @@ -109,7 +108,7 @@ export class EntertainmentService {
id: el.id,
character: el.character,
profileImageUrl: this.utilService.getFullImageUrlPath(el.profile_path),
gender: el.gender === 2 ? GENDER.MALE : GENDER.FEMALE
gender: this.utilService.getGender(el.gender)
}));
}

Expand Down
25 changes: 2 additions & 23 deletions src/entertainment/entertainment.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Keyword } from '../graphql.schema';

export interface ICastAndCrew {
adult: boolean;
gender: number;
id: number;
known_for_department: string;
name: string;
original_name: string;
popularity: number;
profile_path: string;
credit_id: string;
}

export interface ICast extends ICastAndCrew {
cast_id: number;
character: string;
order: number;
}

export interface ICrew extends ICastAndCrew {
department: string;
job: string;
}
import { Keyword } from '../graphql.schema';
import { ICast, ICrew } from '../types/credits';

export interface IReviewQuery {
id: number;
Expand Down
9 changes: 1 addition & 8 deletions src/graphql.schema.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@

/*
* -------------------------------------------------------
* THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
* -------------------------------------------------------
*/

/* tslint:disable */
/* eslint-disable */

export enum GENDER {
MALE = "MALE",
FEMALE = "FEMALE"
}

export enum ENTERTAINMENT_TYPES {
MOVIE = "MOVIE",
TV = "TV"
Expand Down Expand Up @@ -165,7 +158,7 @@ export interface Cast {
id?: Nullable<number>;
character?: Nullable<string>;
profileImageUrl?: Nullable<string>;
gender?: Nullable<GENDER>;
gender?: Nullable<string>;
episodeCount?: Nullable<number>;
}

Expand Down
2 changes: 1 addition & 1 deletion src/models/entertainment/Cast.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ type Cast {
id: Int
character: String
profileImageUrl: String
gender: GENDER
gender: String
episodeCount: Int
}
5 changes: 0 additions & 5 deletions src/models/enum.graphql
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
enum GENDER {
MALE
FEMALE
}

enum ENTERTAINMENT_TYPES {
MOVIE
TV
Expand Down
3 changes: 2 additions & 1 deletion src/movie/movie.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Genre, Keyword } from '../graphql.schema';
import { Gender } from '../types/gender';

export interface TheOpenMovieDatabaseBelongsToCollection {
id: number;
Expand Down Expand Up @@ -59,7 +60,7 @@ export interface TheOpenMovieDatabaseMovieReview {

export interface TheOpenMovieDatabaseMovieCastAndCrew {
adult: boolean;
gender: number;
gender: Gender;
id: number;
known_for_department: string;
name: string;
Expand Down
29 changes: 2 additions & 27 deletions src/person/person.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
Group,
IGetGroupCredits,
TheOpenMovieDatabasePerson,
TheOpenMovieDatabasePersonCombinedCredits,
TheOpenMovieDatabasePersonGender
TheOpenMovieDatabasePersonCombinedCredits
} from './person';
import { SocialsService } from '../socials/socials.service';
import { UtilsService } from '../utils/utils.service';
Expand Down Expand Up @@ -42,30 +41,6 @@ export class PersonService {
return `${birthdayInReadableFormat} (${age} years old)`;
}

getGender(gender: TheOpenMovieDatabasePersonGender) {
switch (gender) {
case 0: {
return 'Not set / not specified';
}

case 1: {
return 'Female';
}

case 2: {
return 'Male';
}

case 3: {
return 'Non-bindary';
}

default: {
return '';
}
}
}

async getPerson(personId: number) {
const { data } = await firstValueFrom(
this.httpService.get<TheOpenMovieDatabasePerson>(
Expand All @@ -87,7 +62,7 @@ export class PersonService {
knowForDepartment: data.known_for_department,
name: data.name,
alsoKnownAs: data.also_known_as,
gender: this.getGender(data.gender),
gender: this.utilService.getGender(data.gender),
overview: data.biography,
placeOfBirth: data.place_of_birth,
posterUrl: this.utilService.getFullImageUrlPath(data.profile_path),
Expand Down
4 changes: 2 additions & 2 deletions src/person/person.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
export type TheOpenMovieDatabasePersonGender = 0 | 1 | 2 | 3;
import { Gender } from '../types/gender';

export type TheOpenMovieDatabaseDepartments =
| 'Production'
Expand All @@ -14,7 +14,7 @@ export interface TheOpenMovieDatabasePerson {
biography: string;
birthday: string;
deathday: null | string;
gender: TheOpenMovieDatabasePersonGender;
gender: Gender;
homepage: string;
id: number;
imdb_id: string;
Expand Down
50 changes: 48 additions & 2 deletions src/show/show.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { firstValueFrom } from 'rxjs';

import { TheOpenMovieDatabaseShow } from './show';
import { IAggregatedCreditsQueryResponse, TheOpenMovieDatabaseShow } from './show';
import { EntertainmentService } from '../entertainment/entertainment.service';
import { CurrentSeason, ENTERTAINMENT_TYPES } from '../graphql.schema';
import { UtilsService } from '../utils/utils.service';
Expand Down Expand Up @@ -89,11 +89,57 @@ export class ShowService {
});
}

async getCastAggregatedCredits(showId: number) {
const { data } = await firstValueFrom(
this.httpService.get<IAggregatedCreditsQueryResponse>(
`https://api.themoviedb.org/3/tv/${showId}/aggregate_credits?language=en-U`,
{
headers: {
Accept: 'application/json',
Authorization:
// eslint-disable-next-line max-len
'Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI1NDMwNWQxNmE1ZThkN2E3ZWMwZmM2NTk5MzZiY2EzMCIsInN1YiI6IjViMzE0MjQ1OTI1MTQxM2M5MTAwNTIwNCIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.iqdLKFCSgeWG3SYso7Rqj297FORviPf9hDdn2kKygTA'
}
}
)
);

return data;
}

async getTopBilledCast(showId: number) {
return this.entertainmentService.getTopBilledCast({
// Get the top billed cast members
const topBilledCast = await this.entertainmentService.getTopBilledCast({
entertainmentId: showId,
entertainmentType: ENTERTAINMENT_TYPES.TV
});

// Since the episode count isn't available via getTopBilledCast perform an additional lookup to get the additional credit
// data like a list of the users roles for the particular show being queried
const aggregatedCredits = await this.getCastAggregatedCredits(showId);

return topBilledCast?.map((topBilledCastMember) => {
// Find the cast members credits data, found via the aggregate_credits endpoint
const aggregatedCredit = aggregatedCredits.cast.find(
(el) => el.id === topBilledCastMember.id
);

// If the aggregatedCredit is available then total up the cast members episode count
if (typeof aggregatedCredit !== 'undefined') {
return {
...topBilledCastMember,
episodeCount: aggregatedCredit.roles.reduce(
(total, role) => total + role.episode_count,
0
)
};
}

return {
...topBilledCastMember,
episodeCount: 0
};
});
}

async getFeaturedCrewMembers(showId: number) {
Expand Down
28 changes: 25 additions & 3 deletions src/show/show.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { TheOpenMovieDatabaseBelongsToCollection } from 'src/movie/movie';

/* eslint-disable @typescript-eslint/naming-convention */
import { Genre } from '../graphql.schema';
import { TheOpenMovieDatabaseBelongsToCollection } from '../movie/movie';
import { ICast, ICrew } from '../types/credits';
import { Gender } from '../types/gender';

export interface TheOpenMovieDatabaseEpisodeToAir {
id: number;
Expand Down Expand Up @@ -34,7 +36,7 @@ export interface TheOpenMovieDatabaseShow {
credit_id: string;
name: string;
original_name: string;
gender: number;
gender: Gender;
profile_path: string;
}[];
episode_run_time: number[];
Expand Down Expand Up @@ -80,3 +82,23 @@ export interface TheOpenMovieDatabaseShow {
vote_average: number;
vote_count: number;
}

interface IAggregatedCreditJob {
credit_id: string;
job: string;
episode_count: number;
}

interface IAggregatedCreditsQueryCastResponse extends ICast {
roles: IAggregatedCreditJob[];
}

interface IAggregatedCreditsQueryCrewResponse extends ICrew {
roles: IAggregatedCreditJob[];
}

export interface IAggregatedCreditsQueryResponse {
id: number;
cast: IAggregatedCreditsQueryCastResponse[];
crew: IAggregatedCreditsQueryCrewResponse[];
}
2 changes: 0 additions & 2 deletions src/socials/socials.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export class SocialsService {
)
);

console.log('social', data);

let facebook = null;
let instagram = null;
let twitter = null;
Expand Down
25 changes: 25 additions & 0 deletions src/types/credits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Gender } from './gender';

export interface ICastAndCrew {
adult: boolean;
gender: Gender;
id: number;
known_for_department: string;
name: string;
original_name: string;
popularity: number;
profile_path: string;
credit_id: string;
}

export interface ICast extends ICastAndCrew {
cast_id: number;
character: string;
order: number;
}

export interface ICrew extends ICastAndCrew {
department: string;
job: string;
}
1 change: 1 addition & 0 deletions src/types/gender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Gender = 0 | 1 | 2 | 3;
22 changes: 22 additions & 0 deletions src/utils/utils.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Injectable } from '@nestjs/common';

import { Gender } from '../types/gender';

@Injectable()
export class UtilsService {
getFullImageUrlPath(imageUrl: string | null | undefined = null): string {
Expand All @@ -24,4 +26,24 @@ export class UtilsService {
currency: 'GBP'
});
}

getGender(gender: Gender) {
switch (gender) {
case 1: {
return 'Female';
}

case 2: {
return 'Male';
}

case 3: {
return 'Non-binary';
}

default: {
return 'Not set / not specified';
}
}
}
}

0 comments on commit 4df634f

Please sign in to comment.