Skip to content

Commit

Permalink
Merge pull request #36 from zhumeisongsong/feature/user-usecase
Browse files Browse the repository at this point in the history
feat: ✨ GetUserUsecase
  • Loading branch information
zhumeisongsong authored Nov 14, 2024
2 parents 1a7dd82 + 29547f2 commit 348753e
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 26 deletions.
27 changes: 11 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 <plugin-name>` 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 &raquo;](https://nx.dev/concepts/nx-plugins?utm_source=nx_project&utm_medium=readme&utm_campaign=nx_projects) | [Browse the plugin registry &raquo;](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 &raquo;](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.<br/> 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. <br/>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&amp;utm_medium=readme&amp;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)
Expand Down
2 changes: 2 additions & 0 deletions libs/user/domain/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// user
export * from './lib/user.entity';
export * from './lib/user.repository';
32 changes: 32 additions & 0 deletions libs/user/domain/src/lib/user.repository.spec.ts
Original file line number Diff line number Diff line change
@@ -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<User | null> {
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();
});

});
5 changes: 5 additions & 0 deletions libs/user/domain/src/lib/user.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { User } from './user.entity';

export interface UserRepository {
findById(id: string): Promise<User | null>;
}
36 changes: 36 additions & 0 deletions libs/user/usecase/src/lib/get-user.usecase.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { GetUserUsecase } from './get-user.usecase';
import { UserRepository } from '@user/domain';

describe('GetUserUsecase', () => {
let getUserUsecase: GetUserUsecase;
let userRepository: jest.Mocked<UserRepository>;

beforeEach(() => {
userRepository = {
findById: jest.fn(),
} as unknown as jest.Mocked<UserRepository>;

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');
});
});
});
9 changes: 9 additions & 0 deletions libs/user/usecase/src/lib/get-user.usecase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { User, UserRepository } from '@user/domain';

export class GetUserUsecase {
constructor(private readonly userRepository: UserRepository) {}

async execute(id: string): Promise<User | null> {
return this.userRepository.findById(id);
}
}
7 changes: 0 additions & 7 deletions libs/user/usecase/src/lib/user-usecase.spec.ts

This file was deleted.

3 changes: 0 additions & 3 deletions libs/user/usecase/src/lib/user-usecase.ts

This file was deleted.

0 comments on commit 348753e

Please sign in to comment.