diff options
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/utils.ts | 10 | ||||
-rw-r--r-- | server/models/video/video-blacklist-interface.ts | 6 | ||||
-rw-r--r-- | server/models/video/video-blacklist.ts | 24 |
3 files changed, 34 insertions, 6 deletions
diff --git a/server/models/utils.ts b/server/models/utils.ts index 7ba96815e..1bf61d2a6 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts | |||
@@ -19,9 +19,17 @@ function addMethodsToModel (model: any, classMethods: Function[], instanceMethod | |||
19 | instanceMethods.forEach(m => model.prototype[m.name] = m) | 19 | instanceMethods.forEach(m => model.prototype[m.name] = m) |
20 | } | 20 | } |
21 | 21 | ||
22 | function getSortOnModel (model: any, value: string) { | ||
23 | let sort = getSort(value) | ||
24 | |||
25 | if (model) return [ { model: model }, sort[0], sort[1] ] | ||
26 | return sort | ||
27 | } | ||
28 | |||
22 | // --------------------------------------------------------------------------- | 29 | // --------------------------------------------------------------------------- |
23 | 30 | ||
24 | export { | 31 | export { |
25 | addMethodsToModel, | 32 | addMethodsToModel, |
26 | getSort | 33 | getSort, |
34 | getSortOnModel | ||
27 | } | 35 | } |
diff --git a/server/models/video/video-blacklist-interface.ts b/server/models/video/video-blacklist-interface.ts index ba48b1b6e..9d167c037 100644 --- a/server/models/video/video-blacklist-interface.ts +++ b/server/models/video/video-blacklist-interface.ts | |||
@@ -1,7 +1,9 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | import * as Promise from 'bluebird' | 2 | import * as Promise from 'bluebird' |
3 | 3 | ||
4 | import { SortType } from '../../helpers' | ||
4 | import { ResultList } from '../../../shared' | 5 | import { ResultList } from '../../../shared' |
6 | import { VideoInstance } from './video-interface' | ||
5 | 7 | ||
6 | // Don't use barrel, import just what we need | 8 | // Don't use barrel, import just what we need |
7 | import { BlacklistedVideo as FormattedBlacklistedVideo } from '../../../shared/models/videos/video-blacklist.model' | 9 | import { BlacklistedVideo as FormattedBlacklistedVideo } from '../../../shared/models/videos/video-blacklist.model' |
@@ -13,7 +15,7 @@ export namespace BlacklistedVideoMethods { | |||
13 | 15 | ||
14 | export type List = () => Promise<BlacklistedVideoInstance[]> | 16 | export type List = () => Promise<BlacklistedVideoInstance[]> |
15 | 17 | ||
16 | export type ListForApi = (start: number, count: number, sort: string) => Promise< ResultList<BlacklistedVideoInstance> > | 18 | export type ListForApi = (start: number, count: number, sort: SortType) => Promise< ResultList<BlacklistedVideoInstance> > |
17 | 19 | ||
18 | export type LoadById = (id: number) => Promise<BlacklistedVideoInstance> | 20 | export type LoadById = (id: number) => Promise<BlacklistedVideoInstance> |
19 | 21 | ||
@@ -31,6 +33,8 @@ export interface BlacklistedVideoClass { | |||
31 | 33 | ||
32 | export interface BlacklistedVideoAttributes { | 34 | export interface BlacklistedVideoAttributes { |
33 | videoId: number | 35 | videoId: number |
36 | |||
37 | Video?: VideoInstance | ||
34 | } | 38 | } |
35 | 39 | ||
36 | export interface BlacklistedVideoInstance | 40 | export interface BlacklistedVideoInstance |
diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts index dc49852b6..1c279b1ba 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts | |||
@@ -1,6 +1,8 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | 2 | ||
3 | import { addMethodsToModel, getSort } from '../utils' | 3 | import { SortType } from '../../helpers' |
4 | import { addMethodsToModel, getSortOnModel } from '../utils' | ||
5 | import { VideoInstance } from './video-interface' | ||
4 | import { | 6 | import { |
5 | BlacklistedVideoInstance, | 7 | BlacklistedVideoInstance, |
6 | BlacklistedVideoAttributes, | 8 | BlacklistedVideoAttributes, |
@@ -49,10 +51,23 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da | |||
49 | // ------------------------------ METHODS ------------------------------ | 51 | // ------------------------------ METHODS ------------------------------ |
50 | 52 | ||
51 | toFormattedJSON = function (this: BlacklistedVideoInstance) { | 53 | toFormattedJSON = function (this: BlacklistedVideoInstance) { |
54 | let video: VideoInstance | ||
55 | |||
56 | video = this.Video | ||
57 | |||
52 | return { | 58 | return { |
53 | id: this.id, | 59 | id: this.id, |
54 | videoId: this.videoId, | 60 | videoId: this.videoId, |
55 | createdAt: this.createdAt | 61 | createdAt: this.createdAt, |
62 | updatedAt: this.updatedAt, | ||
63 | name: video.name, | ||
64 | uuid: video.uuid, | ||
65 | description: video.description, | ||
66 | duration: video.duration, | ||
67 | views: video.views, | ||
68 | likes: video.likes, | ||
69 | dislikes: video.dislikes, | ||
70 | nsfw: video.nsfw | ||
56 | } | 71 | } |
57 | } | 72 | } |
58 | 73 | ||
@@ -76,11 +91,12 @@ list = function () { | |||
76 | return BlacklistedVideo.findAll() | 91 | return BlacklistedVideo.findAll() |
77 | } | 92 | } |
78 | 93 | ||
79 | listForApi = function (start: number, count: number, sort: string) { | 94 | listForApi = function (start: number, count: number, sort: SortType) { |
80 | const query = { | 95 | const query = { |
81 | offset: start, | 96 | offset: start, |
82 | limit: count, | 97 | limit: count, |
83 | order: [ getSort(sort) ] | 98 | order: [ getSortOnModel(sort.sortModel, sort.sortValue) ], |
99 | include: [ { model: BlacklistedVideo['sequelize'].models.Video } ] | ||
84 | } | 100 | } |
85 | 101 | ||
86 | return BlacklistedVideo.findAndCountAll(query).then(({ rows, count }) => { | 102 | return BlacklistedVideo.findAndCountAll(query).then(({ rows, count }) => { |