Skip to content

Commit

Permalink
feat(rest-api): Added episodes to the shows
Browse files Browse the repository at this point in the history
  • Loading branch information
TriPSs committed Nov 13, 2020
1 parent 21b98d6 commit 0aa1d98
Show file tree
Hide file tree
Showing 14 changed files with 370 additions and 4 deletions.
30 changes: 29 additions & 1 deletion apps/rest-api/src/routes/shows/shows.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Controller, Get, Inject, Param, Query } from '@nestjs/common'
import { ShowsArgs, ShowsService } from '@pct-org/api/shows'
import { EpisodesService } from '@pct-org/api/episodes'

import { Show, ShowClean } from '../../shared/show.interface'

Expand All @@ -9,6 +10,9 @@ export class ShowsController {
@Inject()
private readonly showsService: ShowsService

@Inject()
private readonly episodesService: EpisodesService

@Get('/shows')
public async getShows(): Promise<string[]> {
const totalShows = await this.showsService.count()
Expand Down Expand Up @@ -56,6 +60,7 @@ export class ShowsController {
@Param('imdbId') imdbId: string
): Promise<Show> {
const show = await this.showsService.findOne(imdbId)
const episodes = await this.episodesService.findAllForShow(imdbId)

return ({
_id: show._id,
Expand Down Expand Up @@ -86,7 +91,30 @@ export class ShowsController {
hated: 100
},
last_updated: show.updatedAt,
episodes: []
episodes: episodes.map((episode) => ({
torrents: episode.torrents.reduce((newTorrents, torrent) => {
newTorrents[torrent.quality] = {
url: torrent.url,
seed: torrent.seeds,
peer: torrent.peers,
size: torrent.size,
fileSize: torrent.sizeString,
provider: torrent.provider
}

return newTorrents
}, {}),
first_aired: episode.firstAired,
date_based: false,
overview: episode.synopsis,
title: episode.title,
episode: episode.number,
season: episode.season,
tvdb_id: null,
watched: {
watched: false,
}
}))
})
}
}
4 changes: 3 additions & 1 deletion apps/rest-api/src/routes/shows/shows.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Module } from '@nestjs/common'
import { ShowsService } from '@pct-org/api/shows'
import { EpisodesService } from '@pct-org/api/episodes'

import { ShowsController } from './shows.controller'

