Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[COZY-432] feat: 사용자가 방에 들어가면 다른 요청들 날리기 #209

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ public interface MateRepository extends JpaRepository<Mate, Long> {
Optional<Mate> findByRoomIdAndMemberIdAndNotEntryStatus(@Param("roomId") Long roomId,
@Param("memberId") Long memberId, @Param("entryStatus") EntryStatus entryStatus);


@Query("SELECT COUNT(m) FROM Mate m WHERE m.room.id = :roomId AND m.entryStatus = 'JOINED'")
long countActiveMatesByRoomId(@Param("roomId") Long roomId);

Long countByRoomId(Long roomId);

@Query("SELECT COUNT(m) > 0 FROM Mate m WHERE m.member.id = :memberId AND (m.room.status = :status1 OR m.room.status = :status2)")
boolean existsByMemberIdAndRoomStatuses(@Param("memberId") Long memberId,
@Param("status1") RoomStatus status1, @Param("status2") RoomStatus status2);
Expand All @@ -42,8 +36,6 @@ Optional<Mate> findByRoomIdAndMemberIdAndEntryStatus(Long roomId, Long memberId,

boolean existsByRoomIdAndMemberIdAndEntryStatus(Long roomId, Long memberId, EntryStatus status);

Optional<Mate> findByMemberIdAndEntryStatus(Long memberId, EntryStatus entryStatus);

List<Mate> findAllByMemberIdAndEntryStatus(Long memberId, EntryStatus entryStatus);

boolean existsByMemberIdAndEntryStatusAndRoomStatusIn(Long memberId, EntryStatus entryStatus,
Expand All @@ -66,8 +58,6 @@ List<Mate> findByMemberIdAndEntryStatusInAndRoomStatusIn(Long memberId,
List<Mate> findAllByMemberBirthDayMonthAndDayAndEntryStatus(@Param("month") int month,
@Param("day") int day, @Param("entryStatus") EntryStatus entryStatus);

List<Mate> findByRoom(Room room);

@Query("select m from Mate m join fetch m.member where m.room = :room and m.entryStatus = :entryStatus")
List<Mate> findFetchMemberByRoom(@Param("room") Room room,
@Param("entryStatus") EntryStatus entryStatus);
Expand All @@ -80,8 +70,6 @@ List<Mate> findFetchMemberByRoom(@Param("room") Room room,
@Query("select m.id from Mate m where m.member.id in :memberIds")
Set<Long> findMateIdsByMemberIds(@Param("memberIds") Set<Long> memberIds);

void deleteByRoomIdAndMemberId(Long roomId, Long memberId);

List<Mate> findByRoomIdAndEntryStatus(Long roomId, EntryStatus entryStatus);

Integer countByMemberIdAndEntryStatus(Long memberId, EntryStatus entryStatus);
Expand All @@ -97,4 +85,6 @@ Optional<Mate> findFetchByRoomAndIsRoomManager(@Param("room") Room room,
void deleteAllByMemberId(Long memberId);

List<Mate> findAllByIdIn(List<Long> mateIds);

void deleteAllByMemberIdAndEntryStatusIn(Long memberId, List<EntryStatus> entryStatuses);
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,21 @@ public void joinRoom(Long roomId, Long memberId) {
throw new GeneralException(ErrorStatus._ROOM_ALREADY_EXISTS);
}

if (mateRepository.countActiveMatesByRoomId(roomId) >= room.getMaxMateNum()) {
throw new GeneralException(ErrorStatus._ROOM_FULL);
if (room.getNumOfArrival() >= room.getMaxMateNum()) {
throw new GeneralException(ErrorStatus._ROOM_FULL); // 방이 가득 찼을 경우 예외 처리
Comment on lines +138 to +139
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

방 정원 검사를 중구난방으로 하고 있어서 통일했습니다 (무시해도 됨)

}

if (existingMate.isPresent()) {
// 재입장 처리
Mate exitingMate = existingMate.get();
exitingMate.setEntryStatus(EntryStatus.JOINED);
mateRepository.save(exitingMate);
room.arrive();
room.isRoomFull();
roomRepository.save(room);
processJoinRequest(existingMate.get(), room);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

재입장하는 경우(status가 EXITED)에 대해 processJoinRequest를 호출합니다

clearOtherRoomRequests(memberId);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JOINED됐으면 기존 요청들을 날립니다

} else {
Mate mate = MateConverter.toEntity(room, member, false);
mateRepository.save(mate);
room.arrive();
room.isRoomFull();
roomRepository.save(room);
}
roomRepository.save(room);

// 푸시 알림 코드
List<Mate> findRoomMates = mateRepository.findFetchMemberByRoom(room, EntryStatus.JOINED);
Expand Down Expand Up @@ -326,8 +322,8 @@ public void sendInvitation(Long inviteeId, Long inviterId) {
}

// 방 정원 검사
if (mateRepository.countActiveMatesByRoomId(room.getId()) >= room.getMaxMateNum()) {
throw new GeneralException(ErrorStatus._ROOM_FULL);
if (room.getNumOfArrival() >= room.getMaxMateNum()) {
throw new GeneralException(ErrorStatus._ROOM_FULL); // 방이 가득 찼을 경우 예외 처리
}

if (invitee.isPresent()) {
Expand Down Expand Up @@ -369,10 +365,8 @@ public void respondToInvitation(Long roomId, Long inviteeId, boolean accept) {

if (accept) {
// 초대 요청을 수락하여 JOINED 상태로 변경
invitee.setEntryStatus(EntryStatus.JOINED);
mateRepository.save(invitee);
room.arrive();
room.isRoomFull();
processJoinRequest(invitee, room);
clearOtherRoomRequests(inviteeId);
Comment on lines +368 to +369
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위랑 동일

} else {
// 초대 요청을 거절하여 PENDING 상태를 삭제
mateRepository.delete(invitee);
Expand Down Expand Up @@ -546,15 +540,13 @@ public void respondToJoinRequest(Long requesterId, boolean accept, Long managerI
requesterId, EntryStatus.PENDING)
.orElseThrow(() -> new GeneralException(ErrorStatus._REQUEST_NOT_FOUND));

if (room.getNumOfArrival() + 1 > room.getMaxMateNum()) {
throw new GeneralException(ErrorStatus._ROOM_FULL);
if (room.getNumOfArrival() >= room.getMaxMateNum()) {
throw new GeneralException(ErrorStatus._ROOM_FULL); // 방이 가득 찼을 경우 예외 처리
}

if (accept) {
requester.setEntryStatus(EntryStatus.JOINED);
mateRepository.save(requester);
room.arrive();
room.isRoomFull();
processJoinRequest(requester, room);
clearOtherRoomRequests(requesterId);
} else {
mateRepository.delete(requester); // 거절 시 요청자 삭제
}
Expand Down Expand Up @@ -628,6 +620,19 @@ public void changeToPrivateRoom(Long roomId, Long memberId) {
room.changeToPrivateRoom();
}

private void processJoinRequest(Mate mate, Room room) {
mate.setEntryStatus(EntryStatus.JOINED);
mateRepository.save(mate);
room.arrive();
room.isRoomFull();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

방 참여시 JOINED STATE로 업데이트

private void clearOtherRoomRequests(Long memberId) {
mateRepository.deleteAllByMemberIdAndEntryStatusIn(
memberId, List.of(EntryStatus.PENDING, EntryStatus.INVITED)
);
}

Comment on lines +630 to +635
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PENDING, INVITED 상태인 mate를 모두 삭제합니다

// 초대코드 생성 부분
private String generateUniqueUppercaseKey() {
String randomKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public class RoomQueryService {
public RoomDetailResponseDTO getRoomById(Long roomId, Long memberId) {

memberRepository.findById(memberId).orElseThrow(
() -> new GeneralException(ErrorStatus._MEMBER_NOT_FOUND)
);
() -> new GeneralException(ErrorStatus._MEMBER_NOT_FOUND));
Room room = roomRepository.findById(roomId)
.orElseThrow(() -> new GeneralException(ErrorStatus._ROOM_NOT_FOUND));

Expand Down Expand Up @@ -137,19 +136,10 @@ public RoomIdResponseDTO getExistRoom(Long memberId) {
}

public RoomIdResponseDTO getExistRoom(Long otherMemberId, Long memberId) {
memberRepository.findById(otherMemberId).orElseThrow(
() -> new GeneralException(ErrorStatus._MEMBER_NOT_FOUND));

memberRepository.findById(memberId).orElseThrow(
() -> new GeneralException(ErrorStatus._MEMBER_NOT_FOUND));
Optional<Mate> mate = mateRepository.findByMemberIdAndEntryStatusAndRoomStatusIn(
otherMemberId, EntryStatus.JOINED, List.of(RoomStatus.ENABLE, RoomStatus.WAITING));
if (mate.isPresent()) {
return RoomConverter.toRoomExistResponse(mate.get().getRoom());
}

return RoomConverter.toRoomExistResponse(null);

return getExistRoom(otherMemberId);
}

public Integer getCalculateRoomEquality(Map<Long, Integer> equalityMap){
Expand All @@ -172,14 +162,7 @@ public List<MateDetailResponseDTO> getInvitedMemberList(Long roomId, Long member

List<Mate> invitedMates = mateRepository.findByRoomIdAndEntryStatus(room.getId(), EntryStatus.INVITED);

Map<Long, Integer> equalityMap = memberStatEqualityQueryService.getEquality(memberId,
invitedMates.stream().map(mate -> mate.getMember().getId()).collect(Collectors.toList()));

return invitedMates.stream()
.map(mate -> {
Integer mateEquality = equalityMap.get(mate.getMember().getId());
return RoomConverter.toMateDetailListResponse(mate, mateEquality);
}).toList();
return getMateDetailResponseDTOS(memberId, invitedMates);

}

Expand All @@ -197,26 +180,31 @@ public List<MateDetailResponseDTO> getPendingMemberList(Long managerId) {

List<Mate> pendingMates = mateRepository.findByRoomIdAndEntryStatus(room.getId(), EntryStatus.PENDING);

return getMateDetailResponseDTOS(managerId, pendingMates);
}

private List<MateDetailResponseDTO> getMateDetailResponseDTOS(Long managerId,
List<Mate> Mates) {
Map<Long, Integer> equalityMap = memberStatEqualityQueryService.getEquality(managerId,
pendingMates.stream().map(mate -> mate.getMember().getId()).collect(Collectors.toList()));
Mates.stream().map(mate -> mate.getMember().getId()).collect(Collectors.toList()));

return pendingMates.stream()
return Mates.stream()
.map(mate -> {
Integer mateEquality = equalityMap.get(mate.getMember().getId());
return RoomConverter.toMateDetailListResponse(mate, mateEquality);
}).toList();
}

public List<RoomDetailResponseDTO> getRequestedRoomList(Long memberId) {
memberRepository.findById(memberId).orElseThrow(
() -> new GeneralException(ErrorStatus._MEMBER_NOT_FOUND));

List<Room> requestedRooms = mateRepository.findAllByMemberIdAndEntryStatus(memberId, EntryStatus.PENDING)
public List<RoomDetailResponseDTO> getRoomList(Long memberId, EntryStatus entryStatus) {
// 해당 memberId와 entryStatus에 맞는 방을 가져옴
List<Room> rooms = mateRepository.findAllByMemberIdAndEntryStatus(memberId, entryStatus)
.stream()
.map(Mate::getRoom)
.toList();

return requestedRooms.stream()
// 방 정보를 RoomDetailResponseDTO로 변환하여 반환
return rooms.stream()
.map(room -> {
List<Mate> joinedMates = mateRepository.findAllByRoomIdAndEntryStatus(room.getId(), EntryStatus.JOINED);
Map<Long, Integer> equalityMap = memberStatEqualityQueryService.getEquality(memberId,
Expand Down Expand Up @@ -250,51 +238,20 @@ public List<RoomDetailResponseDTO> getRequestedRoomList(Long memberId) {
.toList();
}

public InvitedRoomResponseDTO getInvitedRoomList(Long memberId) {

public List<RoomDetailResponseDTO> getRequestedRoomList(Long memberId) {
memberRepository.findById(memberId).orElseThrow(
() -> new GeneralException(ErrorStatus._MEMBER_NOT_FOUND));

Integer invitedCount = mateRepository.countByMemberIdAndEntryStatus(memberId, EntryStatus.INVITED);

List<Room> invitedRooms = mateRepository.findAllByMemberIdAndEntryStatus(memberId, EntryStatus.INVITED)
.stream()
.map(Mate::getRoom)
.toList();
return getRoomList(memberId, EntryStatus.PENDING);
}

List<RoomDetailResponseDTO> rooms = invitedRooms.stream()
.map(room -> {
List<Mate> joinedMates = mateRepository.findAllByRoomIdAndEntryStatus(room.getId(), EntryStatus.JOINED);
Map<Long, Integer> equalityMap = memberStatEqualityQueryService.getEquality(memberId,
joinedMates.stream().map(mate -> mate.getMember().getId()).collect(Collectors.toList()));
Integer roomEquality = getCalculateRoomEquality(equalityMap);
List<String> hashtags = roomHashtagRepository.findHashtagsByRoomId(room.getId());
return RoomConverter.toRoomDetailResponseDTOWithParams(
room.getId(),
room.getName(),
room.getInviteCode(),
room.getProfileImage(),
joinedMates.stream()
.map(mate -> RoomConverter.toMateDetailListResponse(mate, equalityMap.get(mate.getMember().getId())))
.toList(),
getManagerMemberId(room),
getManagerNickname(room),
false,
isFavoritedRoom(memberId, room.getId()),
room.getMaxMateNum(),
room.getNumOfArrival(),
getDormitoryName(room),
room.getRoomType().toString(),
hashtags,
roomEquality,
MemberStatConverter.toMemberStatDifferenceResponseDTO(joinedMates.stream()
.map(mate -> memberStatRepository.findByMemberId(mate.getMember().getId()))
.flatMap(Optional::stream)
.toList())
);
})
.toList();
public InvitedRoomResponseDTO getInvitedRoomList(Long memberId) {
memberRepository.findById(memberId).orElseThrow(
() -> new GeneralException(ErrorStatus._MEMBER_NOT_FOUND));

return new InvitedRoomResponseDTO(invitedCount, rooms);
Integer invitedCount = mateRepository.countByMemberIdAndEntryStatus(memberId, EntryStatus.INVITED);
return new InvitedRoomResponseDTO(invitedCount, getRoomList(memberId, EntryStatus.INVITED));
}

public Long getManagerMemberId(Room room) {
Expand Down
Loading