]>
Commit | Line | Data |
---|---|---|
d38b8281 C |
1 | /* |
2 | User rates per video. | |
3 | ||
4 | */ | |
65fcc311 | 5 | import { values } from 'lodash' |
d38b8281 | 6 | |
65fcc311 | 7 | import { VIDEO_RATE_TYPES } from '../initializers' |
d38b8281 C |
8 | |
9 | // --------------------------------------------------------------------------- | |
10 | ||
11 | module.exports = function (sequelize, DataTypes) { | |
12 | const UserVideoRate = sequelize.define('UserVideoRate', | |
13 | { | |
14 | type: { | |
65fcc311 | 15 | type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)), |
d38b8281 C |
16 | allowNull: false |
17 | } | |
18 | }, | |
19 | { | |
20 | indexes: [ | |
21 | { | |
22 | fields: [ 'videoId', 'userId', 'type' ], | |
23 | unique: true | |
24 | } | |
25 | ], | |
26 | classMethods: { | |
27 | associate, | |
28 | ||
29 | load | |
30 | } | |
31 | } | |
32 | ) | |
33 | ||
34 | return UserVideoRate | |
35 | } | |
36 | ||
37 | // ------------------------------ STATICS ------------------------------ | |
38 | ||
39 | function associate (models) { | |
40 | this.belongsTo(models.Video, { | |
41 | foreignKey: { | |
42 | name: 'videoId', | |
43 | allowNull: false | |
44 | }, | |
45 | onDelete: 'CASCADE' | |
46 | }) | |
47 | ||
48 | this.belongsTo(models.User, { | |
49 | foreignKey: { | |
50 | name: 'userId', | |
51 | allowNull: false | |
52 | }, | |
53 | onDelete: 'CASCADE' | |
54 | }) | |
55 | } | |
56 | ||
57 | function load (userId, videoId, transaction, callback) { | |
58 | if (!callback) { | |
59 | callback = transaction | |
60 | transaction = null | |
61 | } | |
62 | ||
63 | const query = { | |
64 | where: { | |
65 | userId, | |
66 | videoId | |
67 | } | |
68 | } | |
69 | ||
65fcc311 | 70 | const options: any = {} |
d38b8281 C |
71 | if (transaction) options.transaction = transaction |
72 | ||
73 | return this.findOne(query, options).asCallback(callback) | |
74 | } |