rate-core 0.0.1 → 0.0.4

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 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, rateCommentRepository, rateReactionRepository, queryURL) {
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.rateCommentRepository = rateCommentRepository;
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.rateCommentRepository.insert(comment);
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.rateCommentRepository.load(commentId).then(function (comment) {
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.rateCommentRepository.remove(commentId, comment.id, comment.author);
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.rateCommentRepository.load(comment.commentId).then(function (exist) {
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.rateCommentRepository.update(exist);
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 RateCommentManager = (function () {
193
- function RateCommentManager(find, repository, queryURL) {
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
- RateCommentManager.prototype.load = function (id, ctx) {
209
+ CommentQuery.prototype.load = function (id, ctx) {
201
210
  return this.repository.load(id, ctx);
202
211
  };
203
- RateCommentManager.prototype.search = function (s, limit, offset, fields) {
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 RateCommentManager;
245
+ return CommentQuery;
234
246
  }());
235
- exports.RateCommentManager = RateCommentManager;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rate-core",
3
- "version": "0.0.1",
3
+ "version": "0.0.4",
4
4
  "description": "rate",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Attributes, Search, SearchResult } from './core';
2
2
  import {
3
- InfoRepository, Rate, RateComment, RateCommentFilter, RateCommentRepository, RateCommentQuery, RateFilter, RateReactionRepository,
3
+ Comment, CommentFilter, InfoRepository, Rate, RateCommentRepository, RateCommentQuery, RateFilter, RateReactionRepository,
4
4
  RateRepository, Rater, ShortComment, ShortRate
5
5
  } from './rate';
6
6
 
@@ -10,11 +10,11 @@ export interface URL {
10
10
  id: string;
11
11
  url: string;
12
12
  }
13
- export class RateService implements Rater {
13
+ export class RateService<O> implements Rater {
14
14
  constructor(protected find: Search<Rate, RateFilter>,
15
15
  public repository: RateRepository,
16
- private infoRepository: InfoRepository,
17
- private rateCommentRepository: RateCommentRepository,
16
+ private infoRepository: InfoRepository<O>,
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 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 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: RateComment): Promise<number> {
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.rateCommentRepository.insert(comment);
91
+ return this.commentRepository.insert(comment);
90
92
  }
91
93
  });
92
94
  }
93
95
  removeComment(commentId: string, userId: string): Promise<number> {
94
- return this.rateCommentRepository.load(commentId).then(comment => {
96
+ return this.commentRepository.load(commentId).then(comment => {
95
97
  if (comment) {
96
98
  if (userId === comment.author || userId === comment.userId) {
97
- return this.rateCommentRepository.remove(commentId, comment.id, comment.author);
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 implements Rater {
103
105
  }
104
106
  });
105
107
  }
106
- updateComment(comment: RateComment): Promise<number> {
107
- return this.rateCommentRepository.load(comment.commentId).then(exist => {
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 implements Rater {
119
121
  exist.histories = [c];
120
122
  }
121
123
  exist.comment = comment.comment;
122
- const res = this.rateCommentRepository.update(exist);
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 CommentQuery {
129
- load(id: string, ctx?: any): Promise<RateComment|null>;
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 RateCommentManager implements RateCommentQuery {
133
- constructor(protected find: Search<RateComment, RateCommentFilter>, protected repository: CommentQuery, private queryURL?: (ids: string[]) => Promise<URL[]>) {
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<RateComment|null> {
147
+ load(id: string, ctx?: any): Promise<Comment|null> {
138
148
  return this.repository.load(id, ctx);
139
149
  }
140
- search(s: RateCommentFilter, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<RateComment>> {
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: RateComment): Promise<ErrorMessage[]> {
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,9 +43,9 @@ 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: RateComment): Promise<number>;
46
+ comment(comment: Comment): Promise<number>;
47
47
  removeComment(id: string, author: string, ctx?: any): Promise<number>;
48
- updateComment(comment: RateComment): Promise<number>;
48
+ updateComment(comment: Comment): Promise<number>;
49
49
  }
50
50
 
51
51
  export interface RateReactionRepository {
@@ -53,8 +53,9 @@ export interface RateReactionRepository {
53
53
  save(id: string, author: string, userId: string, type: number): Promise<number>;
54
54
  }
55
55
 
56
- export interface RateCommentRepository extends Repository<RateComment, string> {
56
+ export interface RateCommentRepository extends Repository<Comment, string> {
57
57
  remove(commentId: string, id: string, author: string): Promise<number>;
58
+ getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
58
59
  }
59
60
 
60
61
  export interface Query<T, ID, S> {
@@ -62,7 +63,8 @@ export interface Query<T, ID, S> {
62
63
  metadata?(): Attributes|undefined;
63
64
  load(id: ID, ctx?: any): Promise<T|null>;
64
65
  }
65
- export interface RateCommentQuery extends Query<RateComment, string, RateCommentFilter> {
66
+ export interface RateCommentQuery extends Query<Comment, string, CommentFilter> {
67
+ getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
66
68
  }
67
69
  export const rateHistoryModel: Attributes = {
68
70
  rate: {
@@ -193,17 +195,32 @@ export interface Info {
193
195
  count: number;
194
196
  score: number;
195
197
  }
196
-
197
- export interface InfoRepository extends ViewRepository<Info, string> {
198
+ export interface Info10 {
199
+ id: string;
200
+ rate: number;
201
+ rate1: number;
202
+ rate2: number;
203
+ rate3: number;
204
+ rate4: number;
205
+ rate5: number;
206
+ rate6: number;
207
+ rate7: number;
208
+ rate8: number;
209
+ rate9: number;
210
+ rate10: number;
211
+ count: number;
212
+ score: number;
213
+ }
214
+ export interface InfoRepository<T> extends ViewRepository<T, string> {
198
215
  }
199
216
 
200
- export interface RateCommentId {
217
+ export interface CommentId {
201
218
  id: string;
202
219
  author: string;
203
220
  userId: string;
204
221
  }
205
222
 
206
- export interface RateComment {
223
+ export interface Comment {
207
224
  commentId: string;
208
225
  id: string;
209
226
  author: string;
@@ -220,7 +237,7 @@ export interface ShortComment {
220
237
  time: Date;
221
238
  }
222
239
 
223
- export interface RateCommentFilter extends Filter {
240
+ export interface CommentFilter extends Filter {
224
241
  commentId?: string;
225
242
  id?: string;
226
243
  author?: string;