rate-core 0.0.3 → 0.0.6
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 +27 -17
- package/lib/rate.js +0 -42
- package/package.json +1 -1
- package/src/index.ts +38 -27
- package/src/rate.ts +13 -53
package/lib/index.js
CHANGED
|
@@ -41,18 +41,24 @@ 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);
|
|
52
|
+
this.search = this.search.bind(this);
|
|
53
|
+
this.load = this.load.bind(this);
|
|
54
|
+
this.getRate = this.getRate.bind(this);
|
|
55
|
+
this.setUseful = this.setUseful.bind(this);
|
|
56
|
+
this.removeUseful = this.removeUseful.bind(this);
|
|
52
57
|
this.comment = this.comment.bind(this);
|
|
53
58
|
this.removeComment = this.removeComment.bind(this);
|
|
54
59
|
this.updateComment = this.updateComment.bind(this);
|
|
55
|
-
this.
|
|
60
|
+
this.getComments = this.getComments.bind(this);
|
|
61
|
+
this.getComment = this.getComment.bind(this);
|
|
56
62
|
}
|
|
57
63
|
RateService.prototype.rate = function (rate) {
|
|
58
64
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -69,7 +75,7 @@ var RateService = (function () {
|
|
|
69
75
|
case 2:
|
|
70
76
|
r0 = _a.sent();
|
|
71
77
|
return [2, r0];
|
|
72
|
-
case 3: return [4, this.repository.
|
|
78
|
+
case 3: return [4, this.repository.load(rate.id, rate.author)];
|
|
73
79
|
case 4:
|
|
74
80
|
exist = _a.sent();
|
|
75
81
|
if (!!exist) return [3, 6];
|
|
@@ -125,8 +131,11 @@ var RateService = (function () {
|
|
|
125
131
|
}
|
|
126
132
|
});
|
|
127
133
|
};
|
|
134
|
+
RateService.prototype.load = function (id, author) {
|
|
135
|
+
return this.repository.load(id, author);
|
|
136
|
+
};
|
|
128
137
|
RateService.prototype.getRate = function (id, author) {
|
|
129
|
-
return this.repository.
|
|
138
|
+
return this.repository.load(id, author);
|
|
130
139
|
};
|
|
131
140
|
RateService.prototype.setUseful = function (id, author, userId) {
|
|
132
141
|
return this.rateReactionRepository.save(id, author, userId, 1);
|
|
@@ -136,22 +145,22 @@ var RateService = (function () {
|
|
|
136
145
|
};
|
|
137
146
|
RateService.prototype.comment = function (comment) {
|
|
138
147
|
var _this = this;
|
|
139
|
-
return this.repository.
|
|
148
|
+
return this.repository.load(comment.id, comment.author).then(function (checkRate) {
|
|
140
149
|
if (!checkRate) {
|
|
141
150
|
return -1;
|
|
142
151
|
}
|
|
143
152
|
else {
|
|
144
153
|
comment.time ? comment.time = comment.time : comment.time = new Date();
|
|
145
|
-
return _this.
|
|
154
|
+
return _this.commentRepository.insert(comment);
|
|
146
155
|
}
|
|
147
156
|
});
|
|
148
157
|
};
|
|
149
158
|
RateService.prototype.removeComment = function (commentId, userId) {
|
|
150
159
|
var _this = this;
|
|
151
|
-
return this.
|
|
160
|
+
return this.commentRepository.load(commentId).then(function (comment) {
|
|
152
161
|
if (comment) {
|
|
153
162
|
if (userId === comment.author || userId === comment.userId) {
|
|
154
|
-
return _this.
|
|
163
|
+
return _this.commentRepository.remove(commentId, comment.id, comment.author);
|
|
155
164
|
}
|
|
156
165
|
else {
|
|
157
166
|
return -2;
|
|
@@ -164,7 +173,7 @@ var RateService = (function () {
|
|
|
164
173
|
};
|
|
165
174
|
RateService.prototype.updateComment = function (comment) {
|
|
166
175
|
var _this = this;
|
|
167
|
-
return this.
|
|
176
|
+
return this.commentRepository.load(comment.commentId).then(function (exist) {
|
|
168
177
|
if (!exist) {
|
|
169
178
|
return -1;
|
|
170
179
|
}
|
|
@@ -181,11 +190,17 @@ var RateService = (function () {
|
|
|
181
190
|
exist.histories = [c];
|
|
182
191
|
}
|
|
183
192
|
exist.comment = comment.comment;
|
|
184
|
-
var res = _this.
|
|
193
|
+
var res = _this.commentRepository.update(exist);
|
|
185
194
|
return res;
|
|
186
195
|
}
|
|
187
196
|
});
|
|
188
197
|
};
|
|
198
|
+
RateService.prototype.getComments = function (id, author, limit) {
|
|
199
|
+
return this.commentRepository.getComments(id, author, limit);
|
|
200
|
+
};
|
|
201
|
+
RateService.prototype.getComment = function (id) {
|
|
202
|
+
return this.commentRepository.load(id);
|
|
203
|
+
};
|
|
189
204
|
return RateService;
|
|
190
205
|
}());
|
|
191
206
|
exports.RateService = RateService;
|
|
@@ -202,12 +217,7 @@ var CommentQuery = (function () {
|
|
|
202
217
|
return this.repository.load(id, ctx);
|
|
203
218
|
};
|
|
204
219
|
CommentQuery.prototype.getComments = function (id, author, limit) {
|
|
205
|
-
|
|
206
|
-
return this.repository.getComments(id, author, limit);
|
|
207
|
-
}
|
|
208
|
-
else {
|
|
209
|
-
return Promise.resolve([]);
|
|
210
|
-
}
|
|
220
|
+
return this.repository.getComments(id, author, limit);
|
|
211
221
|
};
|
|
212
222
|
CommentQuery.prototype.search = function (s, limit, offset, fields) {
|
|
213
223
|
var _this = this;
|
package/lib/rate.js
CHANGED
|
@@ -116,45 +116,3 @@ exports.info10Model = {
|
|
|
116
116
|
type: 'number',
|
|
117
117
|
}
|
|
118
118
|
};
|
|
119
|
-
exports.commentModel = {
|
|
120
|
-
comment: {
|
|
121
|
-
length: 500
|
|
122
|
-
},
|
|
123
|
-
time: {
|
|
124
|
-
type: 'datetime'
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
exports.rateCommentModel = {
|
|
128
|
-
commentId: {
|
|
129
|
-
key: true
|
|
130
|
-
},
|
|
131
|
-
id: {
|
|
132
|
-
required: true,
|
|
133
|
-
noupdate: true,
|
|
134
|
-
match: 'equal'
|
|
135
|
-
},
|
|
136
|
-
author: {
|
|
137
|
-
required: true,
|
|
138
|
-
noupdate: true,
|
|
139
|
-
match: 'equal'
|
|
140
|
-
},
|
|
141
|
-
userId: {
|
|
142
|
-
required: true,
|
|
143
|
-
noupdate: true,
|
|
144
|
-
match: 'equal'
|
|
145
|
-
},
|
|
146
|
-
comment: {
|
|
147
|
-
length: 500
|
|
148
|
-
},
|
|
149
|
-
time: {
|
|
150
|
-
type: 'datetime',
|
|
151
|
-
noupdate: true,
|
|
152
|
-
},
|
|
153
|
-
updatedAt: {
|
|
154
|
-
type: 'datetime'
|
|
155
|
-
},
|
|
156
|
-
histories: {
|
|
157
|
-
type: 'array',
|
|
158
|
-
typeof: exports.commentModel
|
|
159
|
-
}
|
|
160
|
-
};
|
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,14 +14,20 @@ 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);
|
|
21
|
+
this.search = this.search.bind(this);
|
|
22
|
+
this.load = this.load.bind(this);
|
|
23
|
+
this.getRate = this.getRate.bind(this);
|
|
24
|
+
this.setUseful = this.setUseful.bind(this);
|
|
25
|
+
this.removeUseful = this.removeUseful.bind(this);
|
|
21
26
|
this.comment = this.comment.bind(this);
|
|
22
27
|
this.removeComment = this.removeComment.bind(this);
|
|
23
28
|
this.updateComment = this.updateComment.bind(this);
|
|
24
|
-
this.
|
|
29
|
+
this.getComments = this.getComments.bind(this);
|
|
30
|
+
this.getComment = this.getComment.bind(this);
|
|
25
31
|
}
|
|
26
32
|
async rate(rate: Rate): Promise<number> {
|
|
27
33
|
rate.time = new Date();
|
|
@@ -30,7 +36,7 @@ export class RateService<O> implements Rater {
|
|
|
30
36
|
const r0 = await this.repository.insert(rate, true);
|
|
31
37
|
return r0;
|
|
32
38
|
}
|
|
33
|
-
const exist = await this.repository.
|
|
39
|
+
const exist = await this.repository.load(rate.id, rate.author);
|
|
34
40
|
if (!exist) {
|
|
35
41
|
const r1 = await this.repository.insert(rate);
|
|
36
42
|
return r1;
|
|
@@ -71,8 +77,11 @@ export class RateService<O> implements Rater {
|
|
|
71
77
|
}
|
|
72
78
|
});
|
|
73
79
|
}
|
|
80
|
+
load(id: string, author: string): Promise<Rate | null> {
|
|
81
|
+
return this.repository.load(id, author);
|
|
82
|
+
}
|
|
74
83
|
getRate(id: string, author: string): Promise<Rate | null> {
|
|
75
|
-
return this.repository.
|
|
84
|
+
return this.repository.load(id, author);
|
|
76
85
|
}
|
|
77
86
|
setUseful(id: string, author: string, userId: string): Promise<number> {
|
|
78
87
|
return this.rateReactionRepository.save(id, author, userId, 1);
|
|
@@ -80,21 +89,21 @@ export class RateService<O> implements Rater {
|
|
|
80
89
|
removeUseful(id: string, author: string, userId: string): Promise<number> {
|
|
81
90
|
return this.rateReactionRepository.remove(id, author, userId);
|
|
82
91
|
}
|
|
83
|
-
comment(comment:
|
|
84
|
-
return this.repository.
|
|
92
|
+
comment(comment: Comment): Promise<number> {
|
|
93
|
+
return this.repository.load(comment.id, comment.author).then(checkRate => {
|
|
85
94
|
if (!checkRate) {
|
|
86
95
|
return -1;
|
|
87
96
|
} else {
|
|
88
97
|
comment.time ? comment.time = comment.time : comment.time = new Date();
|
|
89
|
-
return this.
|
|
98
|
+
return this.commentRepository.insert(comment);
|
|
90
99
|
}
|
|
91
100
|
});
|
|
92
101
|
}
|
|
93
102
|
removeComment(commentId: string, userId: string): Promise<number> {
|
|
94
|
-
return this.
|
|
103
|
+
return this.commentRepository.load(commentId).then(comment => {
|
|
95
104
|
if (comment) {
|
|
96
105
|
if (userId === comment.author || userId === comment.userId) {
|
|
97
|
-
return this.
|
|
106
|
+
return this.commentRepository.remove(commentId, comment.id, comment.author);
|
|
98
107
|
} else {
|
|
99
108
|
return -2;
|
|
100
109
|
}
|
|
@@ -103,8 +112,8 @@ export class RateService<O> implements Rater {
|
|
|
103
112
|
}
|
|
104
113
|
});
|
|
105
114
|
}
|
|
106
|
-
updateComment(comment:
|
|
107
|
-
return this.
|
|
115
|
+
updateComment(comment: Comment): Promise<number> {
|
|
116
|
+
return this.commentRepository.load(comment.commentId).then(exist => {
|
|
108
117
|
if (!exist) {
|
|
109
118
|
return -1;
|
|
110
119
|
} else {
|
|
@@ -119,34 +128,36 @@ export class RateService<O> implements Rater {
|
|
|
119
128
|
exist.histories = [c];
|
|
120
129
|
}
|
|
121
130
|
exist.comment = comment.comment;
|
|
122
|
-
const res = this.
|
|
131
|
+
const res = this.commentRepository.update(exist);
|
|
123
132
|
return res;
|
|
124
133
|
}
|
|
125
134
|
});
|
|
126
135
|
}
|
|
136
|
+
getComments(id: string, author: string, limit?: number): Promise<Comment[]> {
|
|
137
|
+
return this.commentRepository.getComments(id, author, limit);
|
|
138
|
+
}
|
|
139
|
+
getComment(id: string): Promise<Comment|null> {
|
|
140
|
+
return this.commentRepository.load(id);
|
|
141
|
+
}
|
|
127
142
|
}
|
|
128
143
|
export interface CommentRepository {
|
|
129
|
-
load(commentId: string, ctx?: any): Promise<
|
|
130
|
-
getComments
|
|
144
|
+
load(commentId: string, ctx?: any): Promise<Comment|null>;
|
|
145
|
+
getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
|
|
131
146
|
}
|
|
132
147
|
// tslint:disable-next-line:max-classes-per-file
|
|
133
148
|
export class CommentQuery implements RateCommentQuery {
|
|
134
|
-
constructor(protected find: Search<
|
|
149
|
+
constructor(protected find: Search<Comment, CommentFilter>, protected repository: CommentRepository, private queryURL?: (ids: string[]) => Promise<URL[]>) {
|
|
135
150
|
this.load = this.load.bind(this);
|
|
136
151
|
this.search = this.search.bind(this);
|
|
137
|
-
this.getComments = this.getComments.bind(this)
|
|
152
|
+
this.getComments = this.getComments.bind(this);
|
|
138
153
|
}
|
|
139
|
-
load(id: string, ctx?: any): Promise<
|
|
154
|
+
load(id: string, ctx?: any): Promise<Comment|null> {
|
|
140
155
|
return this.repository.load(id, ctx);
|
|
141
156
|
}
|
|
142
|
-
getComments(id: string, author: string, limit?: number): Promise<
|
|
143
|
-
|
|
144
|
-
return this.repository.getComments(id, author, limit);
|
|
145
|
-
} else {
|
|
146
|
-
return Promise.resolve([]);
|
|
147
|
-
}
|
|
157
|
+
getComments(id: string, author: string, limit?: number): Promise<Comment[]> {
|
|
158
|
+
return this.repository.getComments(id, author, limit);
|
|
148
159
|
}
|
|
149
|
-
search(s:
|
|
160
|
+
search(s: CommentFilter, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<Comment>> {
|
|
150
161
|
return this.find(s, limit, offset, fields).then(res => {
|
|
151
162
|
if (!this.queryURL) {
|
|
152
163
|
return res;
|
|
@@ -203,7 +214,7 @@ export class CommentValidator {
|
|
|
203
214
|
constructor(protected attributes: Attributes, protected check: (obj: any, attributes: Attributes) => ErrorMessage[]) {
|
|
204
215
|
this.validate = this.validate.bind(this);
|
|
205
216
|
}
|
|
206
|
-
validate(comment:
|
|
217
|
+
validate(comment: Comment): Promise<ErrorMessage[]> {
|
|
207
218
|
const errs = this.check(comment, this.attributes);
|
|
208
219
|
return Promise.resolve(errs);
|
|
209
220
|
}
|
package/src/rate.ts
CHANGED
|
@@ -35,26 +35,28 @@ export interface RateRepository {
|
|
|
35
35
|
// save(obj: Rate, info?: T, ctx?: any): Promise<number>;
|
|
36
36
|
insert(rate: Rate, newInfo?: boolean): Promise<number>;
|
|
37
37
|
update(rate: Rate, oldRate: number): Promise<number>;
|
|
38
|
-
|
|
38
|
+
load(id: string, author: string): Promise<Rate | null>;
|
|
39
39
|
}
|
|
40
40
|
export interface Rater {
|
|
41
41
|
search(s: RateFilter, limit?: number, offset?: number | string, fields?: string[], ctx?: any): Promise<SearchResult<Rate>>;
|
|
42
|
-
|
|
42
|
+
load(id: string, author: string): Promise<Rate | null>;
|
|
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,8 +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<
|
|
66
|
-
getComments(id: string, author: string, limit?: number): Promise<
|
|
67
|
+
export interface RateCommentQuery extends Query<Comment, string, CommentFilter> {
|
|
68
|
+
getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
|
|
67
69
|
}
|
|
68
70
|
export const rateHistoryModel: Attributes = {
|
|
69
71
|
rate: {
|
|
@@ -213,13 +215,13 @@ export interface Info10 {
|
|
|
213
215
|
export interface InfoRepository<T> extends ViewRepository<T, string> {
|
|
214
216
|
}
|
|
215
217
|
|
|
216
|
-
export interface
|
|
218
|
+
export interface CommentId {
|
|
217
219
|
id: string;
|
|
218
220
|
author: string;
|
|
219
221
|
userId: string;
|
|
220
222
|
}
|
|
221
223
|
|
|
222
|
-
export interface
|
|
224
|
+
export interface Comment {
|
|
223
225
|
commentId: string;
|
|
224
226
|
id: string;
|
|
225
227
|
author: string;
|
|
@@ -236,7 +238,7 @@ export interface ShortComment {
|
|
|
236
238
|
time: Date;
|
|
237
239
|
}
|
|
238
240
|
|
|
239
|
-
export interface
|
|
241
|
+
export interface CommentFilter extends Filter {
|
|
240
242
|
commentId?: string;
|
|
241
243
|
id?: string;
|
|
242
244
|
author?: string;
|
|
@@ -245,45 +247,3 @@ export interface RateCommentFilter extends Filter {
|
|
|
245
247
|
time?: Date;
|
|
246
248
|
updatedAt?: Date;
|
|
247
249
|
}
|
|
248
|
-
export const commentModel: Attributes = {
|
|
249
|
-
comment: {
|
|
250
|
-
length: 500
|
|
251
|
-
},
|
|
252
|
-
time: {
|
|
253
|
-
type: 'datetime'
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
export const rateCommentModel: Attributes = {
|
|
257
|
-
commentId: {
|
|
258
|
-
key: true
|
|
259
|
-
},
|
|
260
|
-
id: {
|
|
261
|
-
required: true,
|
|
262
|
-
noupdate: true,
|
|
263
|
-
match: 'equal'
|
|
264
|
-
},
|
|
265
|
-
author: {
|
|
266
|
-
required: true,
|
|
267
|
-
noupdate: true,
|
|
268
|
-
match: 'equal'
|
|
269
|
-
},
|
|
270
|
-
userId: {
|
|
271
|
-
required: true,
|
|
272
|
-
noupdate: true,
|
|
273
|
-
match: 'equal'
|
|
274
|
-
},
|
|
275
|
-
comment: {
|
|
276
|
-
length: 500
|
|
277
|
-
},
|
|
278
|
-
time: {
|
|
279
|
-
type: 'datetime',
|
|
280
|
-
noupdate: true,
|
|
281
|
-
},
|
|
282
|
-
updatedAt: {
|
|
283
|
-
type: 'datetime'
|
|
284
|
-
},
|
|
285
|
-
histories: {
|
|
286
|
-
type: 'array',
|
|
287
|
-
typeof: commentModel
|
|
288
|
-
}
|
|
289
|
-
};
|