Skip to content

Commit

Permalink
feat: adds button on the page of a media item to add or remove it fro…
Browse files Browse the repository at this point in the history
…m a user's watchlist

re #730
  • Loading branch information
JoaquinOlivero committed Jul 10, 2024
1 parent ae955e9 commit 64da046
Show file tree
Hide file tree
Showing 7 changed files with 447 additions and 181 deletions.
5 changes: 4 additions & 1 deletion server/models/Movie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export interface MovieDetails {
mediaUrl?: string;
watchProviders?: WatchProviders[];
keywords: Keyword[];
onUserWatchlist?: boolean;
}

export const mapProductionCompany = (
Expand All @@ -101,7 +102,8 @@ export const mapProductionCompany = (

export const mapMovieDetails = (
movie: TmdbMovieDetails,
media?: Media
media?: Media,
userWatchlist?: boolean
): MovieDetails => ({
id: movie.id,
adult: movie.adult,
Expand Down Expand Up @@ -148,4 +150,5 @@ export const mapMovieDetails = (
id: keyword.id,
name: keyword.name,
})),
onUserWatchlist: userWatchlist,
});
5 changes: 4 additions & 1 deletion server/models/Tv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export interface TvDetails {
keywords: Keyword[];
mediaInfo?: Media;
watchProviders?: WatchProviders[];
onUserWatchlist?: boolean;
}

const mapEpisodeResult = (episode: TmdbTvEpisodeResult): Episode => ({
Expand Down Expand Up @@ -161,7 +162,8 @@ export const mapNetwork = (network: TmdbNetwork): TvNetwork => ({

export const mapTvDetails = (
show: TmdbTvDetails,
media?: Media
media?: Media,
userWatchlist?: boolean
): TvDetails => ({
createdBy: show.created_by,
episodeRunTime: show.episode_run_time,
Expand Down Expand Up @@ -223,4 +225,5 @@ export const mapTvDetails = (
})),
mediaInfo: media,
watchProviders: mapWatchProviders(show['watch/providers']?.results ?? {}),
onUserWatchlist: userWatchlist,
});
15 changes: 14 additions & 1 deletion server/routes/movie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import RottenTomatoes from '@server/api/rating/rottentomatoes';
import { type RatingResponse } from '@server/api/ratings';
import TheMovieDb from '@server/api/themoviedb';
import { MediaType } from '@server/constants/media';
import { getRepository } from '@server/datasource';
import Media from '@server/entity/Media';
import { Watchlist } from '@server/entity/Watchlist';
import logger from '@server/logger';
import { mapMovieDetails } from '@server/models/Movie';
import { mapMovieResult } from '@server/models/Search';
Expand All @@ -22,7 +24,18 @@ movieRoutes.get('/:id', async (req, res, next) => {

const media = await Media.getMedia(tmdbMovie.id, MediaType.MOVIE);

return res.status(200).json(mapMovieDetails(tmdbMovie, media));
const onUserWatchlist = await getRepository(Watchlist).exist({
where: {
tmdbId: Number(req.params.id),
requestedBy: {
id: req.user?.id,
},
},
});

return res
.status(200)
.json(mapMovieDetails(tmdbMovie, media, onUserWatchlist));
} catch (e) {
logger.debug('Something went wrong retrieving movie', {
label: 'API',
Expand Down
13 changes: 12 additions & 1 deletion server/routes/tv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import RottenTomatoes from '@server/api/rating/rottentomatoes';
import TheMovieDb from '@server/api/themoviedb';
import { MediaType } from '@server/constants/media';
import { getRepository } from '@server/datasource';
import Media from '@server/entity/Media';
import { Watchlist } from '@server/entity/Watchlist';
import logger from '@server/logger';
import { mapTvResult } from '@server/models/Search';
import { mapSeasonWithEpisodes, mapTvDetails } from '@server/models/Tv';
Expand All @@ -19,7 +21,16 @@ tvRoutes.get('/:id', async (req, res, next) => {

const media = await Media.getMedia(tv.id, MediaType.TV);

return res.status(200).json(mapTvDetails(tv, media));
const onUserWatchlist = await getRepository(Watchlist).exist({
where: {
tmdbId: Number(req.params.id),
requestedBy: {
id: req.user?.id,
},
},
});

return res.status(200).json(mapTvDetails(tv, media, onUserWatchlist));
} catch (e) {
logger.debug('Something went wrong retrieving series', {
label: 'API',
Expand Down
Loading

0 comments on commit 64da046

Please sign in to comment.