rate-core 0.0.2 → 0.0.5
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 +25 -13
- package/package.json +1 -1
- package/src/index.ts +30 -17
- package/src/rate.ts +11 -8
package/lib/index.js
CHANGED
|
@@ -41,11 +41,11 @@ function __export(m) {
|
|
|
41
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
42
|
__export(require("./rate"));
|
|
43
43
|
var RateService = (function () {
|
|
44
|
-
function RateService(find, repository, infoRepository,
|
|
44
|
+
function RateService(find, repository, infoRepository, commentRepository, rateReactionRepository, queryURL) {
|
|
45
45
|
this.find = find;
|
|
46
46
|
this.repository = repository;
|
|
47
47
|
this.infoRepository = infoRepository;
|
|
48
|
-
this.
|
|
48
|
+
this.commentRepository = commentRepository;
|
|
49
49
|
this.rateReactionRepository = rateReactionRepository;
|
|
50
50
|
this.queryURL = queryURL;
|
|
51
51
|
this.rate = this.rate.bind(this);
|
|
@@ -53,6 +53,8 @@ var RateService = (function () {
|
|
|
53
53
|
this.removeComment = this.removeComment.bind(this);
|
|
54
54
|
this.updateComment = this.updateComment.bind(this);
|
|
55
55
|
this.search = this.search.bind(this);
|
|
56
|
+
this.getComment = this.getComment.bind(this);
|
|
57
|
+
this.getComments = this.getComments.bind(this);
|
|
56
58
|
}
|
|
57
59
|
RateService.prototype.rate = function (rate) {
|
|
58
60
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -142,16 +144,16 @@ var RateService = (function () {
|
|
|
142
144
|
}
|
|
143
145
|
else {
|
|
144
146
|
comment.time ? comment.time = comment.time : comment.time = new Date();
|
|
145
|
-
return _this.
|
|
147
|
+
return _this.commentRepository.insert(comment);
|
|
146
148
|
}
|
|
147
149
|
});
|
|
148
150
|
};
|
|
149
151
|
RateService.prototype.removeComment = function (commentId, userId) {
|
|
150
152
|
var _this = this;
|
|
151
|
-
return this.
|
|
153
|
+
return this.commentRepository.load(commentId).then(function (comment) {
|
|
152
154
|
if (comment) {
|
|
153
155
|
if (userId === comment.author || userId === comment.userId) {
|
|
154
|
-
return _this.
|
|
156
|
+
return _this.commentRepository.remove(commentId, comment.id, comment.author);
|
|
155
157
|
}
|
|
156
158
|
else {
|
|
157
159
|
return -2;
|
|
@@ -164,7 +166,7 @@ var RateService = (function () {
|
|
|
164
166
|
};
|
|
165
167
|
RateService.prototype.updateComment = function (comment) {
|
|
166
168
|
var _this = this;
|
|
167
|
-
return this.
|
|
169
|
+
return this.commentRepository.load(comment.commentId).then(function (exist) {
|
|
168
170
|
if (!exist) {
|
|
169
171
|
return -1;
|
|
170
172
|
}
|
|
@@ -181,26 +183,36 @@ var RateService = (function () {
|
|
|
181
183
|
exist.histories = [c];
|
|
182
184
|
}
|
|
183
185
|
exist.comment = comment.comment;
|
|
184
|
-
var res = _this.
|
|
186
|
+
var res = _this.commentRepository.update(exist);
|
|
185
187
|
return res;
|
|
186
188
|
}
|
|
187
189
|
});
|
|
188
190
|
};
|
|
191
|
+
RateService.prototype.getComments = function (id, author, limit) {
|
|
192
|
+
return this.commentRepository.getComments(id, author, limit);
|
|
193
|
+
};
|
|
194
|
+
RateService.prototype.getComment = function (id) {
|
|
195
|
+
return this.commentRepository.load(id);
|
|
196
|
+
};
|
|
189
197
|
return RateService;
|
|
190
198
|
}());
|
|
191
199
|
exports.RateService = RateService;
|
|
192
|
-
var
|
|
193
|
-
function
|
|
200
|
+
var CommentQuery = (function () {
|
|
201
|
+
function CommentQuery(find, repository, queryURL) {
|
|
194
202
|
this.find = find;
|
|
195
203
|
this.repository = repository;
|
|
196
204
|
this.queryURL = queryURL;
|
|
197
205
|
this.load = this.load.bind(this);
|
|
198
206
|
this.search = this.search.bind(this);
|
|
207
|
+
this.getComments = this.getComments.bind(this);
|
|
199
208
|
}
|
|
200
|
-
|
|
209
|
+
CommentQuery.prototype.load = function (id, ctx) {
|
|
201
210
|
return this.repository.load(id, ctx);
|
|
202
211
|
};
|
|
203
|
-
|
|
212
|
+
CommentQuery.prototype.getComments = function (id, author, limit) {
|
|
213
|
+
return this.repository.getComments(id, author, limit);
|
|
214
|
+
};
|
|
215
|
+
CommentQuery.prototype.search = function (s, limit, offset, fields) {
|
|
204
216
|
var _this = this;
|
|
205
217
|
return this.find(s, limit, offset, fields).then(function (res) {
|
|
206
218
|
if (!_this.queryURL) {
|
|
@@ -230,9 +242,9 @@ var RateCommentManager = (function () {
|
|
|
230
242
|
}
|
|
231
243
|
});
|
|
232
244
|
};
|
|
233
|
-
return
|
|
245
|
+
return CommentQuery;
|
|
234
246
|
}());
|
|
235
|
-
exports.
|
|
247
|
+
exports.CommentQuery = CommentQuery;
|
|
236
248
|
function binarySearch(ar, el) {
|
|
237
249
|
var m = 0;
|
|
238
250
|
var n = ar.length - 1;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Attributes, Search, SearchResult } from './core';
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
Comment, CommentFilter, InfoRepository, Rate, RateCommentQuery, RateCommentRepository, RateFilter, Rater,
|
|
4
|
+
RateReactionRepository, RateRepository, ShortComment, ShortRate
|
|
5
5
|
} from './rate';
|
|
6
6
|
|
|
7
7
|
export * from './rate';
|
|
@@ -14,7 +14,7 @@ export class RateService<O> implements Rater {
|
|
|
14
14
|
constructor(protected find: Search<Rate, RateFilter>,
|
|
15
15
|
public repository: RateRepository,
|
|
16
16
|
private infoRepository: InfoRepository<O>,
|
|
17
|
-
private
|
|
17
|
+
private commentRepository: RateCommentRepository,
|
|
18
18
|
private rateReactionRepository: RateReactionRepository,
|
|
19
19
|
private queryURL?: (ids: string[]) => Promise<URL[]>) {
|
|
20
20
|
this.rate = this.rate.bind(this);
|
|
@@ -22,6 +22,8 @@ export class RateService<O> implements Rater {
|
|
|
22
22
|
this.removeComment = this.removeComment.bind(this);
|
|
23
23
|
this.updateComment = this.updateComment.bind(this);
|
|
24
24
|
this.search = this.search.bind(this);
|
|
25
|
+
this.getComment = this.getComment.bind(this);
|
|
26
|
+
this.getComments = this.getComments.bind(this);
|
|
25
27
|
}
|
|
26
28
|
async rate(rate: Rate): Promise<number> {
|
|
27
29
|
rate.time = new Date();
|
|
@@ -80,21 +82,21 @@ export class RateService<O> implements Rater {
|
|
|
80
82
|
removeUseful(id: string, author: string, userId: string): Promise<number> {
|
|
81
83
|
return this.rateReactionRepository.remove(id, author, userId);
|
|
82
84
|
}
|
|
83
|
-
comment(comment:
|
|
85
|
+
comment(comment: Comment): Promise<number> {
|
|
84
86
|
return this.repository.getRate(comment.id, comment.author).then(checkRate => {
|
|
85
87
|
if (!checkRate) {
|
|
86
88
|
return -1;
|
|
87
89
|
} else {
|
|
88
90
|
comment.time ? comment.time = comment.time : comment.time = new Date();
|
|
89
|
-
return this.
|
|
91
|
+
return this.commentRepository.insert(comment);
|
|
90
92
|
}
|
|
91
93
|
});
|
|
92
94
|
}
|
|
93
95
|
removeComment(commentId: string, userId: string): Promise<number> {
|
|
94
|
-
return this.
|
|
96
|
+
return this.commentRepository.load(commentId).then(comment => {
|
|
95
97
|
if (comment) {
|
|
96
98
|
if (userId === comment.author || userId === comment.userId) {
|
|
97
|
-
return this.
|
|
99
|
+
return this.commentRepository.remove(commentId, comment.id, comment.author);
|
|
98
100
|
} else {
|
|
99
101
|
return -2;
|
|
100
102
|
}
|
|
@@ -103,8 +105,8 @@ export class RateService<O> implements Rater {
|
|
|
103
105
|
}
|
|
104
106
|
});
|
|
105
107
|
}
|
|
106
|
-
updateComment(comment:
|
|
107
|
-
return this.
|
|
108
|
+
updateComment(comment: Comment): Promise<number> {
|
|
109
|
+
return this.commentRepository.load(comment.commentId).then(exist => {
|
|
108
110
|
if (!exist) {
|
|
109
111
|
return -1;
|
|
110
112
|
} else {
|
|
@@ -119,25 +121,36 @@ export class RateService<O> implements Rater {
|
|
|
119
121
|
exist.histories = [c];
|
|
120
122
|
}
|
|
121
123
|
exist.comment = comment.comment;
|
|
122
|
-
const res = this.
|
|
124
|
+
const res = this.commentRepository.update(exist);
|
|
123
125
|
return res;
|
|
124
126
|
}
|
|
125
127
|
});
|
|
126
128
|
}
|
|
129
|
+
getComments(id: string, author: string, limit?: number): Promise<Comment[]> {
|
|
130
|
+
return this.commentRepository.getComments(id, author, limit);
|
|
131
|
+
}
|
|
132
|
+
getComment(id: string): Promise<Comment|null> {
|
|
133
|
+
return this.commentRepository.load(id);
|
|
134
|
+
}
|
|
127
135
|
}
|
|
128
|
-
export interface
|
|
129
|
-
load(
|
|
136
|
+
export interface CommentRepository {
|
|
137
|
+
load(commentId: string, ctx?: any): Promise<Comment|null>;
|
|
138
|
+
getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
|
|
130
139
|
}
|
|
131
140
|
// tslint:disable-next-line:max-classes-per-file
|
|
132
|
-
export class
|
|
133
|
-
constructor(protected find: Search<
|
|
141
|
+
export class CommentQuery implements RateCommentQuery {
|
|
142
|
+
constructor(protected find: Search<Comment, CommentFilter>, protected repository: CommentRepository, private queryURL?: (ids: string[]) => Promise<URL[]>) {
|
|
134
143
|
this.load = this.load.bind(this);
|
|
135
144
|
this.search = this.search.bind(this);
|
|
145
|
+
this.getComments = this.getComments.bind(this);
|
|
136
146
|
}
|
|
137
|
-
load(id: string, ctx?: any): Promise<
|
|
147
|
+
load(id: string, ctx?: any): Promise<Comment|null> {
|
|
138
148
|
return this.repository.load(id, ctx);
|
|
139
149
|
}
|
|
140
|
-
|
|
150
|
+
getComments(id: string, author: string, limit?: number): Promise<Comment[]> {
|
|
151
|
+
return this.repository.getComments(id, author, limit);
|
|
152
|
+
}
|
|
153
|
+
search(s: CommentFilter, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<Comment>> {
|
|
141
154
|
return this.find(s, limit, offset, fields).then(res => {
|
|
142
155
|
if (!this.queryURL) {
|
|
143
156
|
return res;
|
|
@@ -194,7 +207,7 @@ export class CommentValidator {
|
|
|
194
207
|
constructor(protected attributes: Attributes, protected check: (obj: any, attributes: Attributes) => ErrorMessage[]) {
|
|
195
208
|
this.validate = this.validate.bind(this);
|
|
196
209
|
}
|
|
197
|
-
validate(comment:
|
|
210
|
+
validate(comment: Comment): Promise<ErrorMessage[]> {
|
|
198
211
|
const errs = this.check(comment, this.attributes);
|
|
199
212
|
return Promise.resolve(errs);
|
|
200
213
|
}
|
package/src/rate.ts
CHANGED
|
@@ -43,18 +43,20 @@ export interface Rater {
|
|
|
43
43
|
rate(rate: Rate): Promise<number>;
|
|
44
44
|
setUseful(id: string, author: string, userId: string, ctx?: any): Promise<number>;
|
|
45
45
|
removeUseful(id: string, author: string, userId: string, ctx?: any): Promise<number>;
|
|
46
|
-
comment(comment:
|
|
46
|
+
comment(comment: Comment): Promise<number>;
|
|
47
47
|
removeComment(id: string, author: string, ctx?: any): Promise<number>;
|
|
48
|
-
updateComment(comment:
|
|
48
|
+
updateComment(comment: Comment): Promise<number>;
|
|
49
|
+
getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
|
|
50
|
+
getComment(id: string): Promise<Comment|null>;
|
|
49
51
|
}
|
|
50
|
-
|
|
51
52
|
export interface RateReactionRepository {
|
|
52
53
|
remove(id: string, author: string, userId: string, ctx?: any): Promise<number>;
|
|
53
54
|
save(id: string, author: string, userId: string, type: number): Promise<number>;
|
|
54
55
|
}
|
|
55
56
|
|
|
56
|
-
export interface RateCommentRepository extends Repository<
|
|
57
|
+
export interface RateCommentRepository extends Repository<Comment, string> {
|
|
57
58
|
remove(commentId: string, id: string, author: string): Promise<number>;
|
|
59
|
+
getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
export interface Query<T, ID, S> {
|
|
@@ -62,7 +64,8 @@ export interface Query<T, ID, S> {
|
|
|
62
64
|
metadata?(): Attributes|undefined;
|
|
63
65
|
load(id: ID, ctx?: any): Promise<T|null>;
|
|
64
66
|
}
|
|
65
|
-
export interface RateCommentQuery extends Query<
|
|
67
|
+
export interface RateCommentQuery extends Query<Comment, string, CommentFilter> {
|
|
68
|
+
getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
|
|
66
69
|
}
|
|
67
70
|
export const rateHistoryModel: Attributes = {
|
|
68
71
|
rate: {
|
|
@@ -212,13 +215,13 @@ export interface Info10 {
|
|
|
212
215
|
export interface InfoRepository<T> extends ViewRepository<T, string> {
|
|
213
216
|
}
|
|
214
217
|
|
|
215
|
-
export interface
|
|
218
|
+
export interface CommentId {
|
|
216
219
|
id: string;
|
|
217
220
|
author: string;
|
|
218
221
|
userId: string;
|
|
219
222
|
}
|
|
220
223
|
|
|
221
|
-
export interface
|
|
224
|
+
export interface Comment {
|
|
222
225
|
commentId: string;
|
|
223
226
|
id: string;
|
|
224
227
|
author: string;
|
|
@@ -235,7 +238,7 @@ export interface ShortComment {
|
|
|
235
238
|
time: Date;
|
|
236
239
|
}
|
|
237
240
|
|
|
238
|
-
export interface
|
|
241
|
+
export interface CommentFilter extends Filter {
|
|
239
242
|
commentId?: string;
|
|
240
243
|
id?: string;
|
|
241
244
|
author?: string;
|