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