|
1 | 1 | import { Injectable } from '@nestjs/common'; |
| 2 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 3 | +import { Repository } from 'typeorm'; |
| 4 | +import { PageOptionsDto, PageDto, PageMetaDto } from '../../common/pagination'; |
2 | 5 | import { CreateUserDto } from './dto/create-user.dto'; |
3 | 6 | import { UpdateUserDto } from './dto/update-user.dto'; |
| 7 | +import { User } from './entities/user.entity'; |
4 | 8 |
|
5 | 9 | @Injectable() |
6 | 10 | export class UsersService { |
| 11 | + constructor( |
| 12 | + @InjectRepository(User) |
| 13 | + private readonly userRepository: Repository<User>, |
| 14 | + ) {} |
| 15 | + |
7 | 16 | create(createUserDto: CreateUserDto) { |
8 | | - return 'This action adds a new user'; |
| 17 | + const user = this.userRepository.create(createUserDto); |
| 18 | + return this.userRepository.save(user); |
9 | 19 | } |
10 | 20 |
|
11 | | - findAll() { |
12 | | - return `This action returns all users`; |
| 21 | + async findAll(pageOptionsDto: PageOptionsDto): Promise<PageDto<User>> { |
| 22 | + const queryBuilder = this.userRepository.createQueryBuilder('user'); |
| 23 | + |
| 24 | + if (pageOptionsDto.filter) { |
| 25 | + queryBuilder.where('user.username LIKE :filter', { |
| 26 | + filter: `%${pageOptionsDto.filter}%`, |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + if (pageOptionsDto.cursor) { |
| 31 | + const cursorDate = new Date(pageOptionsDto.cursor); |
| 32 | + queryBuilder.andWhere('user.createdAt > :cursor', { cursor: cursorDate }); |
| 33 | + } |
| 34 | + |
| 35 | + if (pageOptionsDto.sort) { |
| 36 | + const [field, order] = pageOptionsDto.sort.split(':'); |
| 37 | + queryBuilder.orderBy(`user.${field}`, order as 'ASC' | 'DESC'); |
| 38 | + } else { |
| 39 | + queryBuilder.orderBy('user.createdAt', 'DESC'); |
| 40 | + } |
| 41 | + |
| 42 | + queryBuilder.take(pageOptionsDto.limit); |
| 43 | + |
| 44 | + const itemCount = await queryBuilder.getCount(); |
| 45 | + const { entities } = await queryBuilder.getRawAndEntities(); |
| 46 | + |
| 47 | + const nextCursor = |
| 48 | + entities.length > 0 |
| 49 | + ? entities[entities.length - 1].createdAt.toISOString() |
| 50 | + : null; |
| 51 | + const pageMetaDto = new PageMetaDto({ |
| 52 | + itemCount, |
| 53 | + pageOptionsDto, |
| 54 | + nextCursor, |
| 55 | + }); |
| 56 | + |
| 57 | + return new PageDto(entities, pageMetaDto); |
13 | 58 | } |
14 | 59 |
|
15 | 60 | findOne(id: string) { |
16 | | - return `This action returns a user with id #${id}`; |
| 61 | + return this.userRepository.findOne({ where: { id } }); |
17 | 62 | } |
18 | 63 |
|
19 | | - update(id: string, updateUserDto: UpdateUserDto) { |
20 | | - return `This action updates a user with id #${id}`; |
| 64 | + async update(id: string, updateUserDto: UpdateUserDto) { |
| 65 | + await this.userRepository.update(id, updateUserDto); |
| 66 | + return this.findOne(id); |
21 | 67 | } |
22 | 68 |
|
23 | 69 | remove(id: string) { |
24 | | - return `This action removes a user with id #${id}`; |
| 70 | + return this.userRepository.softDelete(id); |
25 | 71 | } |
26 | 72 | } |
0 commit comments