diff options
Diffstat (limited to 'server/models/video-blacklist.js')
-rw-r--r-- | server/models/video-blacklist.js | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/server/models/video-blacklist.js b/server/models/video-blacklist.js new file mode 100644 index 000000000..02ea15760 --- /dev/null +++ b/server/models/video-blacklist.js | |||
@@ -0,0 +1,89 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const modelUtils = require('./utils') | ||
4 | |||
5 | // --------------------------------------------------------------------------- | ||
6 | |||
7 | module.exports = function (sequelize, DataTypes) { | ||
8 | const BlacklistedVideo = sequelize.define('BlacklistedVideo', | ||
9 | {}, | ||
10 | { | ||
11 | indexes: [ | ||
12 | { | ||
13 | fields: [ 'videoId' ], | ||
14 | unique: true | ||
15 | } | ||
16 | ], | ||
17 | classMethods: { | ||
18 | associate, | ||
19 | |||
20 | countTotal, | ||
21 | list, | ||
22 | listForApi, | ||
23 | loadById, | ||
24 | loadByVideoId | ||
25 | }, | ||
26 | instanceMethods: { | ||
27 | toFormatedJSON | ||
28 | }, | ||
29 | hooks: {} | ||
30 | } | ||
31 | ) | ||
32 | |||
33 | return BlacklistedVideo | ||
34 | } | ||
35 | |||
36 | // ------------------------------ METHODS ------------------------------ | ||
37 | |||
38 | function toFormatedJSON () { | ||
39 | return { | ||
40 | id: this.id, | ||
41 | videoId: this.videoId, | ||
42 | createdAt: this.createdAt | ||
43 | } | ||
44 | } | ||
45 | |||
46 | // ------------------------------ STATICS ------------------------------ | ||
47 | |||
48 | function associate (models) { | ||
49 | this.belongsTo(models.Video, { | ||
50 | foreignKey: 'videoId', | ||
51 | onDelete: 'cascade' | ||
52 | }) | ||
53 | } | ||
54 | |||
55 | function countTotal (callback) { | ||
56 | return this.count().asCallback(callback) | ||
57 | } | ||
58 | |||
59 | function list (callback) { | ||
60 | return this.findAll().asCallback(callback) | ||
61 | } | ||
62 | |||
63 | function listForApi (start, count, sort, callback) { | ||
64 | const query = { | ||
65 | offset: start, | ||
66 | limit: count, | ||
67 | order: [ modelUtils.getSort(sort) ] | ||
68 | } | ||
69 | |||
70 | return this.findAndCountAll(query).asCallback(function (err, result) { | ||
71 | if (err) return callback(err) | ||
72 | |||
73 | return callback(null, result.rows, result.count) | ||
74 | }) | ||
75 | } | ||
76 | |||
77 | function loadById (id, callback) { | ||
78 | return this.findById(id).asCallback(callback) | ||
79 | } | ||
80 | |||
81 | function loadByVideoId (id, callback) { | ||
82 | const query = { | ||
83 | where: { | ||
84 | videoId: id | ||
85 | } | ||
86 | } | ||
87 | |||
88 | return this.find(query).asCallback(callback) | ||
89 | } | ||