]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { values } from 'lodash' |
3fd3ab2d C |
2 | import { Transaction } from 'sequelize' |
3 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' | |
4 | import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions' | |
5 | import { VideoRateType } from '../../../shared/models/videos' | |
74889a71 | 6 | import { VIDEO_RATE_TYPES } from '../../initializers' |
3fd3ab2d C |
7 | import { VideoModel } from '../video/video' |
8 | import { AccountModel } from './account' | |
d38b8281 | 9 | |
3fd3ab2d C |
10 | /* |
11 | Account rates per video. | |
12 | */ | |
13 | @Table({ | |
14 | tableName: 'accountVideoRate', | |
15 | indexes: [ | |
d38b8281 | 16 | { |
3fd3ab2d C |
17 | fields: [ 'videoId', 'accountId' ], |
18 | unique: true | |
d38b8281 | 19 | } |
3fd3ab2d C |
20 | ] |
21 | }) | |
22 | export class AccountVideoRateModel extends Model<AccountVideoRateModel> { | |
d38b8281 | 23 | |
3fd3ab2d C |
24 | @AllowNull(false) |
25 | @Column(DataType.ENUM(values(VIDEO_RATE_TYPES))) | |
26 | type: VideoRateType | |
e02643f3 | 27 | |
3fd3ab2d C |
28 | @CreatedAt |
29 | createdAt: Date | |
e02643f3 | 30 | |
3fd3ab2d C |
31 | @UpdatedAt |
32 | updatedAt: Date | |
d38b8281 | 33 | |
3fd3ab2d C |
34 | @ForeignKey(() => VideoModel) |
35 | @Column | |
36 | videoId: number | |
d38b8281 | 37 | |
3fd3ab2d | 38 | @BelongsTo(() => VideoModel, { |
d38b8281 | 39 | foreignKey: { |
d38b8281 C |
40 | allowNull: false |
41 | }, | |
42 | onDelete: 'CASCADE' | |
43 | }) | |
3fd3ab2d | 44 | Video: VideoModel |
d38b8281 | 45 | |
3fd3ab2d C |
46 | @ForeignKey(() => AccountModel) |
47 | @Column | |
48 | accountId: number | |
49 | ||
50 | @BelongsTo(() => AccountModel, { | |
d38b8281 | 51 | foreignKey: { |
d38b8281 C |
52 | allowNull: false |
53 | }, | |
54 | onDelete: 'CASCADE' | |
55 | }) | |
3fd3ab2d | 56 | Account: AccountModel |
d38b8281 | 57 | |
3fd3ab2d C |
58 | static load (accountId: number, videoId: number, transaction: Transaction) { |
59 | const options: IFindOptions<AccountVideoRateModel> = { | |
60 | where: { | |
61 | accountId, | |
62 | videoId | |
63 | } | |
d38b8281 | 64 | } |
3fd3ab2d | 65 | if (transaction) options.transaction = transaction |
d38b8281 | 66 | |
3fd3ab2d C |
67 | return AccountVideoRateModel.findOne(options) |
68 | } | |
d38b8281 | 69 | } |