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