Skip to content

Commit

Permalink
refactor: FindUserQueryHandler now depends on a repository port
Browse files Browse the repository at this point in the history
  • Loading branch information
Sairyss committed May 18, 2022
1 parent 15e8fe4 commit 1005c86
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/modules/user/database/user.repository.port.ts
Original file line number Diff line number Diff line change
@@ -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>;
}
13 changes: 7 additions & 6 deletions src/modules/user/queries/find-users/find-users.query-handler.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

0 comments on commit 1005c86

Please sign in to comment.