Skip to content

Commit

Permalink
Merge pull request #52 from DEPthes/feat/user-character-exist
Browse files Browse the repository at this point in the history
회원 탈퇴 기능 구현
  • Loading branch information
lsn5963 authored Aug 21, 2024
2 parents aece555 + 41cea19 commit c6c2bfb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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;

import java.util.Optional;

@Repository
public interface TokenRepository extends JpaRepository<Token, Long> {
Optional<Token> findByEmail(String email);
Token findByEmail(String email);
Optional<Token> findByRefreshToken(String refreshToken);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Quest> quests = new ArrayList<>();
@OneToMany(mappedBy = "character")

@OneToMany(mappedBy = "character", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Item> items = new ArrayList<>();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface CharacterRepository extends JpaRepository<Character,Long> {
Optional<Character> findByUserId(Long id);

boolean existsByUser(User user);

void deleteByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit c6c2bfb

Please sign in to comment.