-
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.
- Loading branch information
1 parent
7083e4c
commit 8e3ed0b
Showing
5 changed files
with
108 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
ForbiddenException, | ||
Get, | ||
NotFoundException, | ||
Param, | ||
ParseIntPipe, | ||
Patch, | ||
} from "@nestjs/common"; | ||
import * as bcrypt from "bcrypt"; | ||
|
||
import { CurrentMemberDecorator } from "@APP/common/decorators/current-member.decorator"; | ||
import { UpdateMemberDto } from "@APP/dtos/update-member.dto"; | ||
import { MembersService } from "@APP/services/members.service"; | ||
|
||
@Controller("members") | ||
export class MembersController { | ||
constructor(private readonly membersService: MembersService) {} | ||
|
||
@Get(":memberId") | ||
async getMember(@Param("memberId", new ParseIntPipe()) memberId: number) { | ||
return await this.membersService.findById(memberId); | ||
} | ||
|
||
@Delete(":memberId") | ||
async deleteMember( | ||
@CurrentMemberDecorator("id") currentMemberId: number, | ||
@Param("memberId", new ParseIntPipe()) memberId: number, | ||
) { | ||
const member = await this.membersService.findById(memberId); | ||
|
||
if (!member) { | ||
throw new NotFoundException("존재하지 않는 회원입니다."); | ||
} | ||
|
||
if (currentMemberId !== memberId) { | ||
throw new ForbiddenException("권한이 없습니다."); | ||
} | ||
|
||
await this.membersService.deleteById(memberId); | ||
} | ||
|
||
@Patch(":memberId") | ||
async patchMember( | ||
@CurrentMemberDecorator("id") currentMemberId: number, | ||
@Param("memberId", new ParseIntPipe()) memberId: number, | ||
@Body() dto: UpdateMemberDto, | ||
) { | ||
const member = await this.membersService.findById(memberId); | ||
|
||
if (!member) { | ||
throw new NotFoundException("존재하지 않는 회원입니다."); | ||
} | ||
|
||
if (currentMemberId !== memberId) { | ||
throw new ForbiddenException("권한이 없습니다."); | ||
} | ||
|
||
if (dto.password) { | ||
const hashedPassword = await bcrypt.hash(dto.password, 10); | ||
dto.password = hashedPassword; | ||
} | ||
|
||
await this.membersService.updateById(memberId, dto); | ||
|
||
return await this.membersService.findById(memberId); | ||
} | ||
} |
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 { OmitType, PartialType } from "@nestjs/swagger"; | ||
|
||
import { RegisterMemberDto } from "./register-member.dto"; | ||
|
||
export class UpdateMemberDto extends PartialType( | ||
OmitType(RegisterMemberDto, ["email"]), | ||
) {} |
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