]>
Commit | Line | Data |
---|---|---|
e02643f3 C |
1 | import * as Sequelize from 'sequelize' |
2 | ||
3 | import { addMethodsToModel, getSort } from './utils' | |
4 | import { | |
5 | BlacklistedVideoClass, | |
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', | |
198b205c GS |
22 | {}, |
23 | { | |
24 | indexes: [ | |
25 | { | |
26 | fields: [ 'videoId' ], | |
27 | unique: true | |
28 | } | |
e02643f3 | 29 | ] |
198b205c GS |
30 | } |
31 | ) | |
32 | ||
e02643f3 C |
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 | ||
198b205c GS |
47 | return BlacklistedVideo |
48 | } | |
49 | ||
50 | // ------------------------------ METHODS ------------------------------ | |
51 | ||
e02643f3 | 52 | toFormatedJSON = function () { |
198b205c GS |
53 | return { |
54 | id: this.id, | |
55 | videoId: this.videoId, | |
56 | createdAt: this.createdAt | |
57 | } | |
58 | } | |
59 | ||
60 | // ------------------------------ STATICS ------------------------------ | |
61 | ||
62 | function associate (models) { | |
e02643f3 | 63 | BlacklistedVideo.belongsTo(models.Video, { |
198b205c GS |
64 | foreignKey: 'videoId', |
65 | onDelete: 'cascade' | |
66 | }) | |
67 | } | |
68 | ||
69818c93 | 69 | countTotal = function (callback: BlacklistedVideoMethods.CountTotalCallback) { |
e02643f3 | 70 | return BlacklistedVideo.count().asCallback(callback) |
198b205c GS |
71 | } |
72 | ||
69818c93 | 73 | list = function (callback: BlacklistedVideoMethods.ListCallback) { |
e02643f3 | 74 | return BlacklistedVideo.findAll().asCallback(callback) |
198b205c GS |
75 | } |
76 | ||
69818c93 | 77 | listForApi = function (start: number, count: number, sort: string, callback: BlacklistedVideoMethods.ListForApiCallback) { |
198b205c GS |
78 | const query = { |
79 | offset: start, | |
80 | limit: count, | |
65fcc311 | 81 | order: [ getSort(sort) ] |
198b205c GS |
82 | } |
83 | ||
e02643f3 | 84 | return BlacklistedVideo.findAndCountAll(query).asCallback(function (err, result) { |
198b205c GS |
85 | if (err) return callback(err) |
86 | ||
87 | return callback(null, result.rows, result.count) | |
88 | }) | |
89 | } | |
90 | ||
69818c93 | 91 | loadById = function (id: number, callback: BlacklistedVideoMethods.LoadByIdCallback) { |
e02643f3 | 92 | return BlacklistedVideo.findById(id).asCallback(callback) |
198b205c GS |
93 | } |
94 | ||
69818c93 | 95 | loadByVideoId = function (id: string, callback: BlacklistedVideoMethods.LoadByIdCallback) { |
198b205c GS |
96 | const query = { |
97 | where: { | |
98 | videoId: id | |
99 | } | |
100 | } | |
101 | ||
e02643f3 | 102 | return BlacklistedVideo.find(query).asCallback(callback) |
198b205c | 103 | } |