Skip to content

Commit

Permalink
✨ feat: 멤버 정보 가져오기, 수정, 삭제 기능 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanggwangseong committed Dec 5, 2024
1 parent 7083e4c commit 8e3ed0b
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
70 changes: 70 additions & 0 deletions src/controllers/members.controller.ts
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);
}
}
7 changes: 7 additions & 0 deletions src/dtos/update-member.dto.ts
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"]),
) {}
7 changes: 6 additions & 1 deletion src/entities/member.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IsEmail, IsNotEmpty, IsString } from "class-validator";
import { Exclude } from "class-transformer";
import { IsEmail, IsNotEmpty, IsString, Length } from "class-validator";
import {
Column,
CreateDateColumn,
Expand Down Expand Up @@ -31,6 +32,10 @@ export class MemberEntity {

@IsNotEmpty()
@IsString()
@Exclude({
toPlainOnly: true,
})
@Length(4, 20)
@Column({ type: "varchar", length: PASSWORD_HASH_LENGTH, nullable: false })
password!: string; // 비밀번호

Expand Down
2 changes: 1 addition & 1 deletion src/modules/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import { MembersModule } from "./members.module";
imports: [JwtModule.register({}), MembersModule],
controllers: [AuthController],
providers: [AuthService, MailsService],
exports: [],
exports: [AuthService],
})
export class AuthModule {}
24 changes: 24 additions & 0 deletions src/services/members.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Injectable } from "@nestjs/common";
import { BusinessErrorException } from "@APP/common/exception/business-error.exception";
import { MemberErrorCode } from "@APP/common/exception/error-code";
import { RegisterMemberDto } from "@APP/dtos/register-member.dto";
import { UpdateMemberDto } from "@APP/dtos/update-member.dto";
import { VerifyEmailDto } from "@APP/dtos/verify-email.dto";
import { MembersRepository } from "@APP/repositories/members.repository";
import { RefreshTokenRepository } from "@APP/repositories/refresh-token.repository";
Expand All @@ -24,6 +25,29 @@ export class MembersService {
});
}

async findById(memberId: number) {
return this.membersRepository.findOne({
where: {
id: memberId,
},
});
}

async updateById(memberId: number, dto: UpdateMemberDto) {
return this.membersRepository.update(
{
id: memberId,
},
dto,
);
}

async deleteById(memberId: number) {
return this.membersRepository.delete({
id: memberId,
});
}

async verifyEmail(dto: VerifyEmailDto) {
const member = await this.findByEmail(dto.email);

Expand Down

0 comments on commit 8e3ed0b

Please sign in to comment.