Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 325a3a4

Browse filesBrowse files
committed
FEAT: Update user data by id
1 parent 4114896 commit 325a3a4
Copy full SHA for 325a3a4

File tree

Expand file treeCollapse file tree

2 files changed

+32
-8
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+32
-8
lines changed

‎src/users/users.controller.ts

Copy file name to clipboardExpand all lines: src/users/users.controller.ts
+22-5Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common';
1+
import {
2+
Body,
3+
Controller,
4+
Delete,
5+
Get,
6+
Param,
7+
Patch,
8+
Post,
9+
} from '@nestjs/common';
210
import { CreateUserDto } from './dto/create-user.dto';
11+
import { UpdateUserDto } from './dto/update-user.dto';
312
import { User } from './entity/user.entity';
413
import { UsersService } from './users.service';
514

@@ -8,13 +17,13 @@ export class UsersController {
817
constructor(private readonly userService: UsersService) {}
918

1019
@Get()
11-
async findAll(): Promise<User[]> {
12-
return await this.userService.findAll();
20+
async getAllUser(): Promise<User[]> {
21+
return await this.userService.getAllUser();
1322
}
1423

1524
@Get('/:id')
16-
async findOne(@Param('id') userId: number): Promise<User> {
17-
return await this.userService.findOne(userId);
25+
async getUserById(@Param('id') userId: number): Promise<User> {
26+
return await this.userService.getUserById(userId);
1827
}
1928

2029
@Post()
@@ -27,4 +36,12 @@ export class UsersController {
2736
// return을 안하면 insomnia에서 에러가 안오는 것 같다
2837
return this.userService.deleteUser(userId);
2938
}
39+
40+
@Patch('/:id')
41+
async updateUser(
42+
@Param('id') userId: number,
43+
@Body() updateUserDto: UpdateUserDto,
44+
): Promise<User> {
45+
return this.userService.updateUser(userId, updateUserDto);
46+
}
3047
}

‎src/users/users.service.ts

Copy file name to clipboardExpand all lines: src/users/users.service.ts
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Injectable, NotFoundException } from '@nestjs/common';
22
import { InjectRepository } from '@nestjs/typeorm';
3-
import { NotFoundError } from 'rxjs';
43
import { CreateUserDto } from './dto/create-user.dto';
4+
import { UpdateUserDto } from './dto/update-user.dto';
55
import { User } from './entity/user.entity';
66
import { UsersRepository } from './users.repository';
77

@@ -13,12 +13,12 @@ export class UsersService {
1313
) {}
1414

1515
// 모든 유저 정보 받기
16-
async findAll(): Promise<User[]> {
16+
async getAllUser(): Promise<User[]> {
1717
return this.userRepository.find();
1818
}
1919

2020
// id로 유저 찾기
21-
async findOne(id: number): Promise<User | null> {
21+
async getUserById(id: number): Promise<User> {
2222
const user = await this.userRepository.findOneBy({ id });
2323
if (!user) {
2424
throw new NotFoundException(`User with Id ${id} not found`);
@@ -34,6 +34,7 @@ export class UsersService {
3434
userPassword,
3535
userName,
3636
});
37+
// const user = this.userRepository.create(createUserDto);
3738
console.log(user);
3839
await this.userRepository.save(user);
3940
return user;
@@ -46,4 +47,10 @@ export class UsersService {
4647
throw new NotFoundException(`User with Id ${id} not found`);
4748
}
4849
}
50+
51+
async updateUser(id: number, updateUserDto: UpdateUserDto): Promise<User> {
52+
const user = await this.getUserById(id);
53+
this.userRepository.update(id, updateUserDto);
54+
return user;
55+
}
4956
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.