aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/user-video-rate.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/user-video-rate.js')
-rw-r--r--server/models/user-video-rate.js77
1 files changed, 77 insertions, 0 deletions
diff --git a/server/models/user-video-rate.js b/server/models/user-video-rate.js
new file mode 100644
index 000000000..84007d70c
--- /dev/null
+++ b/server/models/user-video-rate.js
@@ -0,0 +1,77 @@
1'use strict'
2
3/*
4 User rates per video.
5
6*/
7
8const values = require('lodash/values')
9
10const constants = require('../initializers/constants')
11
12// ---------------------------------------------------------------------------
13
14module.exports = function (sequelize, DataTypes) {
15 const UserVideoRate = sequelize.define('UserVideoRate',
16 {
17 type: {
18 type: DataTypes.ENUM(values(constants.VIDEO_RATE_TYPES)),
19 allowNull: false
20 }
21 },
22 {
23 indexes: [
24 {
25 fields: [ 'videoId', 'userId', 'type' ],
26 unique: true
27 }
28 ],
29 classMethods: {
30 associate,
31
32 load
33 }
34 }
35 )
36
37 return UserVideoRate
38}
39
40// ------------------------------ STATICS ------------------------------
41
42function associate (models) {
43 this.belongsTo(models.Video, {
44 foreignKey: {
45 name: 'videoId',
46 allowNull: false
47 },
48 onDelete: 'CASCADE'
49 })
50
51 this.belongsTo(models.User, {
52 foreignKey: {
53 name: 'userId',
54 allowNull: false
55 },
56 onDelete: 'CASCADE'
57 })
58}
59
60function load (userId, videoId, transaction, callback) {
61 if (!callback) {
62 callback = transaction
63 transaction = null
64 }
65
66 const query = {
67 where: {
68 userId,
69 videoId
70 }
71 }
72
73 const options = {}
74 if (transaction) options.transaction = transaction
75
76 return this.findOne(query, options).asCallback(callback)
77}