refactor: FindUserQueryHandler now depends on a repository port
This commit is contained in:
@@ -1,12 +1,19 @@
|
||||
import { RepositoryPort } from '@libs/ddd/domain/ports/repository.ports';
|
||||
import { UserEntity, UserProps } from '../domain/entities/user.entity';
|
||||
|
||||
/* Repository port belongs to application's core, but since it usually
|
||||
interface FindUsersParams {
|
||||
readonly country?: string;
|
||||
readonly postalCode?: string;
|
||||
readonly street?: string;
|
||||
}
|
||||
|
||||
/* Repository port belongs to application's core / domain, but since it usually
|
||||
changes together with repository it is kept in the same directory for
|
||||
convenience. */
|
||||
export interface UserRepositoryPort
|
||||
extends RepositoryPort<UserEntity, UserProps> {
|
||||
findOneByIdOrThrow(id: string): Promise<UserEntity>;
|
||||
findOneByEmailOrThrow(email: string): Promise<UserEntity>;
|
||||
findUsers(query: FindUsersParams): Promise<UserEntity[]>;
|
||||
exists(email: string): Promise<boolean>;
|
||||
}
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
import { UserRepository } from '@modules/user/database/user.repository';
|
||||
import { QueryHandlerBase } from '@src/libs/ddd/domain/base-classes/query-handler.base';
|
||||
import { QueryHandler } from '@nestjs/cqrs';
|
||||
import { Ok, Result } from 'oxide.ts/dist';
|
||||
import { Inject } from '@nestjs/common';
|
||||
import { FindUsersQuery } from './find-users.query';
|
||||
import { UserEntity } from '../../domain/entities/user.entity';
|
||||
import { UserRepositoryPort } from '../../database/user.repository.port';
|
||||
import { UserRepository } from '../../database/user.repository';
|
||||
|
||||
@QueryHandler(FindUsersQuery)
|
||||
export class FindUsersQueryHandler extends QueryHandlerBase {
|
||||
constructor(private readonly userRepo: UserRepository) {
|
||||
constructor(
|
||||
@Inject(UserRepository)
|
||||
private readonly userRepo: UserRepositoryPort,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
/* Since this is a simple query with no additional business
|
||||
logic involved, it bypasses application's core completely
|
||||
and retrieves users directly from a repository.
|
||||
*/
|
||||
async handle(query: FindUsersQuery): Promise<Result<UserEntity[], Error>> {
|
||||
const users = await this.userRepo.findUsers(query);
|
||||
return Ok(users);
|
||||
|
||||
Reference in New Issue
Block a user