diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/models/user-video-rate.ts | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip |
First typescript iteration
Diffstat (limited to 'server/models/user-video-rate.ts')
-rw-r--r-- | server/models/user-video-rate.ts | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/server/models/user-video-rate.ts b/server/models/user-video-rate.ts new file mode 100644 index 000000000..6603c7862 --- /dev/null +++ b/server/models/user-video-rate.ts | |||
@@ -0,0 +1,74 @@ | |||
1 | /* | ||
2 | User rates per video. | ||
3 | |||
4 | */ | ||
5 | import { values } from 'lodash' | ||
6 | |||
7 | import { VIDEO_RATE_TYPES } from '../initializers' | ||
8 | |||
9 | // --------------------------------------------------------------------------- | ||
10 | |||
11 | module.exports = function (sequelize, DataTypes) { | ||
12 | const UserVideoRate = sequelize.define('UserVideoRate', | ||
13 | { | ||
14 | type: { | ||
15 | type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)), | ||
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 | |||
70 | const options: any = {} | ||
71 | if (transaction) options.transaction = transaction | ||
72 | |||
73 | return this.findOne(query, options).asCallback(callback) | ||
74 | } | ||