diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-22 20:58:25 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-25 17:32:16 +0200 |
commit | e02643f32e4c97ca307f8fc5b69be79c40d70a3b (patch) | |
tree | b7f6269913cd5a0e4f26a9461a043deb0c168be0 /server/models/request-video-qadu.ts | |
parent | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (diff) | |
download | PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.tar.gz PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.tar.zst PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.zip |
Type models
Diffstat (limited to 'server/models/request-video-qadu.ts')
-rw-r--r-- | server/models/request-video-qadu.ts | 68 |
1 files changed, 41 insertions, 27 deletions
diff --git a/server/models/request-video-qadu.ts b/server/models/request-video-qadu.ts index 2b1ed07c9..e914e06cd 100644 --- a/server/models/request-video-qadu.ts +++ b/server/models/request-video-qadu.ts | |||
@@ -10,13 +10,27 @@ | |||
10 | */ | 10 | */ |
11 | 11 | ||
12 | import { values } from 'lodash' | 12 | import { values } from 'lodash' |
13 | import * as Sequelize from 'sequelize' | ||
13 | 14 | ||
14 | import { REQUEST_VIDEO_QADU_TYPES } from '../initializers' | 15 | import { REQUEST_VIDEO_QADU_TYPES } from '../initializers' |
15 | 16 | ||
16 | // --------------------------------------------------------------------------- | 17 | import { addMethodsToModel } from './utils' |
18 | import { | ||
19 | RequestVideoQaduClass, | ||
20 | RequestVideoQaduInstance, | ||
21 | RequestVideoQaduAttributes, | ||
22 | |||
23 | RequestVideoQaduMethods | ||
24 | } from './request-video-qadu-interface' | ||
25 | |||
26 | let RequestVideoQadu: Sequelize.Model<RequestVideoQaduInstance, RequestVideoQaduAttributes> | ||
27 | let countTotalRequests: RequestVideoQaduMethods.CountTotalRequests | ||
28 | let listWithLimitAndRandom: RequestVideoQaduMethods.ListWithLimitAndRandom | ||
29 | let removeByRequestIdsAndPod: RequestVideoQaduMethods.RemoveByRequestIdsAndPod | ||
30 | let removeAll: RequestVideoQaduMethods.RemoveAll | ||
17 | 31 | ||
18 | module.exports = function (sequelize, DataTypes) { | 32 | export default function (sequelize, DataTypes) { |
19 | const RequestVideoQadu = sequelize.define('RequestVideoQadu', | 33 | RequestVideoQadu = sequelize.define('RequestVideoQadu', |
20 | { | 34 | { |
21 | type: { | 35 | type: { |
22 | type: DataTypes.ENUM(values(REQUEST_VIDEO_QADU_TYPES)), | 36 | type: DataTypes.ENUM(values(REQUEST_VIDEO_QADU_TYPES)), |
@@ -32,26 +46,27 @@ module.exports = function (sequelize, DataTypes) { | |||
32 | { | 46 | { |
33 | fields: [ 'videoId' ] | 47 | fields: [ 'videoId' ] |
34 | } | 48 | } |
35 | ], | 49 | ] |
36 | classMethods: { | ||
37 | associate, | ||
38 | |||
39 | listWithLimitAndRandom, | ||
40 | |||
41 | countTotalRequests, | ||
42 | removeAll, | ||
43 | removeByRequestIdsAndPod | ||
44 | } | ||
45 | } | 50 | } |
46 | ) | 51 | ) |
47 | 52 | ||
53 | const classMethods = [ | ||
54 | associate, | ||
55 | |||
56 | listWithLimitAndRandom, | ||
57 | countTotalRequests, | ||
58 | removeAll, | ||
59 | removeByRequestIdsAndPod | ||
60 | ] | ||
61 | addMethodsToModel(RequestVideoQadu, classMethods) | ||
62 | |||
48 | return RequestVideoQadu | 63 | return RequestVideoQadu |
49 | } | 64 | } |
50 | 65 | ||
51 | // ------------------------------ STATICS ------------------------------ | 66 | // ------------------------------ STATICS ------------------------------ |
52 | 67 | ||
53 | function associate (models) { | 68 | function associate (models) { |
54 | this.belongsTo(models.Pod, { | 69 | RequestVideoQadu.belongsTo(models.Pod, { |
55 | foreignKey: { | 70 | foreignKey: { |
56 | name: 'podId', | 71 | name: 'podId', |
57 | allowNull: false | 72 | allowNull: false |
@@ -59,7 +74,7 @@ function associate (models) { | |||
59 | onDelete: 'CASCADE' | 74 | onDelete: 'CASCADE' |
60 | }) | 75 | }) |
61 | 76 | ||
62 | this.belongsTo(models.Video, { | 77 | RequestVideoQadu.belongsTo(models.Video, { |
63 | foreignKey: { | 78 | foreignKey: { |
64 | name: 'videoId', | 79 | name: 'videoId', |
65 | allowNull: false | 80 | allowNull: false |
@@ -68,14 +83,13 @@ function associate (models) { | |||
68 | }) | 83 | }) |
69 | } | 84 | } |
70 | 85 | ||
71 | function countTotalRequests (callback) { | 86 | countTotalRequests = function (callback) { |
72 | const query = {} | 87 | const query = {} |
73 | return this.count(query).asCallback(callback) | 88 | return RequestVideoQadu.count(query).asCallback(callback) |
74 | } | 89 | } |
75 | 90 | ||
76 | function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { | 91 | listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) { |
77 | const self = this | 92 | const Pod = RequestVideoQadu['sequelize'].models.Pod |
78 | const Pod = this.sequelize.models.Pod | ||
79 | 93 | ||
80 | Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', function (err, podIds) { | 94 | Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', function (err, podIds) { |
81 | if (err) return callback(err) | 95 | if (err) return callback(err) |
@@ -86,7 +100,7 @@ function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { | |||
86 | const query = { | 100 | const query = { |
87 | include: [ | 101 | include: [ |
88 | { | 102 | { |
89 | model: self.sequelize.models.Pod, | 103 | model: RequestVideoQadu['sequelize'].models.Pod, |
90 | where: { | 104 | where: { |
91 | id: { | 105 | id: { |
92 | $in: podIds | 106 | $in: podIds |
@@ -94,12 +108,12 @@ function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { | |||
94 | } | 108 | } |
95 | }, | 109 | }, |
96 | { | 110 | { |
97 | model: self.sequelize.models.Video | 111 | model: RequestVideoQadu['sequelize'].models.Video |
98 | } | 112 | } |
99 | ] | 113 | ] |
100 | } | 114 | } |
101 | 115 | ||
102 | self.findAll(query).asCallback(function (err, requests) { | 116 | RequestVideoQadu.findAll(query).asCallback(function (err, requests) { |
103 | if (err) return callback(err) | 117 | if (err) return callback(err) |
104 | 118 | ||
105 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) | 119 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) |
@@ -108,7 +122,7 @@ function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { | |||
108 | }) | 122 | }) |
109 | } | 123 | } |
110 | 124 | ||
111 | function removeByRequestIdsAndPod (ids, podId, callback) { | 125 | removeByRequestIdsAndPod = function (ids, podId, callback) { |
112 | const query = { | 126 | const query = { |
113 | where: { | 127 | where: { |
114 | id: { | 128 | id: { |
@@ -118,12 +132,12 @@ function removeByRequestIdsAndPod (ids, podId, callback) { | |||
118 | } | 132 | } |
119 | } | 133 | } |
120 | 134 | ||
121 | this.destroy(query).asCallback(callback) | 135 | RequestVideoQadu.destroy(query).asCallback(callback) |
122 | } | 136 | } |
123 | 137 | ||
124 | function removeAll (callback) { | 138 | removeAll = function (callback) { |
125 | // Delete all requests | 139 | // Delete all requests |
126 | this.truncate({ cascade: true }).asCallback(callback) | 140 | RequestVideoQadu.truncate({ cascade: true }).asCallback(callback) |
127 | } | 141 | } |
128 | 142 | ||
129 | // --------------------------------------------------------------------------- | 143 | // --------------------------------------------------------------------------- |