aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-blacklist.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-12 17:53:50 +0100
committerChocobozzz <me@florianbigard.com>2017-12-13 16:50:33 +0100
commit3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch)
treee5ca358287fca6ecacce83defcf23af1e8e9f419 /server/models/video/video-blacklist.ts
parentc893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff)
downloadPeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip
Move models to typescript-sequelize
Diffstat (limited to 'server/models/video/video-blacklist.ts')
-rw-r--r--server/models/video/video-blacklist.ts142
1 files changed, 59 insertions, 83 deletions
diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts
index ae8286285..6db562719 100644
--- a/server/models/video/video-blacklist.ts
+++ b/server/models/video/video-blacklist.ts
@@ -1,104 +1,80 @@
1import * as Sequelize from 'sequelize' 1import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
2
3import { SortType } from '../../helpers' 2import { SortType } from '../../helpers'
4import { addMethodsToModel, getSortOnModel } from '../utils' 3import { getSortOnModel } from '../utils'
5import { VideoInstance } from './video-interface' 4import { VideoModel } from './video'
6import {
7 BlacklistedVideoInstance,
8 BlacklistedVideoAttributes,
9
10 BlacklistedVideoMethods
11} from './video-blacklist-interface'
12
13let BlacklistedVideo: Sequelize.Model<BlacklistedVideoInstance, BlacklistedVideoAttributes>
14let toFormattedJSON: BlacklistedVideoMethods.ToFormattedJSON
15let listForApi: BlacklistedVideoMethods.ListForApi
16let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId
17 5
18export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { 6@Table({
19 BlacklistedVideo = sequelize.define<BlacklistedVideoInstance, BlacklistedVideoAttributes>('BlacklistedVideo', 7 tableName: 'videoBlacklist',
20 {}, 8 indexes: [
21 { 9 {
22 indexes: [ 10 fields: [ 'videoId' ],
23 { 11 unique: true
24 fields: [ 'videoId' ],
25 unique: true
26 }
27 ]
28 } 12 }
29 )
30
31 const classMethods = [
32 associate,
33
34 listForApi,
35 loadByVideoId
36 ] 13 ]
37 const instanceMethods = [ 14})
38 toFormattedJSON 15export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
39 ]
40 addMethodsToModel(BlacklistedVideo, classMethods, instanceMethods)
41
42 return BlacklistedVideo
43}
44 16
45// ------------------------------ METHODS ------------------------------ 17 @CreatedAt
18 createdAt: Date
46 19
47toFormattedJSON = function (this: BlacklistedVideoInstance) { 20 @UpdatedAt
48 let video: VideoInstance 21 updatedAt: Date
49
50 video = this.Video
51
52 return {
53 id: this.id,
54 videoId: this.videoId,
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
65 }
66}
67 22
68// ------------------------------ STATICS ------------------------------ 23 @ForeignKey(() => VideoModel)
24 @Column
25 videoId: number
69 26
70function associate (models) { 27 @BelongsTo(() => VideoModel, {
71 BlacklistedVideo.belongsTo(models.Video, {
72 foreignKey: { 28 foreignKey: {
73 name: 'videoId',
74 allowNull: false 29 allowNull: false
75 }, 30 },
76 onDelete: 'CASCADE' 31 onDelete: 'cascade'
77 }) 32 })
78} 33 Video: VideoModel
34
35 static listForApi (start: number, count: number, sort: SortType) {
36 const query = {
37 offset: start,
38 limit: count,
39 order: [ getSortOnModel(sort.sortModel, sort.sortValue) ],
40 include: [ { model: VideoModel } ]
41 }
79 42
80listForApi = function (start: number, count: number, sort: SortType) { 43 return VideoBlacklistModel.findAndCountAll(query)
81 const query = { 44 .then(({ rows, count }) => {
82 offset: start, 45 return {
83 limit: count, 46 data: rows,
84 order: [ getSortOnModel(sort.sortModel, sort.sortValue) ], 47 total: count
85 include: [ { model: BlacklistedVideo['sequelize'].models.Video } ] 48 }
49 })
86 } 50 }
87 51
88 return BlacklistedVideo.findAndCountAll(query).then(({ rows, count }) => { 52 static loadByVideoId (id: number) {
89 return { 53 const query = {
90 data: rows, 54 where: {
91 total: count 55 videoId: id
56 }
92 } 57 }
93 })
94}
95 58
96loadByVideoId = function (id: number) { 59 return VideoBlacklistModel.findOne(query)
97 const query = {
98 where: {
99 videoId: id
100 }
101 } 60 }
102 61
103 return BlacklistedVideo.findOne(query) 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 }
104} 80}