-
Notifications
You must be signed in to change notification settings - Fork 18
/
category.service.ts
95 lines (82 loc) · 2.81 KB
/
category.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { Appearance } from '../../types/appearance';
import { ServiceContext, Errors } from 'typescript-rest';
import { Category } from './interfaces/Category.interface';
import { CmsDatabase as Db } from './cms.database';
import {
CategoryResponseFields as fields,
CategoryResponse,
EditCategoryDto,
CreateCategoryDto,
PaginateCategory,
} from './dto/category.dto';
import { appearance } from './appearance/category.appearance';
import { Document, Types } from 'mongoose';
import { pick, merge } from 'lodash';
import { Repository } from '../../database/repository';
import { KeyValue, TreeNode } from '../../types/data.types';
export class CategoryService {
async getAppearance(): Promise<Appearance> {
return appearance;
}
async searchTree(keyword?: string, value?: string, limit: number = 10): Promise<Array<TreeNode>> {
return Repository.searchTree(Db.Category, keyword, value, '', limit);
}
async search(keyword?: string, value?: string, limit: number = 10): Promise<Array<KeyValue>> {
return Repository.search(Db.Category, keyword, value, '', limit);
}
async create(entry: CreateCategoryDto): Promise<CategoryResponse> {
const doc = new Db.Category(entry);
const result: any = await doc.save();
return result;
}
async update(
entry: EditCategoryDto,
): Promise<CategoryResponse> {
if (entry.id === entry.parent) {
throw new Errors.BadRequestError('can not be set parent by self.');
}
const doc: any = await Db.Category.findOneAndUpdate(
{
_id: entry.id,
},
entry,
).exec();
return doc;
}
async query(
keyword?: string,
page?: number,
size?: number,
sort?: string
): Promise<PaginateCategory> {
const condition: any = keyword ? { name: new RegExp(keyword, 'i') } : {};
const query = Db.Category.find(condition).sort(sort);
const collection = Db.Category.find(condition);
const result = await Repository.query<Category & Document,
CategoryResponse>(query, collection, page, size, fields);
return result;
}
async remove(id: string): Promise<boolean> {
return Repository.remove(Db.Category, id);
}
async get(id: string): Promise<CategoryResponse> {
const result = await Repository.get(Db.Category, id, [
{
path: 'roles',
select: 'name',
},
]);
return this.pure(result);
}
private pure(entry: Category & Document): CategoryResponse {
return pick(entry, [
'id',
'name',
'slug',
'order',
'parent',
'paths',
'description'
])
}
}