diff options
Diffstat (limited to 'server/models/video-blacklist.ts')
-rw-r--r-- | server/models/video-blacklist.ts | 80 |
1 files changed, 48 insertions, 32 deletions
diff --git a/server/models/video-blacklist.ts b/server/models/video-blacklist.ts index 1f00702c7..fe72d5d46 100644 --- a/server/models/video-blacklist.ts +++ b/server/models/video-blacklist.ts | |||
@@ -1,9 +1,24 @@ | |||
1 | import { getSort } from './utils' | 1 | import * as Sequelize from 'sequelize' |
2 | 2 | ||
3 | // --------------------------------------------------------------------------- | 3 | import { addMethodsToModel, getSort } from './utils' |
4 | 4 | import { | |
5 | module.exports = function (sequelize, DataTypes) { | 5 | BlacklistedVideoClass, |
6 | const BlacklistedVideo = sequelize.define('BlacklistedVideo', | 6 | BlacklistedVideoInstance, |
7 | BlacklistedVideoAttributes, | ||
8 | |||
9 | BlacklistedVideoMethods | ||
10 | } from './video-blacklist-interface' | ||
11 | |||
12 | let BlacklistedVideo: Sequelize.Model<BlacklistedVideoInstance, BlacklistedVideoAttributes> | ||
13 | let toFormatedJSON: BlacklistedVideoMethods.ToFormatedJSON | ||
14 | let countTotal: BlacklistedVideoMethods.CountTotal | ||
15 | let list: BlacklistedVideoMethods.List | ||
16 | let listForApi: BlacklistedVideoMethods.ListForApi | ||
17 | let loadById: BlacklistedVideoMethods.LoadById | ||
18 | let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId | ||
19 | |||
20 | export default function (sequelize, DataTypes) { | ||
21 | BlacklistedVideo = sequelize.define('BlacklistedVideo', | ||
7 | {}, | 22 | {}, |
8 | { | 23 | { |
9 | indexes: [ | 24 | indexes: [ |
@@ -11,29 +26,30 @@ module.exports = function (sequelize, DataTypes) { | |||
11 | fields: [ 'videoId' ], | 26 | fields: [ 'videoId' ], |
12 | unique: true | 27 | unique: true |
13 | } | 28 | } |
14 | ], | 29 | ] |
15 | classMethods: { | ||
16 | associate, | ||
17 | |||
18 | countTotal, | ||
19 | list, | ||
20 | listForApi, | ||
21 | loadById, | ||
22 | loadByVideoId | ||
23 | }, | ||
24 | instanceMethods: { | ||
25 | toFormatedJSON | ||
26 | }, | ||
27 | hooks: {} | ||
28 | } | 30 | } |
29 | ) | 31 | ) |
30 | 32 | ||
33 | const classMethods = [ | ||
34 | associate, | ||
35 | |||
36 | countTotal, | ||
37 | list, | ||
38 | listForApi, | ||
39 | loadById, | ||
40 | loadByVideoId | ||
41 | ] | ||
42 | const instanceMethods = [ | ||
43 | toFormatedJSON | ||
44 | ] | ||
45 | addMethodsToModel(BlacklistedVideo, classMethods, instanceMethods) | ||
46 | |||
31 | return BlacklistedVideo | 47 | return BlacklistedVideo |
32 | } | 48 | } |
33 | 49 | ||
34 | // ------------------------------ METHODS ------------------------------ | 50 | // ------------------------------ METHODS ------------------------------ |
35 | 51 | ||
36 | function toFormatedJSON () { | 52 | toFormatedJSON = function () { |
37 | return { | 53 | return { |
38 | id: this.id, | 54 | id: this.id, |
39 | videoId: this.videoId, | 55 | videoId: this.videoId, |
@@ -44,44 +60,44 @@ function toFormatedJSON () { | |||
44 | // ------------------------------ STATICS ------------------------------ | 60 | // ------------------------------ STATICS ------------------------------ |
45 | 61 | ||
46 | function associate (models) { | 62 | function associate (models) { |
47 | this.belongsTo(models.Video, { | 63 | BlacklistedVideo.belongsTo(models.Video, { |
48 | foreignKey: 'videoId', | 64 | foreignKey: 'videoId', |
49 | onDelete: 'cascade' | 65 | onDelete: 'cascade' |
50 | }) | 66 | }) |
51 | } | 67 | } |
52 | 68 | ||
53 | function countTotal (callback) { | 69 | countTotal = function (callback) { |
54 | return this.count().asCallback(callback) | 70 | return BlacklistedVideo.count().asCallback(callback) |
55 | } | 71 | } |
56 | 72 | ||
57 | function list (callback) { | 73 | list = function (callback) { |
58 | return this.findAll().asCallback(callback) | 74 | return BlacklistedVideo.findAll().asCallback(callback) |
59 | } | 75 | } |
60 | 76 | ||
61 | function listForApi (start, count, sort, callback) { | 77 | listForApi = function (start, count, sort, callback) { |
62 | const query = { | 78 | const query = { |
63 | offset: start, | 79 | offset: start, |
64 | limit: count, | 80 | limit: count, |
65 | order: [ getSort(sort) ] | 81 | order: [ getSort(sort) ] |
66 | } | 82 | } |
67 | 83 | ||
68 | return this.findAndCountAll(query).asCallback(function (err, result) { | 84 | return BlacklistedVideo.findAndCountAll(query).asCallback(function (err, result) { |
69 | if (err) return callback(err) | 85 | if (err) return callback(err) |
70 | 86 | ||
71 | return callback(null, result.rows, result.count) | 87 | return callback(null, result.rows, result.count) |
72 | }) | 88 | }) |
73 | } | 89 | } |
74 | 90 | ||
75 | function loadById (id, callback) { | 91 | loadById = function (id, callback) { |
76 | return this.findById(id).asCallback(callback) | 92 | return BlacklistedVideo.findById(id).asCallback(callback) |
77 | } | 93 | } |
78 | 94 | ||
79 | function loadByVideoId (id, callback) { | 95 | loadByVideoId = function (id, callback) { |
80 | const query = { | 96 | const query = { |
81 | where: { | 97 | where: { |
82 | videoId: id | 98 | videoId: id |
83 | } | 99 | } |
84 | } | 100 | } |
85 | 101 | ||
86 | return this.find(query).asCallback(callback) | 102 | return BlacklistedVideo.find(query).asCallback(callback) |
87 | } | 103 | } |