rate-core 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +18 -102
- package/package.json +4 -2
- package/src/core.ts +1 -1
- package/src/index.ts +9 -78
- package/src/rate.ts +1 -1
package/lib/index.js
CHANGED
|
@@ -53,8 +53,8 @@ function __export(m) {
|
|
|
53
53
|
}
|
|
54
54
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
55
55
|
__export(require("./rate"));
|
|
56
|
-
var
|
|
57
|
-
function
|
|
56
|
+
var ReactionService = (function () {
|
|
57
|
+
function ReactionService(find, repository, infoRepository, commentRepository, reactionRepository, queryURL) {
|
|
58
58
|
this.find = find;
|
|
59
59
|
this.repository = repository;
|
|
60
60
|
this.infoRepository = infoRepository;
|
|
@@ -72,7 +72,7 @@ var BaseRateService = (function () {
|
|
|
72
72
|
this.getComments = this.getComments.bind(this);
|
|
73
73
|
this.getComment = this.getComment.bind(this);
|
|
74
74
|
}
|
|
75
|
-
|
|
75
|
+
ReactionService.prototype.search = function (s, limit, offset, fields) {
|
|
76
76
|
var _this = this;
|
|
77
77
|
return this.find(s, limit, offset, fields).then(function (res) {
|
|
78
78
|
if (!_this.queryURL) {
|
|
@@ -102,19 +102,19 @@ var BaseRateService = (function () {
|
|
|
102
102
|
}
|
|
103
103
|
});
|
|
104
104
|
};
|
|
105
|
-
|
|
105
|
+
ReactionService.prototype.load = function (id, author) {
|
|
106
106
|
return this.repository.load(id, author);
|
|
107
107
|
};
|
|
108
|
-
|
|
108
|
+
ReactionService.prototype.getRate = function (id, author) {
|
|
109
109
|
return this.repository.load(id, author);
|
|
110
110
|
};
|
|
111
|
-
|
|
111
|
+
ReactionService.prototype.setUseful = function (id, author, userId) {
|
|
112
112
|
return this.reactionRepository.save(id, author, userId, 1);
|
|
113
113
|
};
|
|
114
|
-
|
|
114
|
+
ReactionService.prototype.removeUseful = function (id, author, userId) {
|
|
115
115
|
return this.reactionRepository.remove(id, author, userId);
|
|
116
116
|
};
|
|
117
|
-
|
|
117
|
+
ReactionService.prototype.comment = function (comment) {
|
|
118
118
|
var _this = this;
|
|
119
119
|
return this.repository.load(comment.id, comment.author).then(function (checkRate) {
|
|
120
120
|
if (!checkRate) {
|
|
@@ -126,7 +126,7 @@ var BaseRateService = (function () {
|
|
|
126
126
|
}
|
|
127
127
|
});
|
|
128
128
|
};
|
|
129
|
-
|
|
129
|
+
ReactionService.prototype.removeComment = function (commentId, userId) {
|
|
130
130
|
var _this = this;
|
|
131
131
|
return this.commentRepository.load(commentId).then(function (comment) {
|
|
132
132
|
if (comment) {
|
|
@@ -142,7 +142,7 @@ var BaseRateService = (function () {
|
|
|
142
142
|
}
|
|
143
143
|
});
|
|
144
144
|
};
|
|
145
|
-
|
|
145
|
+
ReactionService.prototype.updateComment = function (comment) {
|
|
146
146
|
var _this = this;
|
|
147
147
|
return this.commentRepository.load(comment.commentId).then(function (exist) {
|
|
148
148
|
if (!exist) {
|
|
@@ -166,7 +166,7 @@ var BaseRateService = (function () {
|
|
|
166
166
|
}
|
|
167
167
|
});
|
|
168
168
|
};
|
|
169
|
-
|
|
169
|
+
ReactionService.prototype.getComments = function (id, author, limit) {
|
|
170
170
|
var _this = this;
|
|
171
171
|
return this.commentRepository.getComments(id, author, limit).then(function (comments) {
|
|
172
172
|
if (_this.queryURL) {
|
|
@@ -191,7 +191,7 @@ var BaseRateService = (function () {
|
|
|
191
191
|
}
|
|
192
192
|
});
|
|
193
193
|
};
|
|
194
|
-
|
|
194
|
+
ReactionService.prototype.getComment = function (id) {
|
|
195
195
|
var _this = this;
|
|
196
196
|
return this.commentRepository.load(id).then(function (comment) {
|
|
197
197
|
if (comment && _this.queryURL) {
|
|
@@ -208,9 +208,9 @@ var BaseRateService = (function () {
|
|
|
208
208
|
}
|
|
209
209
|
});
|
|
210
210
|
};
|
|
211
|
-
return
|
|
211
|
+
return ReactionService;
|
|
212
212
|
}());
|
|
213
|
-
exports.
|
|
213
|
+
exports.ReactionService = ReactionService;
|
|
214
214
|
var RateService = (function (_super) {
|
|
215
215
|
__extends(RateService, _super);
|
|
216
216
|
function RateService(find, repository, infoRepository, commentRepository, reactionRepository, queryURL) {
|
|
@@ -225,7 +225,7 @@ var RateService = (function (_super) {
|
|
|
225
225
|
switch (_a.label) {
|
|
226
226
|
case 0:
|
|
227
227
|
rate.time = new Date();
|
|
228
|
-
return [4, this.infoRepository.
|
|
228
|
+
return [4, this.infoRepository.exist(rate.id)];
|
|
229
229
|
case 1:
|
|
230
230
|
info = _a.sent();
|
|
231
231
|
if (!!info) return [3, 3];
|
|
@@ -260,7 +260,7 @@ var RateService = (function (_super) {
|
|
|
260
260
|
});
|
|
261
261
|
};
|
|
262
262
|
return RateService;
|
|
263
|
-
}(
|
|
263
|
+
}(ReactionService));
|
|
264
264
|
exports.RateService = RateService;
|
|
265
265
|
function avg(n) {
|
|
266
266
|
var sum = 0;
|
|
@@ -283,7 +283,7 @@ var RatesService = (function (_super) {
|
|
|
283
283
|
var info, r0, exist, r1, sr, history, res;
|
|
284
284
|
return __generator(this, function (_a) {
|
|
285
285
|
switch (_a.label) {
|
|
286
|
-
case 0: return [4, this.infoRepository.
|
|
286
|
+
case 0: return [4, this.infoRepository.exist(rate.id)];
|
|
287
287
|
case 1:
|
|
288
288
|
info = _a.sent();
|
|
289
289
|
if (rate.rates && rate.rates.length > 0) {
|
|
@@ -322,92 +322,8 @@ var RatesService = (function (_super) {
|
|
|
322
322
|
});
|
|
323
323
|
};
|
|
324
324
|
return RatesService;
|
|
325
|
-
}(
|
|
325
|
+
}(ReactionService));
|
|
326
326
|
exports.RatesService = RatesService;
|
|
327
|
-
var CommentQuery = (function () {
|
|
328
|
-
function CommentQuery(find, repository, queryURL) {
|
|
329
|
-
this.find = find;
|
|
330
|
-
this.repository = repository;
|
|
331
|
-
this.queryURL = queryURL;
|
|
332
|
-
this.load = this.load.bind(this);
|
|
333
|
-
this.search = this.search.bind(this);
|
|
334
|
-
this.getComments = this.getComments.bind(this);
|
|
335
|
-
}
|
|
336
|
-
CommentQuery.prototype.load = function (id, ctx) {
|
|
337
|
-
var _this = this;
|
|
338
|
-
return this.repository.load(id, ctx).then(function (comment) {
|
|
339
|
-
if (comment && _this.queryURL) {
|
|
340
|
-
return _this.queryURL([id]).then(function (urls) {
|
|
341
|
-
var i = binarySearch(urls, comment.userId);
|
|
342
|
-
if (i >= 0) {
|
|
343
|
-
comment.userURL = urls[i].url;
|
|
344
|
-
}
|
|
345
|
-
return comment;
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
else {
|
|
349
|
-
return comment;
|
|
350
|
-
}
|
|
351
|
-
});
|
|
352
|
-
};
|
|
353
|
-
CommentQuery.prototype.getComments = function (id, author, limit) {
|
|
354
|
-
var _this = this;
|
|
355
|
-
return this.repository.getComments(id, author, limit).then(function (comments) {
|
|
356
|
-
if (_this.queryURL) {
|
|
357
|
-
var ids = [];
|
|
358
|
-
for (var _i = 0, comments_3 = comments; _i < comments_3.length; _i++) {
|
|
359
|
-
var comment = comments_3[_i];
|
|
360
|
-
ids.push(comment.userId);
|
|
361
|
-
}
|
|
362
|
-
return _this.queryURL(ids).then(function (urls) {
|
|
363
|
-
for (var _i = 0, comments_4 = comments; _i < comments_4.length; _i++) {
|
|
364
|
-
var comment = comments_4[_i];
|
|
365
|
-
var i = binarySearch(urls, comment.userId);
|
|
366
|
-
if (i >= 0) {
|
|
367
|
-
comment.userURL = urls[i].url;
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
return comments;
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
else {
|
|
374
|
-
return comments;
|
|
375
|
-
}
|
|
376
|
-
});
|
|
377
|
-
};
|
|
378
|
-
CommentQuery.prototype.search = function (s, limit, offset, fields) {
|
|
379
|
-
var _this = this;
|
|
380
|
-
return this.find(s, limit, offset, fields).then(function (res) {
|
|
381
|
-
if (!_this.queryURL) {
|
|
382
|
-
return res;
|
|
383
|
-
}
|
|
384
|
-
else {
|
|
385
|
-
if (res.list && res.list.length > 0) {
|
|
386
|
-
var ids = [];
|
|
387
|
-
for (var _i = 0, _a = res.list; _i < _a.length; _i++) {
|
|
388
|
-
var rate = _a[_i];
|
|
389
|
-
ids.push(rate.userId);
|
|
390
|
-
}
|
|
391
|
-
return _this.queryURL(ids).then(function (urls) {
|
|
392
|
-
for (var _i = 0, _a = res.list; _i < _a.length; _i++) {
|
|
393
|
-
var rate = _a[_i];
|
|
394
|
-
var i = binarySearch(urls, rate.userId);
|
|
395
|
-
if (i >= 0) {
|
|
396
|
-
rate.userURL = urls[i].url;
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
return res;
|
|
400
|
-
});
|
|
401
|
-
}
|
|
402
|
-
else {
|
|
403
|
-
return res;
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
});
|
|
407
|
-
};
|
|
408
|
-
return CommentQuery;
|
|
409
|
-
}());
|
|
410
|
-
exports.CommentQuery = CommentQuery;
|
|
411
327
|
function binarySearch(ar, el) {
|
|
412
328
|
var m = 0;
|
|
413
329
|
var n = ar.length - 1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rate-core",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "rate",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -17,9 +17,11 @@
|
|
|
17
17
|
},
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|
|
20
|
-
"url": "git@github.com/core-ts/
|
|
20
|
+
"url": "git@github.com/core-ts/reaction"
|
|
21
21
|
},
|
|
22
22
|
"keywords": [
|
|
23
|
+
"reaction",
|
|
24
|
+
"reaction core",
|
|
23
25
|
"rate",
|
|
24
26
|
"rate core"
|
|
25
27
|
]
|
package/src/core.ts
CHANGED
|
@@ -78,7 +78,7 @@ export interface ViewRepository<T, ID> {
|
|
|
78
78
|
keys?(): string[];
|
|
79
79
|
all?(ctx?: any): Promise<T[]>;
|
|
80
80
|
load(id: ID, ctx?: any): Promise<T|null>;
|
|
81
|
-
exist
|
|
81
|
+
exist(id: ID, ctx?: any): Promise<boolean>;
|
|
82
82
|
}
|
|
83
83
|
export interface Repository<T, ID> extends ViewRepository<T, ID> {
|
|
84
84
|
insert(obj: T, ctx?: any): Promise<number>;
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Attributes, Search, SearchResult } from './core';
|
|
2
2
|
import {
|
|
3
|
-
BaseRate,
|
|
3
|
+
BaseRate, BaseRepository, Comment, InfoRepository, Rate, RateCommentRepository, RateFilter, Rater, RateReactionRepository,
|
|
4
4
|
Rates, RatesFilter, ShortComment, ShortRate, ShortRates
|
|
5
5
|
} from './rate';
|
|
6
6
|
|
|
@@ -10,9 +10,9 @@ export interface URL {
|
|
|
10
10
|
id: string;
|
|
11
11
|
url: string;
|
|
12
12
|
}
|
|
13
|
-
export class
|
|
13
|
+
export class ReactionService<R extends BaseRate, F, O> {
|
|
14
14
|
constructor(protected find: Search<R, F>,
|
|
15
|
-
public repository:
|
|
15
|
+
public repository: BaseRepository<R>,
|
|
16
16
|
protected infoRepository: InfoRepository<O>,
|
|
17
17
|
protected commentRepository: RateCommentRepository,
|
|
18
18
|
protected reactionRepository: RateReactionRepository,
|
|
@@ -151,9 +151,9 @@ export interface CommentRepository {
|
|
|
151
151
|
getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
|
|
152
152
|
}
|
|
153
153
|
// tslint:disable-next-line:max-classes-per-file
|
|
154
|
-
export class RateService<O> extends
|
|
154
|
+
export class RateService<O> extends ReactionService<Rate, RateFilter, O> implements Rater<Rate, RateFilter> {
|
|
155
155
|
constructor(find: Search<Rate, RateFilter>,
|
|
156
|
-
repository:
|
|
156
|
+
repository: BaseRepository<Rate>,
|
|
157
157
|
infoRepository: InfoRepository<O>,
|
|
158
158
|
commentRepository: RateCommentRepository,
|
|
159
159
|
reactionRepository: RateReactionRepository,
|
|
@@ -163,7 +163,7 @@ export class RateService<O> extends BaseRateService<Rate, RateFilter, O> impleme
|
|
|
163
163
|
}
|
|
164
164
|
async rate(rate: Rate): Promise<number> {
|
|
165
165
|
rate.time = new Date();
|
|
166
|
-
const info = await this.infoRepository.
|
|
166
|
+
const info = await this.infoRepository.exist(rate.id);
|
|
167
167
|
if (!info) {
|
|
168
168
|
const r0 = await this.repository.insert(rate, true);
|
|
169
169
|
return r0;
|
|
@@ -193,9 +193,9 @@ export function avg(n: number[]): number {
|
|
|
193
193
|
return sum / n.length;
|
|
194
194
|
}
|
|
195
195
|
// tslint:disable-next-line:max-classes-per-file
|
|
196
|
-
export class RatesService<O> extends
|
|
196
|
+
export class RatesService<O> extends ReactionService<Rates, RatesFilter, O> implements Rater<Rates, RatesFilter> {
|
|
197
197
|
constructor(find: Search<Rates, RatesFilter>,
|
|
198
|
-
repository:
|
|
198
|
+
repository: BaseRepository<Rates>,
|
|
199
199
|
infoRepository: InfoRepository<O>,
|
|
200
200
|
commentRepository: RateCommentRepository,
|
|
201
201
|
reactionRepository: RateReactionRepository,
|
|
@@ -204,7 +204,7 @@ export class RatesService<O> extends BaseRateService<Rates, RatesFilter, O> impl
|
|
|
204
204
|
this.rate = this.rate.bind(this);
|
|
205
205
|
}
|
|
206
206
|
async rate(rate: Rates): Promise<number> {
|
|
207
|
-
const info = await this.infoRepository.
|
|
207
|
+
const info = await this.infoRepository.exist(rate.id);
|
|
208
208
|
if (rate.rates && rate.rates.length > 0) {
|
|
209
209
|
rate.rate = avg(rate.rates);
|
|
210
210
|
}
|
|
@@ -230,75 +230,6 @@ export class RatesService<O> extends BaseRateService<Rates, RatesFilter, O> impl
|
|
|
230
230
|
return res;
|
|
231
231
|
}
|
|
232
232
|
}
|
|
233
|
-
// tslint:disable-next-line:max-classes-per-file
|
|
234
|
-
export class CommentQuery implements RateCommentQuery {
|
|
235
|
-
constructor(protected find: Search<Comment, CommentFilter>, protected repository: CommentRepository, private queryURL?: (ids: string[]) => Promise<URL[]>) {
|
|
236
|
-
this.load = this.load.bind(this);
|
|
237
|
-
this.search = this.search.bind(this);
|
|
238
|
-
this.getComments = this.getComments.bind(this);
|
|
239
|
-
}
|
|
240
|
-
load(id: string, ctx?: any): Promise<Comment | null> {
|
|
241
|
-
return this.repository.load(id, ctx).then(comment => {
|
|
242
|
-
if (comment && this.queryURL) {
|
|
243
|
-
return this.queryURL([id]).then(urls => {
|
|
244
|
-
const i = binarySearch(urls, comment.userId);
|
|
245
|
-
if (i >= 0) {
|
|
246
|
-
comment.userURL = urls[i].url;
|
|
247
|
-
}
|
|
248
|
-
return comment;
|
|
249
|
-
});
|
|
250
|
-
} else {
|
|
251
|
-
return comment;
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
getComments(id: string, author: string, limit?: number): Promise<Comment[]> {
|
|
256
|
-
return this.repository.getComments(id, author, limit).then(comments => {
|
|
257
|
-
if (this.queryURL) {
|
|
258
|
-
const ids: string[] = [];
|
|
259
|
-
for (const comment of comments) {
|
|
260
|
-
ids.push(comment.userId);
|
|
261
|
-
}
|
|
262
|
-
return this.queryURL(ids).then(urls => {
|
|
263
|
-
for (const comment of comments) {
|
|
264
|
-
const i = binarySearch(urls, comment.userId);
|
|
265
|
-
if (i >= 0) {
|
|
266
|
-
comment.userURL = urls[i].url;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
return comments;
|
|
270
|
-
});
|
|
271
|
-
} else {
|
|
272
|
-
return comments;
|
|
273
|
-
}
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
search(s: CommentFilter, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<Comment>> {
|
|
277
|
-
return this.find(s, limit, offset, fields).then(res => {
|
|
278
|
-
if (!this.queryURL) {
|
|
279
|
-
return res;
|
|
280
|
-
} else {
|
|
281
|
-
if (res.list && res.list.length > 0) {
|
|
282
|
-
const ids: string[] = [];
|
|
283
|
-
for (const rate of res.list) {
|
|
284
|
-
ids.push(rate.userId);
|
|
285
|
-
}
|
|
286
|
-
return this.queryURL(ids).then(urls => {
|
|
287
|
-
for (const rate of res.list) {
|
|
288
|
-
const i = binarySearch(urls, rate.userId);
|
|
289
|
-
if (i >= 0) {
|
|
290
|
-
rate.userURL = urls[i].url;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
return res;
|
|
294
|
-
});
|
|
295
|
-
} else {
|
|
296
|
-
return res;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
233
|
function binarySearch(ar: URL[], el: string): number {
|
|
303
234
|
let m = 0;
|
|
304
235
|
let n = ar.length - 1;
|
package/src/rate.ts
CHANGED
|
@@ -64,7 +64,7 @@ export interface RatesFilter extends RateFilter {
|
|
|
64
64
|
rate9: number;
|
|
65
65
|
rate10: number;
|
|
66
66
|
}
|
|
67
|
-
export interface
|
|
67
|
+
export interface BaseRepository<R> {
|
|
68
68
|
// save(obj: Rate, info?: T, ctx?: any): Promise<number>;
|
|
69
69
|
insert(rate: R, newInfo?: boolean): Promise<number>;
|
|
70
70
|
update(rate: R, oldRate: number): Promise<number>;
|