Skip to content

Commit

Permalink
refactor(server): movie/show discovery cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ADRFranklin committed Jun 15, 2024
1 parent f370f2e commit dd8f26d
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 53 deletions.
8 changes: 4 additions & 4 deletions server/src/api/routes/movie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ const getMovieDiscovery = async (ctx: Context) => {
const service = getFromContainer(MovieService);
const mapper = getFromContainer(MovieMapper);

const { page } = (ctx.request.body as any) || 1;
const { with_companies } = ctx.query;
const { page, params } = (ctx.request.body as any) || 1;
const { with_companies } = params;

const results = await service.getDiscover({
page,
limit: 30,
withCompanies: with_companies
filterByCompanyId: with_companies
? parseInt(with_companies as string)
: undefined,
});
Expand All @@ -70,7 +70,7 @@ export default (app: Router) => {
route.get('/lookup/:id', lookupById);
route.get('/lookup/:id/minified', lookupByIdMinified);
route.post('/discover', getMovieDiscovery);
route.get('/company/:id', getCompanyById);
route.get('/studio/:id', getCompanyById);

app.use(route.routes());
};
4 changes: 2 additions & 2 deletions server/src/api/routes/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ const discoverSeriesData = async (ctx: Context) => {
const service = getFromContainer(ShowService);
const mapper = getFromContainer(ShowMapper);

const { page } = (ctx.request.body as any) || 1;
const { with_networks } = ctx.query;
const { page, params } = (ctx.request.body as any) || 1;
const { with_networks } = params;

const results = await service.getDiscover({
page,
Expand Down
5 changes: 5 additions & 0 deletions server/src/resources/movie/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ export class MovieMapper
name: copy.collections.name,
}
: undefined,
studio: {
id: copy.studios[0]?.providers?.tmdb?.id?.toString() || '0',
name: copy.studios[0].name,
logo_path: copy.studios[0].logoPath,
},
collection: copy.collections?.movies.map((movie) => ({
id: `${movie.providers?.plex?.id || movie.providers?.tmdb?.id || 0}`,
name: movie.name,
Expand Down
5 changes: 5 additions & 0 deletions server/src/resources/movie/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export type MovieResponseProps = {
belongs_to_collection?: {
name: string;
};
studio?: {
id: string;
name: string;
logo_path: string;
};
collection?: Array<{
id: string;
name: string;
Expand Down
16 changes: 16 additions & 0 deletions server/src/resources/network/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ export class NetworkEntity extends BaseEntity<NetworkProps> {
return new NetworkEntity({ id, props });
}

get artwork(): { logo?: { url: string; source: string } } {
return this.props.artwork;
}

get name(): string {
return this.props.name;
}

get providers(): { tmdbId: number } {
return this.props.providers;
}

get source(): string {
return this.props.source;
}

/**
* Validates the Network entity.
*/
Expand Down
4 changes: 2 additions & 2 deletions server/src/resources/network/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class NetworkMapper
source: 'tmdb',
},
},
provider: {
providers: {
tmdbId: record.id,
},
source: 'tmdb',
Expand All @@ -60,7 +60,7 @@ export class NetworkMapper
toResponse(entity: NetworkEntity): NetworkResponseProps {
const copy = entity.getProps();
return {
id: copy.provider.tmdbId,
id: copy.providers.tmdbId,
name: copy.name,
logo_path: copy.artwork.logo?.url || '',
};
Expand Down
2 changes: 1 addition & 1 deletion server/src/resources/network/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type NetworkProps = {
source: string;
};
};
provider: {
providers: {
tmdbId: number;
};
source: string;
Expand Down
54 changes: 30 additions & 24 deletions server/src/services/cache/cache-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Bluebird from 'bluebird';
import { Service } from 'diod';
import pino from 'pino';

import { Logger } from '@/infrastructure/logger/logger';
import is from '@/infrastructure/utils/is';
import { CompanyEntity } from '@/resources/company/entity';
import { MovieEntity } from '@/resources/movie/entity';
import { NetworkEntity } from '@/resources/network/entity';
Expand All @@ -18,6 +20,8 @@ import { NetworkService } from '@/services/network/network-service';
import { PersonService } from '@/services/person/person-service';
import { ShowService } from '@/services/show/show-service';

import { providers } from '../notifications/providers/index';

@Service()
export class CacheService {
/**
Expand Down Expand Up @@ -67,37 +71,39 @@ export class CacheService {
await this.cacheProvider.wrap<CommonResourcesCacheResponse>(
'resources.common',
async () => {
const [
movies,
shows,
people,
networks,
companies,
showDiscovery,
movieDiscovery,
] = await Promise.all([
this.movieService.getTrending(30),
this.showService.getTrending(30),
this.personService.getTrending(30),
this.networkService.getNetworks(),
this.companyService.getCompanies(),
this.showService.getDiscover({
page: 1,
limit: 30,
}),
this.movieService.getDiscover({
page: 1,
limit: 30,
}),
const [movies, shows, people, networks, companies] =
await Promise.all([
this.movieService.getTrending(30),
this.showService.getTrending(30),
this.personService.getTrending(30),
this.networkService.getNetworks(),
this.companyService.getCompanies(),
]);
const [showDiscovery, movieDiscovery] = await Bluebird.all([
Bluebird.map(networks, async (network) =>
this.showService.getDiscover({
filterByNetworkId: network.providers.tmdbId,
}),
),
Bluebird.map(companies, async (company) =>
this.movieService.getDiscover({
filterByCompanyId: parseInt(company.id, 10),
}),
),
]);

return {
movies: movies.map((movie) => movie.getProps()),
shows: shows.map((show) => show.getProps()),
people: people.map((person) => person.getProps()),
networks: networks.map((network) => network.getProps()),
companies: companies.map((company) => company.getProps()),
showDiscovery: showDiscovery.map((show) => show.getProps()),
movieDiscovery: movieDiscovery.map((movie) => movie.getProps()),
showDiscovery: showDiscovery
.flat()
.map((show) => show.getProps()),
movieDiscovery: movieDiscovery
.flat()
.map((movie) => movie.getProps()),
};
},
this.defaultCacheTTL,
Expand Down
7 changes: 5 additions & 2 deletions server/src/services/movie/movie-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,15 @@ export class MovieService {
*/
async getDiscover(options?: MovieDiscoverOptions): Promise<MovieEntity[]> {
try {
const optionsAsString =
options && Object.keys(options).length ? toQueryString(options) : '';
console.log(optionsAsString);
const results = await this.cacheProvider.wrap(
`movie.discover`,
`movie.discover${optionsAsString}`,
async () => {
const discovery = await this.discoverProvider.getDiscover({
page: options?.page || 1,
withCompanyId: options?.withCompanies,
withCompanyId: options?.filterByCompanyId,
});
if (discovery.isErr()) {
return [];
Expand Down
1 change: 1 addition & 0 deletions server/src/services/movie/provider/tmdb/tmdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ export class TmdbMovieProvider
try {
const optionsAsString =
options && Object.keys(options).length ? toQueryString(options) : '';
console.log(optionsAsString);
const results = await this.cache.wrap(
`tmdb.movie.discover${optionsAsString}`,
async () => {
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/movie/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export type MovieLookupOptions = {
export type MovieDiscoverOptions = {
page?: number;
limit?: number;
withCompanies?: number;
filterByCompanyId?: number;
};
35 changes: 19 additions & 16 deletions server/src/services/network/network-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Service } from 'diod';
import { None, Ok, Option, Some } from 'oxide.ts';
import pino from 'pino';

import { Logger } from '@/infrastructure/logger/logger';
Expand All @@ -7,7 +8,6 @@ import { NetworkProps } from '@/resources/network/types';
import { CacheProvider } from '@/services/cache/cache-provider';
import { NetworkDetailsProvider } from '@/services/network/provider/provider';
import { FixedNetworkIdsList } from '@/services/network/types';
import { None, Ok, Option, Some } from 'oxide.ts';

@Service()
export class NetworkService {
Expand All @@ -29,20 +29,23 @@ export class NetworkService {
async getNetworkDetails(id: number): Promise<Option<NetworkEntity>> {
try {
const start = Date.now();
const details = await this.cacheProvider.wrap<NetworkProps | undefined>(`network.${id}`, async () => {
const results = await this.networkDetailsProvider.getDetails(id);
if (!results.isOk()) {
return undefined;
}
const network = results.unwrap();
return {
...network,
provider: {
...network.provider,
tmdbId: id,
},
};
});
const details = await this.cacheProvider.wrap<NetworkProps | undefined>(
`network.${id}`,
async () => {
const results = await this.networkDetailsProvider.getDetails(id);
if (!results.isOk()) {
return undefined;
}
const network = results.unwrap();
return {
...network,
provider: {
...network.providers,
tmdbId: id,
},
};
},
);
if (!details) {
return None;
}
Expand Down Expand Up @@ -79,7 +82,7 @@ export class NetworkService {
);
return networks.map((network) => {
this.logger.debug(
{ networkId: network.provider.tmdbId, name: network.name },
{ networkId: network.providers.tmdbId, name: network.name },
`got network details`,
);
return NetworkEntity.create(network);
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/network/provider/tmdb/tmdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class NetworkTmdbProvider implements NetworkDetailsProvider {
}
: undefined,
},
provider: {
providers: {
tmdbId: response.id!,
},
source: 'tmdb',
Expand Down

0 comments on commit dd8f26d

Please sign in to comment.