rate-core 0.0.0

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/README.md ADDED
@@ -0,0 +1 @@
1
+ # rate
package/lib/core.js ADDED
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ var ViewManager = (function () {
17
+ function ViewManager(r) {
18
+ this.r = r;
19
+ this.metadata = this.metadata.bind(this);
20
+ this.keys = this.keys.bind(this);
21
+ this.all = this.all.bind(this);
22
+ this.load = this.load.bind(this);
23
+ this.exist = this.exist.bind(this);
24
+ }
25
+ ViewManager.prototype.metadata = function () {
26
+ return (this.r.metadata ? this.r.metadata() : undefined);
27
+ };
28
+ ViewManager.prototype.keys = function () {
29
+ return (this.r.keys ? this.r.keys() : []);
30
+ };
31
+ ViewManager.prototype.all = function (ctx) {
32
+ return (this.r.all ? this.r.all() : Promise.resolve([]));
33
+ };
34
+ ViewManager.prototype.load = function (id, ctx) {
35
+ return this.r.load(id, ctx);
36
+ };
37
+ ViewManager.prototype.exist = function (id, ctx) {
38
+ return (this.r.exist ? this.r.exist(id, ctx) : Promise.resolve(false));
39
+ };
40
+ return ViewManager;
41
+ }());
42
+ exports.ViewManager = ViewManager;
43
+ var GenericManager = (function (_super) {
44
+ __extends(GenericManager, _super);
45
+ function GenericManager(repository) {
46
+ var _this = _super.call(this, repository) || this;
47
+ _this.repository = repository;
48
+ _this.insert = _this.insert.bind(_this);
49
+ _this.update = _this.update.bind(_this);
50
+ _this.patch = _this.patch.bind(_this);
51
+ _this.save = _this.save.bind(_this);
52
+ _this.delete = _this.delete.bind(_this);
53
+ return _this;
54
+ }
55
+ GenericManager.prototype.insert = function (obj, ctx) {
56
+ return this.repository.insert(obj, ctx);
57
+ };
58
+ GenericManager.prototype.update = function (obj, ctx) {
59
+ return this.repository.update(obj, ctx);
60
+ };
61
+ GenericManager.prototype.patch = function (obj, ctx) {
62
+ return (this.repository.patch ? this.repository.patch(obj, ctx) : Promise.resolve(-1));
63
+ };
64
+ GenericManager.prototype.save = function (obj, ctx) {
65
+ return (this.repository.save ? this.repository.save(obj, ctx) : Promise.resolve(-1));
66
+ };
67
+ GenericManager.prototype.delete = function (id, ctx) {
68
+ return (this.repository.delete ? this.repository.delete(id, ctx) : Promise.resolve(-1));
69
+ };
70
+ return GenericManager;
71
+ }(ViewManager));
72
+ exports.GenericManager = GenericManager;
73
+ var Manager = (function (_super) {
74
+ __extends(Manager, _super);
75
+ function Manager(find, repo) {
76
+ var _this = _super.call(this, repo) || this;
77
+ _this.find = find;
78
+ _this.search = _this.search.bind(_this);
79
+ return _this;
80
+ }
81
+ Manager.prototype.search = function (s, limit, offset, fields) {
82
+ return this.find(s, limit, offset, fields);
83
+ };
84
+ return Manager;
85
+ }(GenericManager));
86
+ exports.Manager = Manager;
package/lib/index.js ADDED
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
15
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
17
+ return new (P || (P = Promise))(function (resolve, reject) {
18
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
19
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
20
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
21
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
22
+ });
23
+ };
24
+ var __generator = (this && this.__generator) || function (thisArg, body) {
25
+ var _ = { label: 0, sent: function () { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
26
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g;
27
+ function verb(n) { return function (v) { return step([n, v]); }; }
28
+ function step(op) {
29
+ if (f) throw new TypeError("Generator is already executing.");
30
+ while (_) try {
31
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
32
+ if (y = 0, t) op = [op[0] & 2, t.value];
33
+ switch (op[0]) {
34
+ case 0: case 1: t = op; break;
35
+ case 4: _.label++; return { value: op[1], done: false };
36
+ case 5: _.label++; y = op[1]; op = [0]; continue;
37
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
38
+ default:
39
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
40
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
41
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
42
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
43
+ if (t[2]) _.ops.pop();
44
+ _.trys.pop(); continue;
45
+ }
46
+ op = body.call(thisArg, _);
47
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
48
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
49
+ }
50
+ };
51
+ function __export(m) {
52
+ for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
53
+ }
54
+ Object.defineProperty(exports, "__esModule", { value: true });
55
+ var core_1 = require("./core");
56
+ __export(require("./rate"));
57
+ var RateManager = (function (_super) {
58
+ __extends(RateManager, _super);
59
+ function RateManager(search, repository, infoRepository, rateCommentRepository, rateReactionRepository) {
60
+ var _this = _super.call(this, search, repository) || this;
61
+ _this.repository = repository;
62
+ _this.infoRepository = infoRepository;
63
+ _this.rateCommentRepository = rateCommentRepository;
64
+ _this.rateReactionRepository = rateReactionRepository;
65
+ _this.rate = _this.rate.bind(_this);
66
+ _this.update = _this.update.bind(_this);
67
+ _this.save = _this.save.bind(_this);
68
+ _this.comment = _this.comment.bind(_this);
69
+ _this.load = _this.load.bind(_this);
70
+ _this.removeComment = _this.removeComment.bind(_this);
71
+ _this.updateComment = _this.updateComment.bind(_this);
72
+ _this.updateRate = _this.updateRate.bind(_this);
73
+ return _this;
74
+ }
75
+ RateManager.prototype.rate = function (rate) {
76
+ var _a;
77
+ return __awaiter(this, void 0, void 0, function () {
78
+ var info, exist, r, sumRate, count;
79
+ return __generator(this, function (_b) {
80
+ switch (_b.label) {
81
+ case 0: return [4, this.infoRepository.load(rate.id)];
82
+ case 1:
83
+ info = _b.sent();
84
+ if (!info) {
85
+ info = {
86
+ 'id': rate.id,
87
+ 'rate': 0,
88
+ 'rate1': 0,
89
+ 'rate2': 0,
90
+ 'rate3': 0,
91
+ 'rate4': 0,
92
+ 'rate5': 0,
93
+ 'viewCount': 0,
94
+ };
95
+ }
96
+ return [4, this.repository.getRate(rate.id, rate.author)];
97
+ case 2:
98
+ exist = _b.sent();
99
+ r = (exist ? exist.rate : 0);
100
+ info['rate' + ((_a = rate.rate) === null || _a === void 0 ? void 0 : _a.toString())] += 1;
101
+ sumRate = info.rate1 +
102
+ info.rate2 * 2 +
103
+ info.rate3 * 3 +
104
+ info.rate4 * 4 +
105
+ info.rate5 * 5 - r;
106
+ count = info.rate1 +
107
+ info.rate2 +
108
+ info.rate3 +
109
+ info.rate4 +
110
+ info.rate5 + (exist ? 0 : 1);
111
+ info.rate = sumRate / count;
112
+ info.viewCount = count;
113
+ rate.usefulCount = 0;
114
+ return [4, this.infoRepository.save(info)];
115
+ case 3:
116
+ _b.sent();
117
+ return [4, this.repository.save(rate)];
118
+ case 4:
119
+ _b.sent();
120
+ return [2, true];
121
+ }
122
+ });
123
+ });
124
+ };
125
+ RateManager.prototype.getRate = function (id, author) {
126
+ return this.repository.getRate(id, author);
127
+ };
128
+ RateManager.prototype.setUseful = function (id, author, userId) {
129
+ return this.rateReactionRepository.save(id, author, userId, 1);
130
+ };
131
+ RateManager.prototype.removeUseful = function (id, author, userId) {
132
+ return this.rateReactionRepository.remove(id, author, userId);
133
+ };
134
+ RateManager.prototype.comment = function (comment) {
135
+ var _this = this;
136
+ return this.repository.getRate(comment.id, comment.author).then(function (checkRate) {
137
+ if (!checkRate) {
138
+ return 0;
139
+ }
140
+ else {
141
+ comment.time ? comment.time = comment.time : comment.time = new Date();
142
+ return _this.rateCommentRepository.insert(comment);
143
+ }
144
+ });
145
+ };
146
+ RateManager.prototype.removeComment = function (commentId, userId) {
147
+ var _this = this;
148
+ return this.rateCommentRepository.load(commentId).then(function (comment) {
149
+ if (comment) {
150
+ if (userId === comment.author || userId === comment.userId) {
151
+ return _this.rateCommentRepository.remove(commentId, comment.id, comment.author);
152
+ }
153
+ else {
154
+ return -2;
155
+ }
156
+ }
157
+ else {
158
+ return -1;
159
+ }
160
+ });
161
+ };
162
+ RateManager.prototype.updateComment = function (comment) {
163
+ var _this = this;
164
+ return this.rateCommentRepository.load(comment.commentId).then(function (exist) {
165
+ if (!exist) {
166
+ return 0;
167
+ }
168
+ else {
169
+ comment.updatedAt = new Date();
170
+ return _this.rateCommentRepository.update(comment);
171
+ }
172
+ });
173
+ };
174
+ RateManager.prototype.updateRate = function (rate) {
175
+ var _this = this;
176
+ return this.repository.getRate(rate.id, rate.author).then(function (exist) {
177
+ if (exist) {
178
+ rate.time ? rate.time = rate.time : rate.time = new Date();
179
+ return _this.repository.update(rate);
180
+ }
181
+ else {
182
+ return 0;
183
+ }
184
+ });
185
+ };
186
+ return RateManager;
187
+ }(core_1.Manager));
188
+ exports.RateManager = RateManager;
189
+ var RateCommentManager = (function (_super) {
190
+ __extends(RateCommentManager, _super);
191
+ function RateCommentManager(search, replyRepository) {
192
+ var _this = _super.call(this, search, replyRepository) || this;
193
+ _this.replyRepository = replyRepository;
194
+ return _this;
195
+ }
196
+ return RateCommentManager;
197
+ }(core_1.Manager));
198
+ exports.RateCommentManager = RateCommentManager;
package/lib/rate.js ADDED
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rateModel = {
4
+ id: {
5
+ key: true,
6
+ required: true,
7
+ match: 'equal'
8
+ },
9
+ author: {
10
+ key: true,
11
+ required: true,
12
+ match: 'equal'
13
+ },
14
+ rate: {
15
+ type: 'integer',
16
+ min: 1,
17
+ max: 5
18
+ },
19
+ time: {
20
+ type: 'datetime',
21
+ },
22
+ review: {
23
+ q: true,
24
+ },
25
+ usefulCount: {
26
+ type: 'integer',
27
+ min: 0
28
+ },
29
+ replyCount: {
30
+ type: 'integer',
31
+ min: 0
32
+ }
33
+ };
34
+ exports.infoModel = {
35
+ id: {
36
+ key: true,
37
+ },
38
+ viewCount: {
39
+ type: 'number'
40
+ },
41
+ rate: {
42
+ type: 'number'
43
+ },
44
+ rate1: {
45
+ type: 'number',
46
+ },
47
+ rate2: {
48
+ type: 'number',
49
+ },
50
+ rate3: {
51
+ type: 'number',
52
+ },
53
+ rate4: {
54
+ type: 'number',
55
+ },
56
+ rate5: {
57
+ type: 'number',
58
+ },
59
+ };
60
+ exports.rateCommentModel = {
61
+ commentId: {
62
+ key: true
63
+ },
64
+ id: {
65
+ required: true,
66
+ match: 'equal'
67
+ },
68
+ author: {
69
+ required: true,
70
+ match: 'equal'
71
+ },
72
+ userId: {
73
+ required: true,
74
+ match: 'equal'
75
+ },
76
+ comment: {
77
+ length: 500
78
+ },
79
+ time: {
80
+ type: 'datetime'
81
+ }
82
+ };
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "rate-core",
3
+ "version": "0.0.0",
4
+ "description": "rate",
5
+ "main": "./lib/index.js",
6
+ "types": "./src/index.ts",
7
+ "scripts": {
8
+ "build:lib": "tsc",
9
+ "clean:lib": "rimraf lib"
10
+ },
11
+ "devDependencies": {
12
+ "tslint": "5.10.0",
13
+ "typescript": "^3.3.3333"
14
+ },
15
+ "publishConfig": {
16
+ "registry": "https://registry.npmjs.org/"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git@github.com/core-ts/rate"
21
+ },
22
+ "keywords": [
23
+ "rate",
24
+ "rate core"
25
+ ]
26
+ }
package/src/core.ts ADDED
@@ -0,0 +1,195 @@
1
+ export interface StringMap {
2
+ [key: string]: string;
3
+ }
4
+ export interface Statement {
5
+ query: string;
6
+ params?: any[];
7
+ }
8
+ export interface DB {
9
+ driver: string;
10
+ param(i: number): string;
11
+ exec(sql: string, args?: any[], ctx?: any): Promise<number>;
12
+ execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>;
13
+ query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>;
14
+ }
15
+ export type DataType = 'ObjectId' | 'date' | 'datetime' | 'time'
16
+ | 'boolean' | 'number' | 'integer' | 'string' | 'text'
17
+ | 'object' | 'array' | 'binary'
18
+ | 'primitives' | 'booleans' | 'numbers' | 'integers' | 'strings' | 'dates' | 'datetimes' | 'times';
19
+ export type FormatType = 'currency' | 'percentage' | 'email' | 'url' | 'phone' | 'fax' | 'ipv4' | 'ipv6';
20
+ export type MatchType = 'equal' | 'prefix' | 'contain' | 'max' | 'min'; // contain: default for string, min: default for Date, number
21
+
22
+ export interface Attribute {
23
+ name?: string;
24
+ field?: string;
25
+ column?: string;
26
+ type?: DataType;
27
+ format?: FormatType;
28
+ required?: boolean;
29
+ match?: MatchType;
30
+ default?: string|number|Date|boolean;
31
+ key?: boolean;
32
+ unique?: boolean;
33
+ enum?: string[] | number[];
34
+ q?: boolean;
35
+ noinsert?: boolean;
36
+ noupdate?: boolean;
37
+ nopatch?: boolean;
38
+ version?: boolean;
39
+ length?: number;
40
+ min?: number;
41
+ max?: number;
42
+ gt?: number;
43
+ lt?: number;
44
+ precision?: number;
45
+ scale?: number;
46
+ exp?: RegExp | string;
47
+ code?: string;
48
+ noformat?: boolean;
49
+ ignored?: boolean;
50
+ jsonField?: string;
51
+ link?: string;
52
+ typeof?: Attributes;
53
+ true?: string|number;
54
+ false?: string|number;
55
+ }
56
+ export interface Attributes {
57
+ [key: string]: Attribute;
58
+ }
59
+
60
+ export interface Filter {
61
+ page?: number;
62
+ limit?: number;
63
+ firstLimit?: number;
64
+ fields?: string[];
65
+ sort?: string;
66
+ currentUserId?: string;
67
+
68
+ q?: string;
69
+ keyword?: string;
70
+ excluding?: string[]|number[];
71
+ refId?: string|number;
72
+
73
+ pageIndex?: number;
74
+ pageSize?: number;
75
+ }
76
+ export interface ViewRepository<T, ID> {
77
+ metadata?(): Attributes|undefined;
78
+ keys?(): string[];
79
+ all?(ctx?: any): Promise<T[]>;
80
+ load(id: ID, ctx?: any): Promise<T|null>;
81
+ exist?(id: ID, ctx?: any): Promise<boolean>;
82
+ }
83
+ export interface Repository<T, ID> extends ViewRepository<T, ID> {
84
+ insert(obj: T, ctx?: any): Promise<number>;
85
+ update(obj: T, ctx?: any): Promise<number>;
86
+ patch?(obj: T, ctx?: any): Promise<number>;
87
+ save?(obj: T, ctx?: any): Promise<number>;
88
+ delete(id: ID, ctx?: any): Promise<number>;
89
+ }
90
+
91
+ export interface ViewService<T, ID> {
92
+ metadata?(): Attributes|undefined;
93
+ keys?(): string[];
94
+ all?(ctx?: any): Promise<T[]>;
95
+ load(id: ID, ctx?: any): Promise<T|null>;
96
+ exist?(id: ID, ctx?: any): Promise<boolean>;
97
+ }
98
+ export interface GenericService<T, ID, R> extends ViewService<T, ID> {
99
+ insert(obj: T, ctx?: any): Promise<R>;
100
+ update(obj: T, ctx?: any): Promise<R>;
101
+ patch?(obj: T, ctx?: any): Promise<R>;
102
+ save?(obj: T, ctx?: any): Promise<R>;
103
+ delete?(id: ID, ctx?: any): Promise<number>;
104
+ }
105
+ export interface SearchResult<T> {
106
+ list: T[];
107
+ total?: number;
108
+ last?: boolean;
109
+ nextPageToken?: string;
110
+ }
111
+
112
+ export interface ErrorMessage {
113
+ field: string;
114
+ code: string;
115
+ param?: string|number|Date;
116
+ message?: string;
117
+ }
118
+ export interface ResultInfo<T> {
119
+ status: number|string;
120
+ errors?: ErrorMessage[];
121
+ value?: T;
122
+ message?: string;
123
+ }
124
+ export type Result<T> = number | ResultInfo<T>;
125
+ export interface SearchService<T, F extends Filter> {
126
+ keys?(): string[];
127
+ search(s: F, limit?: number, offset?: number|string, fields?: string[], ctx?: any): Promise<SearchResult<T>>;
128
+ }
129
+ export interface GenericSearchService<T, ID, R, F extends Filter>
130
+ extends GenericService<T, ID, R>, SearchService<T, F> {
131
+ }
132
+ export interface Service<T, ID, F extends Filter> extends GenericSearchService<T, ID, Result<T>, F> {
133
+
134
+ }
135
+ export class ViewManager<T, ID> implements ViewService<T, ID> {
136
+ constructor(private r: ViewRepository<T, ID>) {
137
+ this.metadata = this.metadata.bind(this);
138
+ this.keys = this.keys.bind(this);
139
+ this.all = this.all.bind(this);
140
+ this.load = this.load.bind(this);
141
+ this.exist = this.exist.bind(this);
142
+ }
143
+ metadata(): Attributes|undefined {
144
+ return (this.r.metadata ? this.r.metadata() : undefined);
145
+ }
146
+ keys(): string[] {
147
+ return (this.r.keys ? this.r.keys() : []);
148
+ }
149
+ all(ctx?: any): Promise<T[]> {
150
+ return (this.r.all ? this.r.all() : Promise.resolve([]));
151
+ }
152
+ load(id: ID, ctx?: any): Promise<T | null> {
153
+ return this.r.load(id, ctx);
154
+ }
155
+ exist(id: ID, ctx?: any): Promise<boolean> {
156
+ return (this.r.exist ? this.r.exist(id, ctx) : Promise.resolve(false));
157
+ }
158
+ }
159
+ // tslint:disable-next-line:max-classes-per-file
160
+ export class GenericManager<T, ID> extends ViewManager<T, ID> implements GenericService<T, ID, number> {
161
+ constructor(protected repository: Repository<T, ID>) {
162
+ super(repository);
163
+ this.insert = this.insert.bind(this);
164
+ this.update = this.update.bind(this);
165
+ this.patch = this.patch.bind(this);
166
+ this.save = this.save.bind(this);
167
+ this.delete = this.delete.bind(this);
168
+ }
169
+ insert(obj: T, ctx?: any): Promise<number> {
170
+ return this.repository.insert(obj, ctx);
171
+ }
172
+ update(obj: T, ctx?: any): Promise<number> {
173
+ return this.repository.update(obj, ctx);
174
+ }
175
+ patch(obj: T, ctx?: any): Promise<number> {
176
+ return (this.repository.patch ? this.repository.patch(obj, ctx) : Promise.resolve(-1));
177
+ }
178
+ save(obj: T, ctx?: any): Promise<number> {
179
+ return (this.repository.save ? this.repository.save(obj, ctx) : Promise.resolve(-1));
180
+ }
181
+ delete(id: ID, ctx?: any): Promise<number> {
182
+ return (this.repository.delete ? this.repository.delete(id, ctx) : Promise.resolve(-1));
183
+ }
184
+ }
185
+ export type Search<T, F> = (s: F, limit?: number, offset?: number | string, fields?: string[]) => Promise<SearchResult<T>>;
186
+ // tslint:disable-next-line:max-classes-per-file
187
+ export class Manager<T, ID, F extends Filter> extends GenericManager<T, ID> implements GenericSearchService<T, ID, number, F> {
188
+ constructor(public find: Search<T, F>, repo: Repository<T, ID>) {
189
+ super(repo);
190
+ this.search = this.search.bind(this);
191
+ }
192
+ search(s: F, limit?: number, offset?: number|string, fields?: string[]): Promise<SearchResult<T>> {
193
+ return this.find(s, limit, offset, fields);
194
+ }
195
+ }
package/src/index.ts ADDED
@@ -0,0 +1,120 @@
1
+ import { Manager, Search } from './core';
2
+ import {
3
+ InfoRepository, Rate, RateComment, RateCommentFilter, RateCommentRepository, RateCommentService, RateFilter, RateId,
4
+ RateReactionRepository, RateRepository, RateService
5
+ } from './rate';
6
+ export * from './rate';
7
+
8
+ export class RateManager extends Manager<Rate, RateId, RateFilter> implements RateService {
9
+ constructor(search: Search<Rate, RateFilter>,
10
+ public repository: RateRepository,
11
+ private infoRepository: InfoRepository,
12
+ private rateCommentRepository: RateCommentRepository,
13
+ private rateReactionRepository: RateReactionRepository) {
14
+ super(search, repository);
15
+ this.rate = this.rate.bind(this);
16
+ this.update = this.update.bind(this);
17
+ this.save = this.save.bind(this);
18
+ this.comment = this.comment.bind(this);
19
+ this.load = this.load.bind(this);
20
+ this.removeComment = this.removeComment.bind(this);
21
+ this.updateComment = this.updateComment.bind(this);
22
+ this.updateRate = this.updateRate.bind(this);
23
+ }
24
+ async rate(rate: Rate): Promise<boolean> {
25
+ let info = await this.infoRepository.load(rate.id);
26
+ if (!info) {
27
+ info = {
28
+ 'id': rate.id,
29
+ 'rate': 0,
30
+ 'rate1': 0,
31
+ 'rate2': 0,
32
+ 'rate3': 0,
33
+ 'rate4': 0,
34
+ 'rate5': 0,
35
+ 'viewCount': 0,
36
+ };
37
+ }
38
+ const exist = await this.repository.getRate(rate.id, rate.author);
39
+ const r = (exist ? exist.rate : 0);
40
+ (info as any)['rate' + rate.rate?.toString()] += 1;
41
+ const sumRate = info.rate1 +
42
+ info.rate2 * 2 +
43
+ info.rate3 * 3 +
44
+ info.rate4 * 4 +
45
+ info.rate5 * 5 - r;
46
+
47
+ const count = info.rate1 +
48
+ info.rate2 +
49
+ info.rate3 +
50
+ info.rate4 +
51
+ info.rate5 + (exist ? 0 : 1);
52
+
53
+ info.rate = sumRate / count;
54
+ info.viewCount = count;
55
+ rate.usefulCount = 0;
56
+ await this.infoRepository.save(info);
57
+ await this.repository.save(rate);
58
+ return true;
59
+ }
60
+ getRate(id: string, author: string): Promise<Rate | null> {
61
+ return this.repository.getRate(id, author);
62
+ }
63
+
64
+ setUseful(id: string, author: string, userId: string): Promise<number> {
65
+ return this.rateReactionRepository.save(id, author, userId, 1);
66
+ }
67
+ removeUseful(id: string, author: string, userId: string): Promise<number> {
68
+ return this.rateReactionRepository.remove(id, author, userId);
69
+ }
70
+ comment(comment: RateComment): Promise<number> {
71
+ return this.repository.getRate(comment.id, comment.author).then(checkRate => {
72
+ if (!checkRate) {
73
+ return 0;
74
+ } else {
75
+ comment.time ? comment.time = comment.time : comment.time = new Date();
76
+ return this.rateCommentRepository.insert(comment);
77
+ }
78
+ });
79
+ }
80
+ removeComment(commentId: string, userId: string): Promise<number> {
81
+ return this.rateCommentRepository.load(commentId).then(comment => {
82
+ if (comment) {
83
+ if (userId === comment.author || userId === comment.userId) {
84
+ return this.rateCommentRepository.remove(commentId, comment.id, comment.author);
85
+ } else {
86
+ return -2;
87
+ }
88
+ } else {
89
+ return -1;
90
+ }
91
+ });
92
+ }
93
+ updateComment(comment: RateComment): Promise<number> {
94
+ return this.rateCommentRepository.load(comment.commentId).then(exist => {
95
+ if (!exist) {
96
+ return 0;
97
+ } else {
98
+ comment.updatedAt = new Date();
99
+ return this.rateCommentRepository.update(comment);
100
+ }
101
+ });
102
+ }
103
+ updateRate(rate: Rate): Promise<number> {
104
+ return this.repository.getRate(rate.id, rate.author).then(exist => {
105
+ if (exist) {
106
+ rate.time ? rate.time = rate.time : rate.time = new Date();
107
+ return this.repository.update(rate);
108
+ } else {
109
+ return 0;
110
+ }
111
+ });
112
+ }
113
+ }
114
+ // tslint:disable-next-line:max-classes-per-file
115
+ export class RateCommentManager extends Manager<RateComment, string, RateCommentFilter> implements RateCommentService {
116
+ constructor(search: Search<RateComment, RateCommentFilter>,
117
+ protected replyRepository: RateCommentRepository) {
118
+ super(search, replyRepository);
119
+ }
120
+ }
package/src/rate.ts ADDED
@@ -0,0 +1,181 @@
1
+ import { Attributes, Filter, Repository, Service, ViewRepository } from './core';
2
+
3
+ export interface RateId {
4
+ id: string;
5
+ author: string;
6
+ }
7
+
8
+ export interface Rate {
9
+ id: string;
10
+ author: string;
11
+ rate: number;
12
+ time: Date;
13
+ review: string;
14
+ usefulCount: number;
15
+ replyCount: number;
16
+ }
17
+
18
+ export interface RateFilter extends Filter {
19
+ id?: string;
20
+ author?: string;
21
+ rate: number;
22
+ time?: Date;
23
+ review?: string;
24
+ usefulCount?: number;
25
+ replyCount?: number;
26
+ }
27
+
28
+ export interface RateRepository extends Repository<Rate, RateId> {
29
+ save(obj: Rate, ctx?: any): Promise<number>;
30
+ getRate(id: string, author: string): Promise<Rate | null>;
31
+ }
32
+
33
+ export interface RateService extends Service<Rate, RateId, RateFilter> {
34
+ getRate(id: string, author: string): Promise<Rate | null>;
35
+ updateRate(rate: Rate): Promise<number>;
36
+ rate(rate: Rate): Promise<boolean>;
37
+ setUseful(id: string, author: string, userId: string, ctx?: any): Promise<number>;
38
+ removeUseful(id: string, author: string, userId: string, ctx?: any): Promise<number>;
39
+ comment(comment: RateComment): Promise<number>;
40
+ removeComment(id: string, author: string, ctx?: any): Promise<number>;
41
+ updateComment(comment: RateComment): Promise<number>;
42
+ }
43
+
44
+ export interface RateReactionRepository {
45
+ remove(id: string, author: string, userId: string, ctx?: any): Promise<number>;
46
+ save(id: string, author: string, userId: string, type: number): Promise<number>;
47
+ }
48
+
49
+ export interface RateCommentRepository extends Repository<RateComment, string> {
50
+ remove(commentId: string, id: string, author: string): Promise<number>;
51
+ }
52
+
53
+ export interface RateCommentService extends Service<RateComment, string, RateCommentFilter> {
54
+ }
55
+
56
+ export const rateModel: Attributes = {
57
+ id: {
58
+ key: true,
59
+ required: true,
60
+ match: 'equal'
61
+ },
62
+ author: {
63
+ key: true,
64
+ required: true,
65
+ match: 'equal'
66
+ },
67
+ rate: {
68
+ type: 'integer',
69
+ min: 1,
70
+ max: 5
71
+ },
72
+ time: {
73
+ type: 'datetime',
74
+ },
75
+ review: {
76
+ q: true,
77
+ },
78
+ usefulCount: {
79
+ type: 'integer',
80
+ min: 0
81
+ },
82
+ replyCount: {
83
+ type: 'integer',
84
+ min: 0
85
+ }
86
+ };
87
+
88
+ export const infoModel: Attributes = {
89
+ id: {
90
+ key: true,
91
+ },
92
+ viewCount: {
93
+ type: 'number'
94
+ },
95
+ rate: {
96
+ type: 'number'
97
+ },
98
+ rate1: {
99
+ type: 'number',
100
+ },
101
+ rate2: {
102
+ type: 'number',
103
+ },
104
+ rate3: {
105
+ type: 'number',
106
+ },
107
+ rate4: {
108
+ type: 'number',
109
+ },
110
+ rate5: {
111
+ type: 'number',
112
+ },
113
+ };
114
+
115
+ export interface Info {
116
+ id: string;
117
+ rate: number;
118
+ rate1: number;
119
+ rate2: number;
120
+ rate3: number;
121
+ rate4: number;
122
+ rate5: number;
123
+ viewCount: number;
124
+ }
125
+
126
+ export interface InfoRepository extends ViewRepository<Info, string> {
127
+ save(obj: Info, ctx?: any): Promise<number>;
128
+ }
129
+
130
+ export interface RateCommentId {
131
+ id: string;
132
+ author: string;
133
+ userId: string;
134
+ }
135
+
136
+ export interface RateComment {
137
+ commentId: string;
138
+ id: string;
139
+ author: string;
140
+ userId: string;
141
+ comment: string;
142
+ time: Date;
143
+ updatedAt?: Date;
144
+ history?: ShortComment;
145
+ }
146
+ export interface ShortComment {
147
+ comment: string;
148
+ time: Date;
149
+ }
150
+
151
+ export interface RateCommentFilter extends Filter {
152
+ commentId?: string;
153
+ id?: string;
154
+ author?: string;
155
+ userId?: string;
156
+ comment?: string;
157
+ time?: Date;
158
+ }
159
+ export const rateCommentModel: Attributes = {
160
+ commentId: {
161
+ key: true
162
+ },
163
+ id: {
164
+ required: true,
165
+ match: 'equal'
166
+ },
167
+ author: {
168
+ required: true,
169
+ match: 'equal'
170
+ },
171
+ userId: {
172
+ required: true,
173
+ match: 'equal'
174
+ },
175
+ comment: {
176
+ length: 500
177
+ },
178
+ time: {
179
+ type: 'datetime'
180
+ }
181
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "compileOnSave": true,
3
+ "compilerOptions": {
4
+ "target": "es5",
5
+ "baseUrl": "./src",
6
+ "outDir": "./lib",
7
+ "module": "commonjs",
8
+ "moduleResolution": "node",
9
+ "strict": true,
10
+ "pretty": true,
11
+ "sourceMap": false,
12
+ "declaration": false,
13
+ "experimentalDecorators": false,
14
+ "removeComments": true,
15
+ "lib": [
16
+ "es6"
17
+ ]
18
+ },
19
+ "include": [
20
+ "src/**/*.ts"
21
+ ],
22
+ "exclude": [
23
+ "node_modules"
24
+ ]
25
+ }