-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserService.ts
53 lines (44 loc) · 1.56 KB
/
UserService.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
import { Service } from 'typedi';
import { OrmRepository } from 'typeorm-typedi-extensions';
import { User } from '../models';
import { BaseService } from './BaseService';
import { UserRepository } from '../repositories';
@Service()
export class UserService extends BaseService {
@OrmRepository()
private userRepository: UserRepository;
constructor() {
super();
}
public async getAll(): Promise<User[]> {
return this.userRepository.find();
}
public async search({
page = this.defaultPage,
limit = this.defaultLimit,
orderBy = this.defaultOrderBy,
}): Promise<[User[], number]> {
const skipOffset = (page - 1) * limit;
// return this.userRepository.findAndCount({ skip: skipOffset, take: limit, order: orderBy });
return this.userRepository.findAndCount({});
}
public async getById(id: string): Promise<User> {
return this.userRepository.findOne(id);
}
public async create(user: User): Promise<User> {
return this.userRepository.save(user);
}
public async update(id: string, user: User): Promise<User> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const updateResult = this.userRepository.update(id, user);
// TODO: Put check on updateResult
const updatedUser = await this.getById(id);
return updatedUser;
}
public async delete(id: string): Promise<boolean> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const deleteResult = await this.userRepository.softDelete({ id });
// TODO: Put check on deleteResult
return Promise.resolve(true);
}
}