diff --git a/README.md b/README.md index 4474857..5a66768 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,7 @@ ✨ Your new, shiny [Nx workspace](https://nx.dev) is almost ready ✨. -[Learn more about this workspace setup and its capabilities](https://nx.dev/nx-api/nest?utm_source=nx_project&utm_medium=readme&utm_campaign=nx_projects) or run `npx nx graph` to visually explore what was created. Now, let's get you up to speed! - -## Finish your CI setup - -[Click here to finish setting up your workspace!](https://cloud.nx.app/connect/DryfbJA1XD) - +[Learn more about this workspace setup and its capabilities](https://nx.dev/nx-api/nest?utm_source=nx_project&utm_medium=readme&utm_campaign=nx_projects) or run `npx nx graph` to visually explore what was created. Now, let's get you up to speed! ## Run tasks @@ -44,38 +39,38 @@ Use the plugin's generator to create new projects. To generate a new application, use: ```sh -npx nx g @nx/nest:app demo +npx nx g @nx/nest:app xxx ``` To generate a new library, use: ```sh -npx nx g @nx/node:lib mylib +npx nx g @nx/node:lib xxx ``` You can use `npx nx list` to get a list of installed plugins. Then, run `npx nx list ` to learn about more specific capabilities of a particular plugin. Alternatively, [install Nx Console](https://nx.dev/getting-started/editor-setup?utm_source=nx_project&utm_medium=readme&utm_campaign=nx_projects) to browse plugins and generators in your IDE. [Learn more about Nx plugins »](https://nx.dev/concepts/nx-plugins?utm_source=nx_project&utm_medium=readme&utm_campaign=nx_projects) | [Browse the plugin registry »](https://nx.dev/plugin-registry?utm_source=nx_project&utm_medium=readme&utm_campaign=nx_projects) +## Architecture -[Learn more about Nx on CI](https://nx.dev/ci/intro/ci-with-nx#ready-get-started-with-your-provider?utm_source=nx_project&utm_medium=readme&utm_campaign=nx_projects) - -## Install Nx Console - -Nx Console is an editor extension that enriches your developer experience. It lets you run tasks, generate code, and improves code autocompletion in your IDE. It is available for VSCode and IntelliJ. - -[Install Nx Console »](https://nx.dev/getting-started/editor-setup?utm_source=nx_project&utm_medium=readme&utm_campaign=nx_projects) +| Layer | Description | +| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| usecase | Define business use cases and encapsulate business logic. | +| entity(domain) | Define core business entities and business rules.
Maintain entity independence from database and framework. | +| repository(domain) | Interfaces (or abstract classes), which define methods for manipulating data without concern for specific database implementations.
By defining this interface, we can decouple database access: the specific details of data access will be done by implementation classes, such as specific implementations using tools like Mongoose, TypeORM, Prisma, and so on. | ## Useful links Learn more: -- [Learn more about this workspace setup](https://nx.dev/nx-api/nest?utm_source=nx_project&utm_medium=readme&utm_campaign=nx_projects) +- [Learn more about this workspace setup](https://nx.dev/nx-api/nest?utm_source=nx_project&utm_medium=readme&utm_campaign=nx_projects) - [Learn about Nx on CI](https://nx.dev/ci/intro/ci-with-nx?utm_source=nx_project&utm_medium=readme&utm_campaign=nx_projects) - [Releasing Packages with Nx release](https://nx.dev/features/manage-releases?utm_source=nx_project&utm_medium=readme&utm_campaign=nx_projects) - [What are Nx plugins?](https://nx.dev/concepts/nx-plugins?utm_source=nx_project&utm_medium=readme&utm_campaign=nx_projects) And join the Nx community: + - [Discord](https://go.nx.dev/community) - [Follow us on X](https://twitter.com/nxdevtools) or [LinkedIn](https://www.linkedin.com/company/nrwl) - [Our Youtube channel](https://www.youtube.com/@nxdevtools) diff --git a/libs/user/domain/src/index.ts b/libs/user/domain/src/index.ts index 2dbc61e..9602d75 100644 --- a/libs/user/domain/src/index.ts +++ b/libs/user/domain/src/index.ts @@ -1 +1,3 @@ +// user export * from './lib/user.entity'; +export * from './lib/user.repository'; diff --git a/libs/user/domain/src/lib/user.repository.spec.ts b/libs/user/domain/src/lib/user.repository.spec.ts new file mode 100644 index 0000000..f3be2c5 --- /dev/null +++ b/libs/user/domain/src/lib/user.repository.spec.ts @@ -0,0 +1,32 @@ +import { UserRepository } from './user.repository'; +import { User } from './user.entity'; + +class MockUserRepository implements UserRepository { + private users: User[] = [ + { id: '1', name: 'John Doe' }, + { id: '2', name: 'Jane Doe' }, + ]; + + async findById(id: string): Promise { + return this.users.find((user) => user.id === id) || null; + } +} + +describe('UserRepository', () => { + let userRepository: UserRepository; + + beforeEach(() => { + userRepository = new MockUserRepository(); + }); + + test('findById should return a user by id', async () => { + const user = await userRepository.findById('1'); + expect(user).toEqual({ id: '1', name: 'John Doe' }); + }); + + test('findById should return null if user not found', async () => { + const user = await userRepository.findById('3'); + expect(user).toBeNull(); + }); + +}); diff --git a/libs/user/domain/src/lib/user.repository.ts b/libs/user/domain/src/lib/user.repository.ts new file mode 100644 index 0000000..0712d58 --- /dev/null +++ b/libs/user/domain/src/lib/user.repository.ts @@ -0,0 +1,5 @@ +import { User } from './user.entity'; + +export interface UserRepository { + findById(id: string): Promise; +} \ No newline at end of file diff --git a/libs/user/usecase/src/lib/get-user.usecase.spec.ts b/libs/user/usecase/src/lib/get-user.usecase.spec.ts new file mode 100644 index 0000000..0b2ae94 --- /dev/null +++ b/libs/user/usecase/src/lib/get-user.usecase.spec.ts @@ -0,0 +1,36 @@ +import { GetUserUsecase } from './get-user.usecase'; +import { UserRepository } from '@user/domain'; + +describe('GetUserUsecase', () => { + let getUserUsecase: GetUserUsecase; + let userRepository: jest.Mocked; + + beforeEach(() => { + userRepository = { + findById: jest.fn(), + } as unknown as jest.Mocked; + + getUserUsecase = new GetUserUsecase(userRepository); + }); + + describe('execute', () => { + it('should return a user when found', async () => { + const user = { id: '1', name: 'John Doe' }; + userRepository.findById.mockResolvedValue(user); + + const result = await getUserUsecase.execute('1'); + + expect(result).toEqual(user); + expect(userRepository.findById).toHaveBeenCalledWith('1'); + }); + + it('should return null when user is not found', async () => { + userRepository.findById.mockResolvedValue(null); + + const result = await getUserUsecase.execute('1'); + + expect(result).toBeNull(); + expect(userRepository.findById).toHaveBeenCalledWith('1'); + }); + }); +}); \ No newline at end of file diff --git a/libs/user/usecase/src/lib/get-user.usecase.ts b/libs/user/usecase/src/lib/get-user.usecase.ts new file mode 100644 index 0000000..9012a7f --- /dev/null +++ b/libs/user/usecase/src/lib/get-user.usecase.ts @@ -0,0 +1,9 @@ +import { User, UserRepository } from '@user/domain'; + +export class GetUserUsecase { + constructor(private readonly userRepository: UserRepository) {} + + async execute(id: string): Promise { + return this.userRepository.findById(id); + } +} diff --git a/libs/user/usecase/src/lib/user-usecase.spec.ts b/libs/user/usecase/src/lib/user-usecase.spec.ts deleted file mode 100644 index db5d6da..0000000 --- a/libs/user/usecase/src/lib/user-usecase.spec.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { userUsecase } from './user-usecase'; - -describe('userUsecase', () => { - it('should work', () => { - expect(userUsecase()).toEqual('user-usecase'); - }); -}); diff --git a/libs/user/usecase/src/lib/user-usecase.ts b/libs/user/usecase/src/lib/user-usecase.ts deleted file mode 100644 index 55af887..0000000 --- a/libs/user/usecase/src/lib/user-usecase.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function userUsecase(): string { - return 'user-usecase'; -}