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