diff --git a/src/main/java/com/noplanb/domain/auth/repository/TokenRepository.java b/src/main/java/com/noplanb/domain/auth/repository/TokenRepository.java index 1b040c1..0a10a00 100644 --- a/src/main/java/com/noplanb/domain/auth/repository/TokenRepository.java +++ b/src/main/java/com/noplanb/domain/auth/repository/TokenRepository.java @@ -1,6 +1,7 @@ package com.noplanb.domain.auth.repository; import com.noplanb.domain.auth.domain.Token; +import com.noplanb.domain.user.domain.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -8,7 +9,7 @@ @Repository public interface TokenRepository extends JpaRepository { - Optional findByEmail(String email); + Token findByEmail(String email); Optional findByRefreshToken(String refreshToken); } \ No newline at end of file diff --git a/src/main/java/com/noplanb/domain/character/domain/Character.java b/src/main/java/com/noplanb/domain/character/domain/Character.java index 5c97fc6..adb198d 100644 --- a/src/main/java/com/noplanb/domain/character/domain/Character.java +++ b/src/main/java/com/noplanb/domain/character/domain/Character.java @@ -30,16 +30,15 @@ public class Character extends BaseEntity { private Long todayExp; private Long level; -// @OneToOne(mappedBy = "character", fetch = FetchType.LAZY) -// private User user; @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id", unique = true) private User user; @JsonIgnore - @OneToMany(mappedBy = "character", fetch = FetchType.LAZY) + @OneToMany(mappedBy = "character", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) private List quests = new ArrayList<>(); - @OneToMany(mappedBy = "character") + + @OneToMany(mappedBy = "character", cascade = CascadeType.ALL, orphanRemoval = true) private List items = new ArrayList<>(); diff --git a/src/main/java/com/noplanb/domain/character/repository/CharacterRepository.java b/src/main/java/com/noplanb/domain/character/repository/CharacterRepository.java index af8d3d5..8546895 100644 --- a/src/main/java/com/noplanb/domain/character/repository/CharacterRepository.java +++ b/src/main/java/com/noplanb/domain/character/repository/CharacterRepository.java @@ -12,4 +12,6 @@ public interface CharacterRepository extends JpaRepository { Optional findByUserId(Long id); boolean existsByUser(User user); + + void deleteByUser(User user); } diff --git a/src/main/java/com/noplanb/domain/user/application/UserService.java b/src/main/java/com/noplanb/domain/user/application/UserService.java index 8636766..3d9fcc8 100644 --- a/src/main/java/com/noplanb/domain/user/application/UserService.java +++ b/src/main/java/com/noplanb/domain/user/application/UserService.java @@ -1,5 +1,7 @@ package com.noplanb.domain.user.application; +import com.noplanb.domain.auth.domain.Token; +import com.noplanb.domain.auth.repository.TokenRepository; import com.noplanb.domain.character.domain.Character; import com.noplanb.domain.character.repository.CharacterRepository; import com.noplanb.domain.user.domain.User; @@ -21,7 +23,8 @@ @Transactional(readOnly = true) public class UserService { - final UserRepository userRepository; + private final TokenRepository tokenRepository; + private final UserRepository userRepository; private final CharacterRepository characterRepository; @Transactional @@ -61,4 +64,26 @@ public ResponseEntity getUserCharacterExist(UserPrincipal userPrincipal) { return ResponseEntity.ok(apiResponse); } + @Transactional + public ResponseEntity cancelAccount(UserPrincipal userPrincipal) { + User user = userRepository.findById(userPrincipal.getId()).orElseThrow(() -> new IllegalArgumentException("사용자를 찾을 수 없습니다.")); + String email = user.getEmail(); + + // 토큰 정보 삭제 + Token token = tokenRepository.findByEmail(email); + if (token != null) { tokenRepository.delete(token); } + + // 캐릭터 정보 삭제 + characterRepository.deleteByUser(user); + + // 유저 정보 삭제 + userRepository.delete(user); + + ApiResponse apiResponse = ApiResponse.builder() + .check(true) + .information("회원 탈퇴가 완료되었습니다.") + .build(); + + return ResponseEntity.ok(apiResponse); + } } diff --git a/src/main/java/com/noplanb/domain/user/controller/UserController.java b/src/main/java/com/noplanb/domain/user/controller/UserController.java index 6b2513f..de9e9e6 100644 --- a/src/main/java/com/noplanb/domain/user/controller/UserController.java +++ b/src/main/java/com/noplanb/domain/user/controller/UserController.java @@ -18,7 +18,9 @@ import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller @@ -48,4 +50,16 @@ public ResponseEntity getMyAccount(@Parameter @CurrentUser UserPrincipal user public ResponseEntity getUserCharacterExist(@Parameter @CurrentUser UserPrincipal userPrincipal){ return userService.getUserCharacterExist(userPrincipal); } + + @Operation(summary = "회원 탈퇴 API", description = "회원 탈퇴 API입니다.") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "탈퇴 성공", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Message.class) ) } ), + @ApiResponse(responseCode = "400", description = "탈퇴 실패", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class) ) } ), + }) + @DeleteMapping("/user") + public ResponseEntity cancelAccount(@Parameter @CurrentUser UserPrincipal userPrincipal) { + { + return userService.cancelAccount(userPrincipal); + } + } }