Skip to content

Commit

Permalink
#65 [fix] 사용자 탈퇴 api 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
hyejungg committed Sep 21, 2022
1 parent b3f4d22 commit 0a19a51
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
10 changes: 4 additions & 6 deletions src/main/java/hous/server/controller/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public ResponseEntity<String> updateRepresentBadge(@ApiParam(name = "badgeId", v
userService.updateRepresentBadge(badgeId, userId);
return SuccessResponse.NO_CONTENT;
}


@ApiOperation(
value = "[인증] 마이 페이지(설정) - 회원 정보를 삭제합니다.",
Expand All @@ -169,19 +169,17 @@ public ResponseEntity<String> updateRepresentBadge(@ApiParam(name = "badgeId", v
@ApiResponses(value = {
@ApiResponse(code = 204, message = ""),
@ApiResponse(code = 401, message = "토큰이 만료되었습니다. 다시 로그인 해주세요.", response = ErrorResponse.class),
@ApiResponse(code = 403, message = "방에 참가하고 있는 유저는 탈퇴할 수 없습니다..", response = ErrorResponse.class),
@ApiResponse(code = 404,
message = "1. 탈퇴했거나 존재하지 않는 유저입니다. \n"
+ "2. 같은 방에 참가하고 있지 않습니다.\n"
+ "3. 존재하지 않는 유저입니다.",
+ "3. 참가중인 방이 존재하지 않습니다.",
response = ErrorResponse.class),
@ApiResponse(code = 500, message = "예상치 못한 서버 에러가 발생하였습니다.", response = ErrorResponse.class)
})
@Auth
@ResponseStatus(HttpStatus.NO_CONTENT)
@PutMapping("/user")
public ResponseEntity<String> deleteUser(
@Valid @RequestBody UpdateTestScoreRequestDto request, @ApiIgnore @UserId Long userId) {
@DeleteMapping("/user")
public ResponseEntity<String> deleteUser(@ApiIgnore @UserId Long userId) {
userService.deleteUser(userId);
return SuccessResponse.NO_CONTENT;
}
Expand Down
46 changes: 29 additions & 17 deletions src/main/java/hous/server/service/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
import hous.server.domain.badge.repository.BadgeRepository;
import hous.server.domain.badge.repository.RepresentRepository;
import hous.server.domain.common.RedisKey;
import hous.server.common.exception.ForbiddenException;
import hous.server.common.exception.NotFoundException;
import hous.server.domain.notification.repository.NotificationRepository;
import hous.server.domain.personality.Personality;
import hous.server.domain.personality.PersonalityColor;
import hous.server.domain.personality.repository.PersonalityRepository;
import hous.server.domain.room.Participate;
import hous.server.domain.room.Room;
import hous.server.domain.room.Participate;
import hous.server.domain.room.repository.ParticipateRepository;
import hous.server.domain.room.repository.RoomRepository;
import hous.server.domain.todo.Todo;
import hous.server.domain.todo.repository.DoneRepository;
import hous.server.domain.todo.repository.TakeRepository;
import hous.server.domain.todo.repository.TodoRepository;
import hous.server.domain.user.Onboarding;
import hous.server.domain.user.Setting;
import hous.server.domain.user.TestScore;
Expand All @@ -26,6 +30,7 @@
import hous.server.service.badge.BadgeService;
import hous.server.service.badge.BadgeServiceUtils;
import hous.server.service.room.RoomServiceUtils;
import hous.server.service.todo.TodoServiceUtils;
import hous.server.service.user.dto.request.*;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
Expand All @@ -34,11 +39,6 @@

import java.util.List;

import java.util.List;

import static hous.server.common.exception.ErrorCode.FORBIDDEN_USER_DELETE_ROOM_PARTICIPATE_EXCEPTION;
import static hous.server.common.exception.ErrorCode.NOTFOUND_USER_EXCEPTION;

@RequiredArgsConstructor
@Service
@Transactional
Expand All @@ -54,6 +54,12 @@ public class UserService {
private final BadgeRepository badgeRepository;
private final AcquireRepository acquireRepository;
private final RepresentRepository representRepository;
private final TakeRepository takeRepository;
private final DoneRepository doneRepository;
private final TodoRepository todoRepository;
private final ParticipateRepository participateRepository;
private final RoomRepository roomRepository;
private final NotificationRepository notificationRepository;

private final BadgeService badgeService;

Expand Down Expand Up @@ -136,14 +142,20 @@ public void updateRepresentBadge(Long badgeId, Long userId) {

public void deleteUser(Long userId) {
User user = UserServiceUtils.findUserById(userRepository, userId);
List<Participate> participateList = user.getOnboarding().getParticipates();
if (!participateList.isEmpty()) {
throw new ForbiddenException(String.format("방 (%s) 의 참가자 는 탈퇴할 수 없습니다.", participateList.get(0).getRoom().getId()),
FORBIDDEN_USER_DELETE_ROOM_PARTICIPATE_EXCEPTION);
}
if (userRepository.deleteUserById(userId) != 1) {
throw new NotFoundException(String.format("존재 하지 않은 유저 (%s) 는 탈퇴할 수 없습니다.", userId),
NOTFOUND_USER_EXCEPTION);
}
Room room = RoomServiceUtils.findParticipatingRoom(user);
Onboarding me = user.getOnboarding();
Setting setting = user.getSetting();

List<Todo> todos = room.getTodos();
List<Todo> myTodos = TodoServiceUtils.filterAllDaysUserTodos(todos, me);
RoomServiceUtils.deleteMyTodosTakeMe(takeRepository, doneRepository, todoRepository, myTodos, me, room);
RoomServiceUtils.leaveParticipateRoom(participateRepository, roomRepository, me, room);
RoomServiceUtils.deleteRepresentByOnboarding(representRepository, me);
RoomServiceUtils.deleteAcquireByOnboarding(acquireRepository, me);
RoomServiceUtils.deleteNotificationByOnboarding(notificationRepository, me);

userRepository.delete(user);
onboardingRepository.delete(me);
settingRepository.delete(setting);
}
}

0 comments on commit 0a19a51

Please sign in to comment.