onecore 0.0.37 → 0.0.40

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
@@ -105,3 +105,63 @@ exports.DefaultGenericService = GenericManager;
105
105
  exports.GenericSearchManager = Manager;
106
106
  exports.DefaultService = GenericManager;
107
107
  exports.DefaultGenericSearchService = GenericManager;
108
+ var SavedService = (function () {
109
+ function SavedService(repository, query, max) {
110
+ this.repository = repository;
111
+ this.query = query;
112
+ this.max = max;
113
+ this.load = this.load.bind(this);
114
+ this.save = this.save.bind(this);
115
+ this.remove = this.remove.bind(this);
116
+ }
117
+ SavedService.prototype.load = function (id) {
118
+ var _this = this;
119
+ return this.repository.load(id).then(function (items) {
120
+ if (!items || items.length === 0) {
121
+ return [];
122
+ }
123
+ return _this.query(items);
124
+ });
125
+ };
126
+ SavedService.prototype.save = function (id, itemId) {
127
+ var _this = this;
128
+ return this.repository.load(id).then(function (items) {
129
+ if (items == null) {
130
+ return _this.repository.insert(id, [itemId]);
131
+ }
132
+ else {
133
+ if (items.includes(itemId)) {
134
+ return Promise.resolve(0);
135
+ }
136
+ else {
137
+ items.push(itemId);
138
+ if (items.length > _this.max) {
139
+ items.shift();
140
+ }
141
+ return _this.repository.update(id, items);
142
+ }
143
+ }
144
+ });
145
+ };
146
+ SavedService.prototype.remove = function (id, itemId) {
147
+ var _this = this;
148
+ return this.repository.load(id).then(function (items) {
149
+ if (items == null) {
150
+ return Promise.resolve(0);
151
+ }
152
+ else {
153
+ if (items.includes(itemId)) {
154
+ items = items.filter(function (item) {
155
+ return item !== itemId;
156
+ });
157
+ return _this.repository.update(id, items);
158
+ }
159
+ else {
160
+ return Promise.resolve(0);
161
+ }
162
+ }
163
+ });
164
+ };
165
+ return SavedService;
166
+ }());
167
+ exports.SavedService = SavedService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onecore",
3
- "version": "0.0.37",
3
+ "version": "0.0.40",
4
4
  "description": "onecore",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -47,7 +47,7 @@ export type Generate = () => string;
47
47
  export type SaveValues = (values: string[]) => Promise<number>;
48
48
  export type SaveStrings = (values: string[]) => Promise<number>;
49
49
  export interface QueryService<T> {
50
- query(keyword: string, max?: number): Promise<T[]>
50
+ query(keyword: string, max?: number): Promise<T[]>;
51
51
  }
52
52
  export type Get<T> = (keyword: string, max?: number) => Promise<T[]>;
53
53
  export type Load<T> = (keyword: string, max?: number) => Promise<T[]>;
@@ -140,3 +140,57 @@ export const DefaultGenericService = GenericManager;
140
140
  export const GenericSearchManager = Manager;
141
141
  export const DefaultService = GenericManager;
142
142
  export const DefaultGenericSearchService = GenericManager;
143
+ export interface SavedRepository<ID> {
144
+ load(id: ID): Promise<string[]|null>;
145
+ insert(id: ID, arr: string[]): Promise<number>;
146
+ update(id: ID, arr: string[]): Promise<number>;
147
+ }
148
+ // tslint:disable-next-line:max-classes-per-file
149
+ export class SavedService<ID, T> {
150
+ constructor(protected repository: SavedRepository<ID>, protected query: (ids: string[]) => Promise<T[]>, public max: number) {
151
+ this.load = this.load.bind(this);
152
+ this.save = this.save.bind(this);
153
+ this.remove = this.remove.bind(this);
154
+ }
155
+ load(id: ID): Promise<T[]> {
156
+ return this.repository.load(id).then(items => {
157
+ if (!items || items.length === 0) {
158
+ return [];
159
+ }
160
+ return this.query(items);
161
+ });
162
+ }
163
+ save(id: ID, itemId: string): Promise<number> {
164
+ return this.repository.load(id).then(items => {
165
+ if (items == null) {
166
+ return this.repository.insert(id, [itemId]);
167
+ } else {
168
+ if (items.includes(itemId)) {
169
+ return Promise.resolve(0);
170
+ } else {
171
+ items.push(itemId);
172
+ if (items.length > this.max) {
173
+ items.shift();
174
+ }
175
+ return this.repository.update(id, items);
176
+ }
177
+ }
178
+ });
179
+ }
180
+ remove(id: ID, itemId: string): Promise<number> {
181
+ return this.repository.load(id).then(items => {
182
+ if (items == null) {
183
+ return Promise.resolve(0);
184
+ } else {
185
+ if (items.includes(itemId)) {
186
+ items = items.filter((item: any) => {
187
+ return item !== itemId;
188
+ });
189
+ return this.repository.update(id, items);
190
+ } else {
191
+ return Promise.resolve(0);
192
+ }
193
+ }
194
+ });
195
+ }
196
+ }
package/src/model.ts CHANGED
@@ -158,3 +158,60 @@ export interface RoleFilter extends Filter {
158
158
  export type Validate<T> = (obj: T, patch?: boolean) => Promise<ErrorMessage[]>;
159
159
  export type Save<T> = (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>;
160
160
  export type Load<T, ID> = ((id: ID, ctx?: any) => Promise<T|null>);
161
+
162
+ export interface ReviewComment {
163
+ commentId: string;
164
+ id: string;
165
+ author: string;
166
+ userId: string;
167
+ comment: string;
168
+ time: Date;
169
+ updatedAt?: Date;
170
+ histories?: ShortComment[];
171
+ userURL?: string;
172
+ authorURL?: string;
173
+ }
174
+ export interface ShortComment {
175
+ comment: string;
176
+ time: Date;
177
+ }
178
+
179
+ export interface ReviewCommentFilter extends Filter {
180
+ commentId?: string;
181
+ id?: string;
182
+ author?: string;
183
+ userId?: string;
184
+ comment?: string;
185
+ time?: DateRange;
186
+ updatedAt?: DateRange;
187
+ }
188
+ export interface Info {
189
+ id: string;
190
+ rate: number;
191
+ rate1: number;
192
+ rate2: number;
193
+ rate3: number;
194
+ rate4: number;
195
+ rate5: number;
196
+ count: number;
197
+ score: number;
198
+ }
199
+ export interface Info10 {
200
+ id: string;
201
+ rate: number;
202
+ rate1: number;
203
+ rate2: number;
204
+ rate3: number;
205
+ rate4: number;
206
+ rate5: number;
207
+ rate6: number;
208
+ rate7: number;
209
+ rate8: number;
210
+ rate9: number;
211
+ rate10: number;
212
+ count: number;
213
+ score: number;
214
+ }
215
+ export interface InfoRepository<ID> {
216
+ exist(id: ID, ctx?: any): Promise<boolean>;
217
+ }
package/tsconfig.json CHANGED
@@ -14,7 +14,8 @@
14
14
  "removeComments": true,
15
15
  "lib": [
16
16
  "es5",
17
- "es6"
17
+ "es6",
18
+ "es7"
18
19
  ]
19
20
  },
20
21
  "include": [