-
Notifications
You must be signed in to change notification settings - Fork 3
/
show-api.js
49 lines (40 loc) · 1.54 KB
/
show-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* eslint-disable no-useless-constructor */
import { createController } from 'awilix-koa';
import BaseRestApi from './base-api';
class ShowRestApi extends BaseRestApi {
constructor(showService, seasonService, episodeService) {
super(showService);
this.seasonService = seasonService;
this.episodeService = episodeService;
}
async getSeasons(ctx) {
let query = { ...{ show_uid: ctx.params.id }, ...ctx.query };
if (ctx.params.sid) {
const seasonIdkey = isNaN(ctx.params.sid) ? 'uid' : 'season_number';
query[seasonIdkey] = ctx.params.sid;
}
return ctx.ok(await this.seasonService.find(query));
}
async getEpisodes(ctx) {
let query = { ...{ show_uid: ctx.params.id }, ...ctx.query };
if (ctx.params.sid) {
const seasonIdkey = isNaN(ctx.params.sid) ? 'season_uid' : 'season_number';
query[seasonIdkey] = ctx.params.sid;
}
return ctx.ok(await this.episodeService.find(query));
}
}
export default createController(ShowRestApi)
.prefix('/api/shows')
.get('', 'find')
.get('/:id', 'get')
.get('/:id/seasons', 'getSeasons')
.get('/:id/episodes', 'getEpisodes')
.get('/:id/seasons/:sid', 'getSeasons')
.get('/:id/seasons/:sid/episodes', 'getEpisodes')
.post('', 'create')
.patch('/:id', 'update')
.delete('/:id', 'remove');
// Maps routes to method calls on the `api` controller.
// See the `awilix-router-core` docs for info:
// https://github.com/jeffijoe/awilix-router-core