]>
Commit | Line | Data |
---|---|---|
e02643f3 C |
1 | import * as Sequelize from 'sequelize' |
2 | ||
74889a71 | 3 | import { addMethodsToModel, getSort } from '../utils' |
e02643f3 | 4 | import { |
e02643f3 C |
5 | BlacklistedVideoInstance, |
6 | BlacklistedVideoAttributes, | |
7 | ||
8 | BlacklistedVideoMethods | |
9 | } from './video-blacklist-interface' | |
10 | ||
11 | let BlacklistedVideo: Sequelize.Model<BlacklistedVideoInstance, BlacklistedVideoAttributes> | |
12 | let toFormatedJSON: BlacklistedVideoMethods.ToFormatedJSON | |
13 | let countTotal: BlacklistedVideoMethods.CountTotal | |
14 | let list: BlacklistedVideoMethods.List | |
15 | let listForApi: BlacklistedVideoMethods.ListForApi | |
16 | let loadById: BlacklistedVideoMethods.LoadById | |
17 | let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId | |
18 | ||
127944aa C |
19 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
20 | BlacklistedVideo = sequelize.define<BlacklistedVideoInstance, BlacklistedVideoAttributes>('BlacklistedVideo', | |
198b205c GS |
21 | {}, |
22 | { | |
23 | indexes: [ | |
24 | { | |
25 | fields: [ 'videoId' ], | |
26 | unique: true | |
27 | } | |
e02643f3 | 28 | ] |
198b205c GS |
29 | } |
30 | ) | |
31 | ||
e02643f3 C |
32 | const classMethods = [ |
33 | associate, | |
34 | ||
35 | countTotal, | |
36 | list, | |
37 | listForApi, | |
38 | loadById, | |
39 | loadByVideoId | |
40 | ] | |
41 | const instanceMethods = [ | |
42 | toFormatedJSON | |
43 | ] | |
44 | addMethodsToModel(BlacklistedVideo, classMethods, instanceMethods) | |
45 | ||
198b205c GS |
46 | return BlacklistedVideo |
47 | } | |
48 | ||
49 | // ------------------------------ METHODS ------------------------------ | |
50 | ||
70c065d6 | 51 | toFormatedJSON = function (this: BlacklistedVideoInstance) { |
198b205c GS |
52 | return { |
53 | id: this.id, | |
54 | videoId: this.videoId, | |
55 | createdAt: this.createdAt | |
56 | } | |
57 | } | |
58 | ||
59 | // ------------------------------ STATICS ------------------------------ | |
60 | ||
61 | function associate (models) { | |
e02643f3 | 62 | BlacklistedVideo.belongsTo(models.Video, { |
198b205c GS |
63 | foreignKey: 'videoId', |
64 | onDelete: 'cascade' | |
65 | }) | |
66 | } | |
67 | ||
6fcd19ba C |
68 | countTotal = function () { |
69 | return BlacklistedVideo.count() | |
198b205c GS |
70 | } |
71 | ||
6fcd19ba C |
72 | list = function () { |
73 | return BlacklistedVideo.findAll() | |
198b205c GS |
74 | } |
75 | ||
6fcd19ba | 76 | listForApi = function (start: number, count: number, sort: string) { |
198b205c GS |
77 | const query = { |
78 | offset: start, | |
79 | limit: count, | |
65fcc311 | 80 | order: [ getSort(sort) ] |
198b205c GS |
81 | } |
82 | ||
6fcd19ba C |
83 | return BlacklistedVideo.findAndCountAll(query).then(({ rows, count }) => { |
84 | return { | |
85 | data: rows, | |
86 | total: count | |
87 | } | |
198b205c GS |
88 | }) |
89 | } | |
90 | ||
6fcd19ba C |
91 | loadById = function (id: number) { |
92 | return BlacklistedVideo.findById(id) | |
198b205c GS |
93 | } |
94 | ||
6fcd19ba | 95 | loadByVideoId = function (id: string) { |
198b205c GS |
96 | const query = { |
97 | where: { | |
98 | videoId: id | |
99 | } | |
100 | } | |
101 | ||
6fcd19ba | 102 | return BlacklistedVideo.findOne(query) |
198b205c | 103 | } |