@Module({
providers: [
ShowsService
ShowsService,
EpisodesService
],
controllers: [
ShowsController
Expand Down
7 changes: 5 additions & 2 deletions apps/rest-api/src/shared/models.module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Global, Module } from '@nestjs/common'
import { MOVIES_MONGOOSE_FEATURE } from '@pct-org/api/movies'
import { SHOWS_MONGOOSE_FEATURE } from '@pct-org/api/shows'
import { EPISODES_MONGOOSE_FEATURE } from '@pct-org/api/episodes'

@Global()
@Module({
imports: [
MOVIES_MONGOOSE_FEATURE,
SHOWS_MONGOOSE_FEATURE
SHOWS_MONGOOSE_FEATURE,
EPISODES_MONGOOSE_FEATURE
],
exports: [
MOVIES_MONGOOSE_FEATURE,
SHOWS_MONGOOSE_FEATURE
SHOWS_MONGOOSE_FEATURE,
EPISODES_MONGOOSE_FEATURE
]
})
export class ModelsModule {
Expand Down
5 changes: 5 additions & 0 deletions libs/api/episodes/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../../.eslintrc.json",
"ignorePatterns": ["!**/*"],
"rules": {}
}
7 changes: 7 additions & 0 deletions libs/api/episodes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# api-episodes

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test api-episodes` to execute the unit tests via [Jest](https://jestjs.io).
14 changes: 14 additions & 0 deletions libs/api/episodes/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
displayName: 'api-episodes',
preset: '../../../jest.preset.js',
globals: {
'ts-jest': {
tsConfig: '<rootDir>/tsconfig.spec.json',
},
},
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../../coverage/libs/api/episodes',
};
11 changes: 11 additions & 0 deletions libs/api/episodes/src/episode.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Model, Document } from 'mongoose'

import { Episode } from './episode.object-type'

export interface EpisodeDocument extends Episode, Document {

_id: string

}

export type EpisodeModel = Model<EpisodeDocument>
63 changes: 63 additions & 0 deletions libs/api/episodes/src/episode.object-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Field, ObjectType } from '@nestjs/graphql'

import { Show } from '@pct-org/api/shows'
import { Torrent, Images, Watched, DownloadInfo } from '@pct-org/api/shared'

@ObjectType()
export class Episode {

@Field({ description: 'The id of the episode.' })
_id: string

@Field({ description: 'The imdb id of the show.' })
showImdbId: string

@Field({ description: 'The tmdb_id of the episode.' })
tmdbId: number

@Field({ description: 'The episode number of the current episode.' })
number: number

@Field({ description: 'Number of season the episode is in.' })
season: number

@Field({ description: 'The title of the episode.' })
title: string

@Field({ description: 'A brief summary of the episode.', nullable: true })
synopsis: string

@Field({ description: 'The date on which the episode was first aired.' })
firstAired: number

@Field(type => Watched, { description: 'Did the user watched this episode already.' })
watched: Watched

@Field({ description: 'The type of the content.' })
type: string

@Field(type => Images, { description: 'The still for the current episode.' })
images: Images

@Field(type => [Torrent], { description: 'The episode\'s torrents.' })
torrents: Torrent[]

@Field(type => [Torrent], {
description: 'The episode\'s torrents that where found by search.',
defaultValue: []
})
searchedTorrents: Torrent[]

@Field(type => DownloadInfo, { description: 'Download info' })
download: DownloadInfo

@Field({ description: 'The time at which the content was created.' })
createdAt: number

@Field({ description: 'The time at which the content was last updated.' })
updatedAt: number

@Field(tye => Show, { description: 'The show this episode belongs to.' })
show?: Show

}
38 changes: 38 additions & 0 deletions libs/api/episodes/src/episode.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Schema } from 'mongoose'

import { torrentSchema, watchedSchema, imagesSchema, downloadInfoSchema } from '@pct-org/api/shared'

export const episodeSchema = (new Schema(
{
_id: {
type: String,
required: true
},
showImdbId: String,
tmdbId: Number,
number: Number,
season: Number,
title: String,
synopsis: String,
firstAired: Number,
type: String,
...watchedSchema,
...downloadInfoSchema,
images: imagesSchema,
torrents: {
type: [torrentSchema]
},
searchedTorrents: {
type: [torrentSchema]
},
createdAt: Number,
updatedAt: Number
},
{
collection: 'episodes'
}
)).index({
showImdbId: 1,
season: 1,
number: 1
})
149 changes: 149 additions & 0 deletions libs/api/episodes/src/episodes.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'

import { Episode } from './episode.object-type'
import { EpisodeModel } from './episode.model'

// import { BookmarksService } from '../bookmarks/bookmarks.service'

@Injectable()
export class EpisodesService {

@InjectModel('Episodes')
private readonly episodeModel: EpisodeModel

/**
* Returns all the episodes for the user that he did not watch
* from shows he bookmarked
*/
// async findMyEpisodes(bookmarksService: BookmarksService, lean = true): Promise<Episode[]> {
// const shows = await bookmarksService.findAllShows({
// offset: 0,
// limit: 1000,
// query: null
// })
//
// const eightDaysAgo = new Date(new Date().getTime() - (8 * 24 * 60 * 60 * 1000)).getTime()
//
// return this.episodeModel.find(
// {
// showImdbId: {
// $in: shows.map(show => show._id)
// },
// firstAired: {
// $gt: eightDaysAgo
// },
// $where: 'this.torrents.length > 0'
// },
// {},
// {
// sort: {
// firstAired: -1
// },
// lean
// }
// )
// }

public async findOne(id: string, lean = true): Promise<Episode> {
return this.episodeModel.findById(
id,
{},
{
lean
}
)
}

public async findAllForShow(imdbId: string, lean = true): Promise<Episode[]> {
return this.episodeModel.find(
{
showImdbId: imdbId,
firstAired: {
$gt: 0
}
},
{},
{
// skip: showsArgs.offset,
// limit: showsArgs.limit,
sort: {
season: 0,
number: 0 // Sort on episode number
},
lean
}
)
}

public async findAllForSeason(imdbId: string, seasonNumber: number, lean = true): Promise<Episode[]> {
return this.episodeModel.find(
{
showImdbId: imdbId,
season: seasonNumber,
firstAired: {
$gt: 0
}
},
{},
{
// skip: showsArgs.offset,
// limit: showsArgs.limit,
sort: {
number: 0 // Sort on episode number
},
lean
}
)
}

public async findAllWithIDS(ids: string[], lean = true): Promise<Episode[]> {
return this.episodeModel.find(
{
_id: {
$in: ids
}
},
{},
{
// skip: showsArgs.offset,
// limit: showsArgs.limit,
sort: {
number: 0 // Sort on episode number
},
lean
}
)
}

public async findForCalendar(showImdbId: string, lean = true): Promise<Episode[]> {
const fourteenDaysAgo = new Date(new Date().getTime() - (14 * 24 * 60 * 60 * 1000)).getTime()

return this.episodeModel.find(
{
showImdbId,
firstAired: {
$gt: fourteenDaysAgo
}
},
{},
{
sort: {
number: 0 // Sort on episode number
},
lean
}
)
}

public async updateOne(episode: Partial<Episode>): Promise<Episode> {
return this.episodeModel.findByIdAndUpdate(
episode._id,
episode,
{
new: true
}
)
}

}
7 changes: 7 additions & 0 deletions libs/api/episodes/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { MongooseModule } from '@nestjs/mongoose'

export { EpisodesService } from './episodes.service'

import { episodeSchema } from './episode.schema'

export const EPISODES_MONGOOSE_FEATURE = MongooseModule.forFeature([{ name: 'Episodes', schema: episodeSchema }])
Loading

0 comments on commit 0aa1d98

Please sign in to comment.