aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/user/user-video-rate.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/user/user-video-rate.ts')
-rw-r--r--server/models/user/user-video-rate.ts80
1 files changed, 80 insertions, 0 deletions
diff --git a/server/models/user/user-video-rate.ts b/server/models/user/user-video-rate.ts
new file mode 100644
index 000000000..68be62fc2
--- /dev/null
+++ b/server/models/user/user-video-rate.ts
@@ -0,0 +1,80 @@
1/*
2 User rates per video.
3
4*/
5import { values } from 'lodash'
6import * as Sequelize from 'sequelize'
7
8import { VIDEO_RATE_TYPES } from '../../initializers'
9
10import { addMethodsToModel } from '../utils'
11import {
12 UserVideoRateClass,
13 UserVideoRateInstance,
14 UserVideoRateAttributes,
15
16 UserVideoRateMethods
17} from './user-video-rate-interface'
18
19let UserVideoRate: Sequelize.Model<UserVideoRateInstance, UserVideoRateAttributes>
20let load: UserVideoRateMethods.Load
21
22export 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
52function 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
70load = 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}