aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/account/account-video-rate.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/account/account-video-rate.ts
parentc893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff)
downloadPeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip
Move models to typescript-sequelize
Diffstat (limited to 'server/models/account/account-video-rate.ts')
-rw-r--r--server/models/account/account-video-rate.ts97
1 files changed, 44 insertions, 53 deletions
diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts
index d92834bbb..e969e4a43 100644
--- a/server/models/account/account-video-rate.ts
+++ b/server/models/account/account-video-rate.ts
@@ -1,78 +1,69 @@
1/*
2 Account rates per video.
3*/
4import { values } from 'lodash' 1import { values } from 'lodash'
5import * as Sequelize from 'sequelize' 2import { Transaction } from 'sequelize'
6 3import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
4import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions'
5import { VideoRateType } from '../../../shared/models/videos'
7import { VIDEO_RATE_TYPES } from '../../initializers' 6import { VIDEO_RATE_TYPES } from '../../initializers'
7import { VideoModel } from '../video/video'
8import { AccountModel } from './account'
8 9
9import { addMethodsToModel } from '../utils' 10/*
10import { 11 Account rates per video.
11 AccountVideoRateInstance, 12*/
12 AccountVideoRateAttributes, 13@Table({
13 14 tableName: 'accountVideoRate',
14 AccountVideoRateMethods 15 indexes: [
15} from './account-video-rate-interface'
16
17let AccountVideoRate: Sequelize.Model<AccountVideoRateInstance, AccountVideoRateAttributes>
18let load: AccountVideoRateMethods.Load
19
20export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
21 AccountVideoRate = sequelize.define<AccountVideoRateInstance, AccountVideoRateAttributes>('AccountVideoRate',
22 {
23 type: {
24 type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)),
25 allowNull: false
26 }
27 },
28 { 16 {
29 indexes: [ 17 fields: [ 'videoId', 'accountId' ],
30 { 18 unique: true
31 fields: [ 'videoId', 'accountId' ],
32 unique: true
33 }
34 ]
35 } 19 }
36 ) 20 ]
21})
22export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
37 23
38 const classMethods = [ 24 @AllowNull(false)
39 associate, 25 @Column(DataType.ENUM(values(VIDEO_RATE_TYPES)))
26 type: VideoRateType
40 27
41 load 28 @CreatedAt
42 ] 29 createdAt: Date
43 addMethodsToModel(AccountVideoRate, classMethods)
44 30
45 return AccountVideoRate 31 @UpdatedAt
46} 32 updatedAt: Date
47 33
48// ------------------------------ STATICS ------------------------------ 34 @ForeignKey(() => VideoModel)
35 @Column
36 videoId: number
49 37
50function associate (models) { 38 @BelongsTo(() => VideoModel, {
51 AccountVideoRate.belongsTo(models.Video, {
52 foreignKey: { 39 foreignKey: {
53 name: 'videoId',
54 allowNull: false 40 allowNull: false
55 }, 41 },
56 onDelete: 'CASCADE' 42 onDelete: 'CASCADE'
57 }) 43 })
44 Video: VideoModel
58 45
59 AccountVideoRate.belongsTo(models.Account, { 46 @ForeignKey(() => AccountModel)
47 @Column
48 accountId: number
49
50 @BelongsTo(() => AccountModel, {
60 foreignKey: { 51 foreignKey: {
61 name: 'accountId',
62 allowNull: false 52 allowNull: false
63 }, 53 },
64 onDelete: 'CASCADE' 54 onDelete: 'CASCADE'
65 }) 55 })
66} 56 Account: AccountModel
67 57
68load = function (accountId: number, videoId: number, transaction: Sequelize.Transaction) { 58 static load (accountId: number, videoId: number, transaction: Transaction) {
69 const options: Sequelize.FindOptions<AccountVideoRateAttributes> = { 59 const options: IFindOptions<AccountVideoRateModel> = {
70 where: { 60 where: {
71 accountId, 61 accountId,
72 videoId 62 videoId
63 }
73 } 64 }
74 } 65 if (transaction) options.transaction = transaction
75 if (transaction) options.transaction = transaction
76 66
77 return AccountVideoRate.findOne(options) 67 return AccountVideoRateModel.findOne(options)
68 }
78} 69}