]>
Commit | Line | Data |
---|---|---|
d38b8281 C |
1 | /* |
2 | User rates per video. | |
d38b8281 | 3 | */ |
65fcc311 | 4 | import { values } from 'lodash' |
e02643f3 | 5 | import * as Sequelize from 'sequelize' |
d38b8281 | 6 | |
74889a71 | 7 | import { VIDEO_RATE_TYPES } from '../../initializers' |
d38b8281 | 8 | |
74889a71 | 9 | import { addMethodsToModel } from '../utils' |
e02643f3 C |
10 | import { |
11 | UserVideoRateClass, | |
12 | UserVideoRateInstance, | |
13 | UserVideoRateAttributes, | |
d38b8281 | 14 | |
e02643f3 C |
15 | UserVideoRateMethods |
16 | } from './user-video-rate-interface' | |
17 | ||
18 | let UserVideoRate: Sequelize.Model<UserVideoRateInstance, UserVideoRateAttributes> | |
19 | let load: UserVideoRateMethods.Load | |
20 | ||
127944aa C |
21 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
22 | UserVideoRate = sequelize.define<UserVideoRateInstance, UserVideoRateAttributes>('UserVideoRate', | |
d38b8281 C |
23 | { |
24 | type: { | |
65fcc311 | 25 | type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)), |
d38b8281 C |
26 | allowNull: false |
27 | } | |
28 | }, | |
29 | { | |
30 | indexes: [ | |
31 | { | |
32 | fields: [ 'videoId', 'userId', 'type' ], | |
33 | unique: true | |
34 | } | |
e02643f3 | 35 | ] |
d38b8281 C |
36 | } |
37 | ) | |
38 | ||
e02643f3 C |
39 | const classMethods = [ |
40 | associate, | |
41 | ||
42 | load | |
43 | ] | |
44 | addMethodsToModel(UserVideoRate, classMethods) | |
45 | ||
d38b8281 C |
46 | return UserVideoRate |
47 | } | |
48 | ||
49 | // ------------------------------ STATICS ------------------------------ | |
50 | ||
51 | function associate (models) { | |
e02643f3 | 52 | UserVideoRate.belongsTo(models.Video, { |
d38b8281 C |
53 | foreignKey: { |
54 | name: 'videoId', | |
55 | allowNull: false | |
56 | }, | |
57 | onDelete: 'CASCADE' | |
58 | }) | |
59 | ||
e02643f3 | 60 | UserVideoRate.belongsTo(models.User, { |
d38b8281 C |
61 | foreignKey: { |
62 | name: 'userId', | |
63 | allowNull: false | |
64 | }, | |
65 | onDelete: 'CASCADE' | |
66 | }) | |
67 | } | |
68 | ||
ee9e7b61 | 69 | load = function (userId: number, videoId: string, transaction: Sequelize.Transaction, callback: UserVideoRateMethods.LoadCallback) { |
e02643f3 | 70 | const options: Sequelize.FindOptions = { |
d38b8281 C |
71 | where: { |
72 | userId, | |
73 | videoId | |
74 | } | |
75 | } | |
d38b8281 C |
76 | if (transaction) options.transaction = transaction |
77 | ||
e02643f3 | 78 | return UserVideoRate.findOne(options).asCallback(callback) |
d38b8281 | 79 | } |