,
22 | ) {}
23 |
24 | getUsers() {
25 | return this.userRepository.find();
26 | }
27 |
28 | getUser(id: number) {
29 | return this.userRepository.findOneBy({ id });
30 | }
31 |
32 | searchUsers(searchUsersDto: SearchUsersDto) {
33 | return this.userRepository.find({
34 | where: { name: searchUsersDto.name },
35 | skip: searchUsersDto.skip,
36 | take: searchUsersDto.take,
37 | });
38 | }
39 |
40 | getUserByEmail(email: string) {
41 | return this.userRepository.findOneBy({ email });
42 | }
43 |
44 | async createUser(signupCredentialsDto: SignupCredentialsDto) {
45 | const foundUser = await this.getUserByEmail(signupCredentialsDto.email);
46 | if (foundUser) {
47 | throw new ConflictException('Email already in use');
48 | }
49 | const passwordHash = Hash.generateHash(signupCredentialsDto.password);
50 | const user = this.userRepository.create({
51 | name: signupCredentialsDto.name,
52 | bio: signupCredentialsDto.bio,
53 | email: signupCredentialsDto.email,
54 | password: passwordHash,
55 | });
56 | await this.userRepository.save(user);
57 | return user;
58 | }
59 |
60 | async updateUser(id: number, updateUserDto: UpdateUserDto) {
61 | const result = await this.userRepository.update(id, updateUserDto);
62 | if (result.affected === 0) {
63 | throw new NotFoundException(`User with id ${id} not found`);
64 | }
65 | }
66 |
67 | async updatePassword(id: number, updatePasswordDto: UpdatePasswordDto) {
68 | const { oldPassword, newPassword } = updatePasswordDto;
69 | const user = await this.userRepository.findOneBy({ id });
70 | if (!user) {
71 | throw new NotFoundException(`User with id ${id} not found`);
72 | }
73 | const isPasswordValid = Hash.compare(oldPassword, user.password);
74 | if (!isPasswordValid) {
75 | throw new UnauthorizedException('Invalid credentials');
76 | }
77 | const newPasswordHash = Hash.generateHash(newPassword);
78 | user.password = newPasswordHash;
79 | await this.userRepository.save(user);
80 | }
81 |
82 | async deleteUser(id: number) {
83 | const result = await this.userRepository.delete(id);
84 | if (result.affected === 0) {
85 | throw new NotFoundException(`User with id ${id} not found`);
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/server/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
6 | [circleci-url]: https://circleci.com/gh/nestjs/nest
7 |
8 | A progressive Node.js framework for building efficient and scalable server-side applications.
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
24 |
25 | ## Description
26 |
27 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
28 |
29 | ## Installation
30 |
31 | ```bash
32 | $ npm install
33 | ```
34 |
35 | ## Running the app
36 |
37 | ```bash
38 | # development
39 | $ npm run start
40 |
41 | # watch mode
42 | $ npm run start:dev
43 |
44 | # production mode
45 | $ npm run start:prod
46 | ```
47 |
48 | ## Test
49 |
50 | ```bash
51 | # unit tests
52 | $ npm run test
53 |
54 | # e2e tests
55 | $ npm run test:e2e
56 |
57 | # test coverage
58 | $ npm run test:cov
59 | ```
60 |
61 | ## Support
62 |
63 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
64 |
65 | ## Stay in touch
66 |
67 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
68 | - Website - [https://nestjs.com](https://nestjs.com/)
69 | - Twitter - [@nestframework](https://twitter.com/nestframework)
70 |
71 | ## License
72 |
73 | Nest is [MIT licensed](LICENSE).
74 |
--------------------------------------------------------------------------------