initial
This commit is contained in:
43
apps/backend/src/users/repositories/users.repository.ts
Normal file
43
apps/backend/src/users/repositories/users.repository.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { EntityManager, Repository } from 'typeorm';
|
||||
import { UserEntity } from '../entities/user.entity';
|
||||
|
||||
@Injectable()
|
||||
export class UsersRepository {
|
||||
constructor(
|
||||
@InjectRepository(UserEntity) private readonly repo: Repository<UserEntity>,
|
||||
) {}
|
||||
|
||||
findById(id: string): Promise<UserEntity | null> {
|
||||
return this.repo.findOne({ where: { id } });
|
||||
}
|
||||
|
||||
findByIdentity(issuer: string, subject: string): Promise<UserEntity | null> {
|
||||
return this.repo.findOne({ where: { issuer, subject } });
|
||||
}
|
||||
|
||||
async search(
|
||||
query: string | undefined,
|
||||
page: number,
|
||||
pageSize: number,
|
||||
): Promise<[UserEntity[], number]> {
|
||||
const qb = this.repo
|
||||
.createQueryBuilder('user')
|
||||
.leftJoinAndSelect('user.roles', 'role');
|
||||
if (query) {
|
||||
qb.where('user.name LIKE :query OR user.email LIKE :query', {
|
||||
query: `%${query}%`,
|
||||
});
|
||||
}
|
||||
return qb
|
||||
.orderBy('user.createdAt', 'DESC')
|
||||
.skip((page - 1) * pageSize)
|
||||
.take(pageSize)
|
||||
.getManyAndCount();
|
||||
}
|
||||
|
||||
async save(user: UserEntity, manager?: EntityManager): Promise<UserEntity> {
|
||||
return (manager?.getRepository(UserEntity) ?? this.repo).save(user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user