]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' |
da854ddd | 2 | import { SortType } from '../../helpers/utils' |
3fd3ab2d C |
3 | import { getSortOnModel } from '../utils' |
4 | import { VideoModel } from './video' | |
e02643f3 | 5 | |
3fd3ab2d C |
6 | @Table({ |
7 | tableName: 'videoBlacklist', | |
8 | indexes: [ | |
198b205c | 9 | { |
3fd3ab2d C |
10 | fields: [ 'videoId' ], |
11 | unique: true | |
198b205c | 12 | } |
e02643f3 | 13 | ] |
3fd3ab2d C |
14 | }) |
15 | export class VideoBlacklistModel extends Model<VideoBlacklistModel> { | |
198b205c | 16 | |
3fd3ab2d C |
17 | @CreatedAt |
18 | createdAt: Date | |
198b205c | 19 | |
3fd3ab2d C |
20 | @UpdatedAt |
21 | updatedAt: Date | |
198b205c | 22 | |
3fd3ab2d C |
23 | @ForeignKey(() => VideoModel) |
24 | @Column | |
25 | videoId: number | |
198b205c | 26 | |
3fd3ab2d | 27 | @BelongsTo(() => VideoModel, { |
0a6658fd | 28 | foreignKey: { |
0a6658fd C |
29 | allowNull: false |
30 | }, | |
3fd3ab2d | 31 | onDelete: 'cascade' |
198b205c | 32 | }) |
3fd3ab2d C |
33 | Video: VideoModel |
34 | ||
35 | static listForApi (start: number, count: number, sort: SortType) { | |
36 | const query = { | |
37 | offset: start, | |
38 | limit: count, | |
3bb6c526 | 39 | order: getSortOnModel(sort.sortModel, sort.sortValue), |
3fd3ab2d C |
40 | include: [ { model: VideoModel } ] |
41 | } | |
198b205c | 42 | |
3fd3ab2d C |
43 | return VideoBlacklistModel.findAndCountAll(query) |
44 | .then(({ rows, count }) => { | |
45 | return { | |
46 | data: rows, | |
47 | total: count | |
48 | } | |
49 | }) | |
198b205c GS |
50 | } |
51 | ||
3fd3ab2d C |
52 | static loadByVideoId (id: number) { |
53 | const query = { | |
54 | where: { | |
55 | videoId: id | |
56 | } | |
6fcd19ba | 57 | } |
198b205c | 58 | |
3fd3ab2d | 59 | return VideoBlacklistModel.findOne(query) |
198b205c GS |
60 | } |
61 | ||
3fd3ab2d C |
62 | toFormattedJSON () { |
63 | const video = this.Video | |
64 | ||
65 | return { | |
66 | id: this.id, | |
67 | videoId: this.videoId, | |
68 | createdAt: this.createdAt, | |
69 | updatedAt: this.updatedAt, | |
70 | name: video.name, | |
71 | uuid: video.uuid, | |
72 | description: video.description, | |
73 | duration: video.duration, | |
74 | views: video.views, | |
75 | likes: video.likes, | |
76 | dislikes: video.dislikes, | |
77 | nsfw: video.nsfw | |
78 | } | |
79 | } | |
198b205c | 80 | } |