-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from zhumeisongsong/feature/user-service
feat: ✨ import getUserUseCase in UsersService
- Loading branch information
Showing
18 changed files
with
140 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './lib/use-case/get-user.use-case'; | ||
export * from './lib/use-cases/get-user.use-case'; | ||
|
||
// service | ||
export * from './lib/users.service'; |
36 changes: 0 additions & 36 deletions
36
libs/users/application/src/lib/use-case/get-user.use-case.spec.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
libs/users/application/src/lib/use-cases/get-user.use-case.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { GetUserUseCase } from './get-user.use-case'; | ||
import { UsersRepository } from '@users/domain'; | ||
|
||
describe('GetUserUseCase', () => { | ||
let getUserUseCase: GetUserUseCase; | ||
let usersRepository: UsersRepository; | ||
|
||
beforeEach(() => { | ||
usersRepository = { | ||
findById: jest.fn(), | ||
}; | ||
getUserUseCase = new GetUserUseCase(usersRepository); | ||
}); | ||
|
||
describe('execute', () => { | ||
it('should return user when found', async () => { | ||
const mockUser = { id: '1', name: 'John Doe' }; | ||
(usersRepository.findById as jest.Mock).mockResolvedValue(mockUser); | ||
|
||
const result = await getUserUseCase.execute('1'); | ||
|
||
expect(result).toEqual(mockUser); | ||
expect(usersRepository.findById).toHaveBeenCalledWith('1'); | ||
}); | ||
|
||
it('should return null when user not found', async () => { | ||
(usersRepository.findById as jest.Mock).mockResolvedValue(null); | ||
|
||
const result = await getUserUseCase.execute('1'); | ||
|
||
expect(result).toBeNull(); | ||
expect(usersRepository.findById).toHaveBeenCalledWith('1'); | ||
}); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
libs/users/application/src/lib/use-cases/get-user.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { User, USERS_REPOSITORY, UsersRepository } from '@users/domain'; | ||
|
||
@Injectable() | ||
export class GetUserUseCase { | ||
constructor( | ||
@Inject(USERS_REPOSITORY) | ||
private readonly usersRepository: UsersRepository, | ||
) {} | ||
|
||
async execute(id: string): Promise<User | null> { | ||
const user = await this.usersRepository.findById(id); | ||
|
||
if (!user) { | ||
return null; | ||
} | ||
return user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,47 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { User } from '@users/domain'; | ||
|
||
import { UsersService } from './users.service'; | ||
import { GetUserUseCase } from './use-cases/get-user.use-case'; | ||
import { User } from '@users/domain'; | ||
|
||
describe('UsersService', () => { | ||
let service: UsersService; | ||
let getUserUseCase: GetUserUseCase; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [UsersService], | ||
providers: [ | ||
UsersService, | ||
{ | ||
provide: GetUserUseCase, | ||
useValue: { | ||
execute: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<UsersService>(UsersService); | ||
getUserUseCase = module.get<GetUserUseCase>(GetUserUseCase); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
it('should return a user by id', () => { | ||
const user: User | undefined = service.findById('1'); | ||
expect(user).toEqual({ id: '1', name: 'John Doe' }); | ||
}); | ||
describe('findById', () => { | ||
it('should return a user if found', async () => { | ||
const user: User = { id: '1', name: 'John Doe' }; | ||
jest.spyOn(getUserUseCase, 'execute').mockResolvedValue(user); | ||
|
||
const result = await service.findById('1'); | ||
expect(result).toEqual(user); | ||
}); | ||
|
||
it('should return null if user not found', async () => { | ||
jest.spyOn(getUserUseCase, 'execute').mockResolvedValue(null); | ||
|
||
it('should return undefined if user is not found', () => { | ||
const user: User | undefined = service.findById('3'); | ||
expect(user).toBeUndefined(); | ||
const result = await service.findById('2'); | ||
expect(result).toBeNull(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { User } from '@users/domain'; | ||
|
||
import { GetUserUseCase } from './use-cases/get-user.use-case'; | ||
|
||
@Injectable() | ||
export class UsersService { | ||
private users: User[] = [ | ||
{ id: '1', name: 'John Doe' }, | ||
{ id: '2', name: 'Richard Roe' }, | ||
]; | ||
constructor(private readonly getUserUseCase: GetUserUseCase) {} | ||
|
||
findById(id: string): User | undefined { | ||
return this.users.find((user) => user.id === id); | ||
async findById(id: string): Promise<User | null> { | ||
return this.getUserUseCase.execute(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
// user | ||
export * from './lib/user.entity'; | ||
export * from './lib/user.repository'; | ||
export * from './lib/users.repository'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { User } from './user.entity'; | ||
|
||
export interface UsersRepository { | ||
findById(id: string): Promise<User | null>; | ||
} | ||
|
||
export const USERS_REPOSITORY = Symbol('USERS_REPOSITORY'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './lib/mongoose-user.repository'; | ||
export * from './lib/mongoose-users.repository'; | ||
export * from './lib/user.schema'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 9 additions & 3 deletions
12
...goose/src/lib/mongoose-user.repository.ts → ...oose/src/lib/mongoose-users.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,22 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { User, UserRepository } from '@users/domain'; | ||
import { User, UsersRepository } from '@users/domain'; | ||
import { Model } from 'mongoose'; | ||
|
||
import { UserDocument } from './user.schema'; | ||
|
||
export class MongooseUserRepository implements UserRepository { | ||
@Injectable() | ||
export class MongooseUsersRepository implements UsersRepository { | ||
constructor( | ||
@InjectModel(UserDocument.name) private userModel: Model<UserDocument>, | ||
) {} | ||
|
||
async findById(id: string): Promise<User | null> { | ||
const userDoc = await this.userModel.findById(id).exec(); | ||
return userDoc ? new User(userDoc.id, userDoc.name) : null; | ||
|
||
if (!userDoc) { | ||
return null; | ||
} | ||
return new User(userDoc.id, userDoc.name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.