diff options
Diffstat (limited to 'server/models/user-video-rate.ts')
-rw-r--r-- | server/models/user-video-rate.ts | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/server/models/user-video-rate.ts b/server/models/user-video-rate.ts index 6603c7862..87886d8d0 100644 --- a/server/models/user-video-rate.ts +++ b/server/models/user-video-rate.ts | |||
@@ -3,13 +3,24 @@ | |||
3 | 3 | ||
4 | */ | 4 | */ |
5 | import { values } from 'lodash' | 5 | import { values } from 'lodash' |
6 | import * as Sequelize from 'sequelize' | ||
6 | 7 | ||
7 | import { VIDEO_RATE_TYPES } from '../initializers' | 8 | import { VIDEO_RATE_TYPES } from '../initializers' |
8 | 9 | ||
9 | // --------------------------------------------------------------------------- | 10 | import { addMethodsToModel } from './utils' |
11 | import { | ||
12 | UserVideoRateClass, | ||
13 | UserVideoRateInstance, | ||
14 | UserVideoRateAttributes, | ||
10 | 15 | ||
11 | module.exports = function (sequelize, DataTypes) { | 16 | UserVideoRateMethods |
12 | const UserVideoRate = sequelize.define('UserVideoRate', | 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, DataTypes) { | ||
23 | UserVideoRate = sequelize.define('UserVideoRate', | ||
13 | { | 24 | { |
14 | type: { | 25 | type: { |
15 | type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)), | 26 | type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)), |
@@ -22,22 +33,24 @@ module.exports = function (sequelize, DataTypes) { | |||
22 | fields: [ 'videoId', 'userId', 'type' ], | 33 | fields: [ 'videoId', 'userId', 'type' ], |
23 | unique: true | 34 | unique: true |
24 | } | 35 | } |
25 | ], | 36 | ] |
26 | classMethods: { | ||
27 | associate, | ||
28 | |||
29 | load | ||
30 | } | ||
31 | } | 37 | } |
32 | ) | 38 | ) |
33 | 39 | ||
40 | const classMethods = [ | ||
41 | associate, | ||
42 | |||
43 | load | ||
44 | ] | ||
45 | addMethodsToModel(UserVideoRate, classMethods) | ||
46 | |||
34 | return UserVideoRate | 47 | return UserVideoRate |
35 | } | 48 | } |
36 | 49 | ||
37 | // ------------------------------ STATICS ------------------------------ | 50 | // ------------------------------ STATICS ------------------------------ |
38 | 51 | ||
39 | function associate (models) { | 52 | function associate (models) { |
40 | this.belongsTo(models.Video, { | 53 | UserVideoRate.belongsTo(models.Video, { |
41 | foreignKey: { | 54 | foreignKey: { |
42 | name: 'videoId', | 55 | name: 'videoId', |
43 | allowNull: false | 56 | allowNull: false |
@@ -45,7 +58,7 @@ function associate (models) { | |||
45 | onDelete: 'CASCADE' | 58 | onDelete: 'CASCADE' |
46 | }) | 59 | }) |
47 | 60 | ||
48 | this.belongsTo(models.User, { | 61 | UserVideoRate.belongsTo(models.User, { |
49 | foreignKey: { | 62 | foreignKey: { |
50 | name: 'userId', | 63 | name: 'userId', |
51 | allowNull: false | 64 | allowNull: false |
@@ -54,21 +67,14 @@ function associate (models) { | |||
54 | }) | 67 | }) |
55 | } | 68 | } |
56 | 69 | ||
57 | function load (userId, videoId, transaction, callback) { | 70 | load = function (userId, videoId, transaction, callback) { |
58 | if (!callback) { | 71 | const options: Sequelize.FindOptions = { |
59 | callback = transaction | ||
60 | transaction = null | ||
61 | } | ||
62 | |||
63 | const query = { | ||
64 | where: { | 72 | where: { |
65 | userId, | 73 | userId, |
66 | videoId | 74 | videoId |
67 | } | 75 | } |
68 | } | 76 | } |
69 | |||
70 | const options: any = {} | ||
71 | if (transaction) options.transaction = transaction | 77 | if (transaction) options.transaction = transaction |
72 | 78 | ||
73 | return this.findOne(query, options).asCallback(callback) | 79 | return UserVideoRate.findOne(options).asCallback(callback) |
74 | } | 80 | } |