]>
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 C |
15 | let countTotal: BlacklistedVideoMethods.CountTotal |
16 | let list: BlacklistedVideoMethods.List | |
17 | let listForApi: BlacklistedVideoMethods.ListForApi | |
18 | let loadById: BlacklistedVideoMethods.LoadById | |
19 | let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId | |
20 | ||
127944aa C |
21 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
22 | BlacklistedVideo = sequelize.define<BlacklistedVideoInstance, BlacklistedVideoAttributes>('BlacklistedVideo', | |
198b205c GS |
23 | {}, |
24 | { | |
25 | indexes: [ | |
26 | { | |
27 | fields: [ 'videoId' ], | |
28 | unique: true | |
29 | } | |
e02643f3 | 30 | ] |
198b205c GS |
31 | } |
32 | ) | |
33 | ||
e02643f3 C |
34 | const classMethods = [ |
35 | associate, | |
36 | ||
37 | countTotal, | |
38 | list, | |
39 | listForApi, | |
40 | loadById, | |
41 | loadByVideoId | |
42 | ] | |
43 | const instanceMethods = [ | |
0aef76c4 | 44 | toFormattedJSON |
e02643f3 C |
45 | ] |
46 | addMethodsToModel(BlacklistedVideo, classMethods, instanceMethods) | |
47 | ||
198b205c GS |
48 | return BlacklistedVideo |
49 | } | |
50 | ||
51 | // ------------------------------ METHODS ------------------------------ | |
52 | ||
0aef76c4 | 53 | toFormattedJSON = function (this: BlacklistedVideoInstance) { |
792dbaf0 GS |
54 | let video: VideoInstance |
55 | ||
56 | video = this.Video | |
57 | ||
198b205c GS |
58 | return { |
59 | id: this.id, | |
60 | videoId: this.videoId, | |
792dbaf0 GS |
61 | createdAt: this.createdAt, |
62 | updatedAt: this.updatedAt, | |
63 | name: video.name, | |
64 | uuid: video.uuid, | |
65 | description: video.description, | |
66 | duration: video.duration, | |
67 | views: video.views, | |
68 | likes: video.likes, | |
69 | dislikes: video.dislikes, | |
70 | nsfw: video.nsfw | |
198b205c GS |
71 | } |
72 | } | |
73 | ||
74 | // ------------------------------ STATICS ------------------------------ | |
75 | ||
76 | function associate (models) { | |
e02643f3 | 77 | BlacklistedVideo.belongsTo(models.Video, { |
0a6658fd C |
78 | foreignKey: { |
79 | name: 'videoId', | |
80 | allowNull: false | |
81 | }, | |
82 | onDelete: 'CASCADE' | |
198b205c GS |
83 | }) |
84 | } | |
85 | ||
6fcd19ba C |
86 | countTotal = function () { |
87 | return BlacklistedVideo.count() | |
198b205c GS |
88 | } |
89 | ||
6fcd19ba C |
90 | list = function () { |
91 | return BlacklistedVideo.findAll() | |
198b205c GS |
92 | } |
93 | ||
792dbaf0 | 94 | listForApi = function (start: number, count: number, sort: SortType) { |
198b205c GS |
95 | const query = { |
96 | offset: start, | |
97 | limit: count, | |
792dbaf0 GS |
98 | order: [ getSortOnModel(sort.sortModel, sort.sortValue) ], |
99 | include: [ { model: BlacklistedVideo['sequelize'].models.Video } ] | |
198b205c GS |
100 | } |
101 | ||
6fcd19ba C |
102 | return BlacklistedVideo.findAndCountAll(query).then(({ rows, count }) => { |
103 | return { | |
104 | data: rows, | |
105 | total: count | |
106 | } | |
198b205c GS |
107 | }) |
108 | } | |
109 | ||
6fcd19ba C |
110 | loadById = function (id: number) { |
111 | return BlacklistedVideo.findById(id) | |
198b205c GS |
112 | } |
113 | ||
0a6658fd | 114 | loadByVideoId = function (id: number) { |
198b205c GS |
115 | const query = { |
116 | where: { | |
117 | videoId: id | |
118 | } | |
119 | } | |
120 | ||
6fcd19ba | 121 | return BlacklistedVideo.findOne(query) |
198b205c | 122 | } |