Skip to content

Commit

Permalink
[COZY-432] feat: 사용자가 방에 들어가면 다른 요청들 날리기 (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
suuu0719 authored Dec 5, 2024
1 parent 379454a commit b49a851
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 100 deletions.
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); // 방이 가득 찼을 경우 예외 처리
}

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);
clearOtherRoomRequests(memberId);
} 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 @@ -330,8 +326,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 @@ -374,10 +370,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);
} else {
// 초대 요청을 거절하여 PENDING 상태를 삭제
mateRepository.delete(invitee);
Expand Down Expand Up @@ -551,15 +545,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 @@ -634,6 +626,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();
}

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

// 초대코드 생성 부분
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

0 comments on commit b49a851

Please sign in to comment.