Skip to content

Commit

Permalink
Merge pull request #45 from Domitory-CheckMate/feature/42-member
Browse files Browse the repository at this point in the history
[feat] 회원탈퇴 기능
  • Loading branch information
OJOJIN authored Jan 13, 2024
2 parents a524d0c + 1782ddf commit a9be718
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 22 deletions.
22 changes: 11 additions & 11 deletions src/main/java/org/gachon/checkmate/domain/chat/entity/ChatRoom.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.gachon.checkmate.domain.chat.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.*;
import lombok.*;
import org.gachon.checkmate.domain.member.entity.User;
import org.gachon.checkmate.global.common.BaseTimeEntity;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -26,17 +24,19 @@ public class ChatRoom extends BaseTimeEntity {
@Column(name = "chat_room_id", length = 100)
private String id;

@Column(name = "first_member_id")
private Long firstMemberId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "first_member_id")
private User firstUser;

@Column(name = "second_member_id")
private Long secondMemberId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "second_member_id")
private User secondUser;

public static ChatRoom createChatRoom(String id, Long firstMemberId, Long secondMemberId) {
public static ChatRoom createChatRoom(String id, User firstUser, User secondUser) {
return ChatRoom.builder()
.id(id)
.firstMemberId(firstMemberId)
.secondMemberId(secondMemberId)
.firstUser(firstUser)
.secondUser(secondUser)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.gachon.checkmate.domain.chat.repository;

import org.gachon.checkmate.domain.chat.entity.ChatRoom;
import org.gachon.checkmate.domain.member.entity.User;

import java.util.List;

public interface ChatRoomCustomRepository {

List<ChatRoom> findUserAllChatRoom(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.gachon.checkmate.domain.chat.repository;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.gachon.checkmate.domain.chat.entity.ChatRoom;
import org.gachon.checkmate.domain.member.entity.User;
import org.gachon.checkmate.domain.member.entity.UserState;

import java.util.List;

import static org.gachon.checkmate.domain.chat.entity.QChatRoom.chatRoom;

@RequiredArgsConstructor
public class ChatRoomCustomRepositoryImpl implements ChatRoomCustomRepository {

private final JPAQueryFactory queryFactory;

@Override
public List<ChatRoom> findUserAllChatRoom(Long userId) {
return queryFactory
.selectFrom(chatRoom)
.where(
userEnterChatRoom(userId),
validateUsersState()
)
.fetch();
}

private BooleanExpression userEnterChatRoom(Long userId) {
return chatRoom.firstUser.id.eq(userId)
.or(chatRoom.secondUser.id.eq(userId));
}

private BooleanExpression validateUsersState() {
return chatRoom.firstUser.userState.eq(UserState.JOIN)
.and(chatRoom.secondUser.userState.eq(UserState.JOIN));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@

import java.util.List;

public interface ChatRoomRepository extends JpaRepository<ChatRoom, String> {
List<ChatRoom> findAllByFirstMemberIdOrSecondMemberId(Long firstMemberId, Long secondMemberId);
public interface ChatRoomRepository extends JpaRepository<ChatRoom, String>, ChatRoomCustomRepository {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
import org.gachon.checkmate.domain.chat.repository.ChatRoomRepository;
import org.gachon.checkmate.domain.chat.repository.LiveChatRoomRepository;
import org.gachon.checkmate.domain.chat.util.ListComparatorChatSendTime;
import org.gachon.checkmate.domain.member.entity.User;
import org.gachon.checkmate.domain.member.repository.UserQuerydslRepository;
import org.gachon.checkmate.domain.member.repository.UserRepository;
import org.gachon.checkmate.global.error.ErrorCode;
import org.gachon.checkmate.global.error.exception.EntityNotFoundException;
import org.gachon.checkmate.global.socket.error.SocketErrorCode;
import org.gachon.checkmate.global.socket.error.SocketNotFoundException;
import org.springframework.data.domain.PageRequest;
Expand All @@ -25,7 +29,6 @@
import java.util.Map;
import java.util.stream.Collectors;

import static org.gachon.checkmate.domain.chat.entity.ChatRoom.createChatRoom;
import static org.gachon.checkmate.domain.chat.entity.LiveChatRoom.createLiveChatRoom;

@Service
Expand All @@ -34,6 +37,7 @@
public class ChatService {

private final SimpMessageSendingOperations sendingOperations;
private final UserRepository userRepository;
private final ChatRepository chatRepository;
private final ChatRoomRepository chatRoomRepository;
private final LiveChatRoomRepository liveChatRoomRepository;
Expand Down Expand Up @@ -124,7 +128,7 @@ public ChatRoomEnterResponseDto enterChatRoom(Map<String, Object> simpSessionAtt
chatRepository.updateChatRead(chatRoomId, userId);
}
else {
ChatRoom chatRoom = createChatRoom(chatRoomId, userId, request.otherUserId());
ChatRoom chatRoom = createChatRoomByIds(chatRoomId, userId, request.otherUserId());
chatRoomRepository.save(chatRoom);
}
return ChatRoomEnterResponseDto.of(userId, chatRoomId);
Expand All @@ -142,6 +146,12 @@ public ChatTotalNotReadResponseDto getTotalNotReadCount(Map<String, Object> simp
return ChatTotalNotReadResponseDto.of(userTotalNotReadCount);
}

private ChatRoom createChatRoomByIds(String chatRoomId, Long firstUserId, Long secondUserId) {
User firstUser = getUserOrThrow(firstUserId);
User secondUser = getUserOrThrow(secondUserId);
return ChatRoom.createChatRoom(chatRoomId, firstUser, secondUser);
}

private void sendNotificationToOtherUser(Boolean isOtherUserInChatRoom, Chat savedChat, Long otherUserId) {
if(!isOtherUserInChatRoom) {
NewChatResponseDto notificationChat = NewChatResponseDto.of(savedChat);
Expand Down Expand Up @@ -185,11 +195,11 @@ private ChatLastMessageDto getLastChatRoomContent(ChatRoom chatRoom) {
}

private Long getChatRoomOtherUserId(ChatRoom chatRoom, Long userId) {
return chatRoom.getFirstMemberId().equals(userId) ? chatRoom.getSecondMemberId() : chatRoom.getFirstMemberId();
return chatRoom.getFirstUser().getId().equals(userId) ? chatRoom.getSecondUser().getId() : chatRoom.getFirstUser().getId();
}

private List<ChatRoom> getUserChatRoomsByUserId(Long userId) {
return chatRoomRepository.findAllByFirstMemberIdOrSecondMemberId(userId, userId);
return chatRoomRepository.findUserAllChatRoom(userId);
}

private ChatUserInfoDto getUserChatUserInfoByUserId(ChatListRequestDto request) {
Expand All @@ -211,7 +221,7 @@ private ChatRoom getChatRoomById(String roomId) {
}

private Long getOtherUserIdInChatRoom(ChatRoom chatRoom, Long myUserId) {
return chatRoom.getFirstMemberId().equals(myUserId) ? chatRoom.getSecondMemberId() : chatRoom.getFirstMemberId();
return chatRoom.getFirstUser().getId().equals(myUserId) ? chatRoom.getSecondUser().getId() : chatRoom.getFirstUser().getId();
}

private boolean validateChatRoomExist(String chatRoomId) {
Expand All @@ -230,4 +240,9 @@ private String getRoomIdInAttributes(Map<String, Object> simpSessionAttributes)
return (String)simpSessionAttributes.get("roomId");
}

private User getUserOrThrow(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ public void updateCheckList(CheckListRequestDto checkListRequestDto) {
this.lifePatternType = checkListRequestDto.lifePatternType();
this.noiseType = checkListRequestDto.noiseType();
this.sleepType = checkListRequestDto.sleepType();
this.smokeType = checkListRequestDto.smokeType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public ResponseEntity<SuccessResponse<?>> signIn(@RequestBody final MemberSignIn
return SuccessResponse.ok(memberSignInResponseDto);
}

@PatchMapping("/withdraw")
public ResponseEntity<SuccessResponse<?>> withdraw(@UserId final Long userId) {
memberService.withdraw(userId);
return SuccessResponse.ok(null);
}

@PatchMapping("/reset")
public ResponseEntity<SuccessResponse<?>> setPassword(@RequestBody final PasswordResetRequestDto passwordResetRequestDto){
memberService.setPassword(passwordResetRequestDto);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/gachon/checkmate/domain/member/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ public static User createUser(String email, String storedPassword, String name,
.build();
}

public void withdrawUser() {
this.email = null;
this.password = null;
this.name = null;
this.profile = null;
this.school = null;
this.major = null;
this.userState = UserState.WITHDRAW;
this.mbtiType = null;
this.gender = null;
this.checkList = null;
}

public void setPassword(String newPassword) {
this.password = newPassword;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public MemberSignInResponseDto signIn(MemberSignInRequestDto memberSignInRequest
return MemberSignInResponseDto.of(user.getId(), accessToken, refreshToken);
}

public void withdraw(Long userId) {
User user = findByIdOrThrow(userId);
deleteRefreshToken(user);
user.withdrawUser();
}

public void setPassword(PasswordResetRequestDto passwordResetRequestDto) {
User user = getUserFromEmail(passwordResetRequestDto.email());
user.setPassword(encodedPassword(passwordResetRequestDto.newPassword()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import lombok.*;
import org.gachon.checkmate.domain.chat.entity.ChatRoom;
import org.gachon.checkmate.domain.member.entity.User;
import org.gachon.checkmate.domain.post.entity.Post;
import org.gachon.checkmate.domain.scrap.entity.Scrap;
import org.gachon.checkmate.global.common.BaseTimeEntity;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public void reportChatRoom(Long userId, ChatRoomReportRequestDto requestDto) {
}

private void validateUserChatRoomMember(User user, ChatRoom chatRoom) {
if(!chatRoom.getFirstMemberId().equals(user.getId()) &&
!chatRoom.getSecondMemberId().equals(user.getId())) {
if(!chatRoom.getFirstUser().getId().equals(user.getId()) &&
!chatRoom.getSecondUser().getId().equals(user.getId())) {
throw new ForbiddenException(NOT_CHATROOM_USER);
}
}
Expand Down

0 comments on commit a9be718

Please sign in to comment.