rate-core 0.0.3 → 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,11 +183,17 @@ 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;
@@ -202,12 +210,7 @@ var CommentQuery = (function () {
202
210
  return this.repository.load(id, ctx);
203
211
  };
204
212
  CommentQuery.prototype.getComments = function (id, author, limit) {
205
- if (this.repository.getComments) {
206
- return this.repository.getComments(id, author, limit);
207
- }
208
- else {
209
- return Promise.resolve([]);
210
- }
213
+ return this.repository.getComments(id, author, limit);
211
214
  };
212
215
  CommentQuery.prototype.search = function (s, limit, offset, fields) {
213
216
  var _this = this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rate-core",
3
- "version": "0.0.3",
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
 
@@ -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 rateCommentRepository: RateCommentRepository,
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: 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<O> 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,34 +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.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
136
  export interface CommentRepository {
129
- load(commentId: string, ctx?: any): Promise<RateComment|null>;
130
- getComments?(id: string, author: string, limit?: number): Promise<RateComment[]>;
137
+ load(commentId: string, ctx?: any): Promise<Comment|null>;
138
+ getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
131
139
  }
132
140
  // tslint:disable-next-line:max-classes-per-file
133
141
  export class CommentQuery implements RateCommentQuery {
134
- constructor(protected find: Search<RateComment, RateCommentFilter>, protected repository: CommentRepository, private queryURL?: (ids: string[]) => Promise<URL[]>) {
142
+ constructor(protected find: Search<Comment, CommentFilter>, protected repository: CommentRepository, private queryURL?: (ids: string[]) => Promise<URL[]>) {
135
143
  this.load = this.load.bind(this);
136
144
  this.search = this.search.bind(this);
137
145
  this.getComments = this.getComments.bind(this)
138
146
  }
139
- load(id: string, ctx?: any): Promise<RateComment|null> {
147
+ load(id: string, ctx?: any): Promise<Comment|null> {
140
148
  return this.repository.load(id, ctx);
141
149
  }
142
- getComments(id: string, author: string, limit?: number): Promise<RateComment[]> {
143
- if (this.repository.getComments) {
144
- return this.repository.getComments(id, author, limit);
145
- } else {
146
- return Promise.resolve([]);
147
- }
150
+ getComments(id: string, author: string, limit?: number): Promise<Comment[]> {
151
+ return this.repository.getComments(id, author, limit);
148
152
  }
149
- search(s: RateCommentFilter, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<RateComment>> {
153
+ search(s: CommentFilter, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<Comment>> {
150
154
  return this.find(s, limit, offset, fields).then(res => {
151
155
  if (!this.queryURL) {
152
156
  return res;
@@ -203,7 +207,7 @@ export class CommentValidator {
203
207
  constructor(protected attributes: Attributes, protected check: (obj: any, attributes: Attributes) => ErrorMessage[]) {
204
208
  this.validate = this.validate.bind(this);
205
209
  }
206
- validate(comment: RateComment): Promise<ErrorMessage[]> {
210
+ validate(comment: Comment): Promise<ErrorMessage[]> {
207
211
  const errs = this.check(comment, this.attributes);
208
212
  return Promise.resolve(errs);
209
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,8 +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
- getComments(id: string, author: string, limit?: number): Promise<RateComment[]>;
66
+ export interface RateCommentQuery extends Query<Comment, string, CommentFilter> {
67
+ getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
67
68
  }
68
69
  export const rateHistoryModel: Attributes = {
69
70
  rate: {
@@ -213,13 +214,13 @@ export interface Info10 {
213
214
  export interface InfoRepository<T> extends ViewRepository<T, string> {
214
215
  }
215
216
 
216
- export interface RateCommentId {
217
+ export interface CommentId {
217
218
  id: string;
218
219
  author: string;
219
220
  userId: string;
220
221
  }
221
222
 
222
- export interface RateComment {
223
+ export interface Comment {
223
224
  commentId: string;
224
225
  id: string;
225
226
  author: string;
@@ -236,7 +237,7 @@ export interface ShortComment {
236
237
  time: Date;
237
238
  }
238
239
 
239
- export interface RateCommentFilter extends Filter {
240
+ export interface CommentFilter extends Filter {
240
241
  commentId?: string;
241
242
  id?: string;
242
243
  author?: string;