Skip to content

Commit

Permalink
Merge pull request #262 from junseoparkk/fix/lounge-invite
Browse files Browse the repository at this point in the history
라운지 초대 관련 버그 해결
  • Loading branch information
jjikky authored Oct 1, 2024
2 parents 91fcae5 + 87e35fc commit 86746a0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.daobe.lounge.application;

import static com.example.daobe.lounge.exception.LoungeExceptionType.ALREADY_INVITED_USER_EXCEPTION;
import static com.example.daobe.lounge.exception.LoungeExceptionType.ALREADY_EXISTS_LOUNGE_USER_EXCEPTION;
import static com.example.daobe.lounge.exception.LoungeExceptionType.ALREADY_INVITED_LOUNGE_USER_EXCEPTION;
import static com.example.daobe.lounge.exception.LoungeExceptionType.INVALID_LOUNGE_SHARER_EXCEPTION;
import static com.example.daobe.lounge.exception.LoungeExceptionType.MAXIMUM_LOUNGE_LIMIT_EXCEEDED_EXCEPTION;

Expand Down Expand Up @@ -30,7 +31,7 @@ public class LoungeSharerService {

public void createAndSaveLoungeSharer(User user, Lounge lounge) {
// 라운지 최대 개수를 초과하는지 검증
if (isMaximumCountLounge(user.getId())) {
if (isOverMaximumCountLounge(user.getId())) {
throw new LoungeException(MAXIMUM_LOUNGE_LIMIT_EXCEEDED_EXCEPTION);
}

Expand All @@ -43,7 +44,7 @@ public void createAndSaveLoungeSharer(User user, Lounge lounge) {
}

public void inviteUser(User user, Lounge lounge, Long inviterId) {
validateInvite(user, lounge, inviterId);
validateInvite(user.getId(), lounge, inviterId);
LoungeSharer loungeSharer = LoungeSharer.builder()
.user(user)
.lounge(lounge)
Expand All @@ -52,6 +53,7 @@ public void inviteUser(User user, Lounge lounge, Long inviterId) {
eventPublisher.publishEvent(new LoungeInviteEvent(inviterId, loungeSharer));
}

// FIXME:
public void updateInvitedUserStatus(User invitedUser, Lounge lounge) {
LoungeSharer findSharer = loungeSharerRepository.findByUserIdAndLoungeId(invitedUser.getId(), lounge.getId());
if (!findSharer.isActive()) {
Expand Down Expand Up @@ -81,15 +83,16 @@ public void validateLoungeSharer(Long userId, Long loungeId) {
}

// TODO: 추후 도메인 로직으로 분리
private void validateInvite(User user, Lounge lounge, Long inviterId) {
private void validateInvite(Long userId, Lounge lounge, Long inviterId) {
lounge.isActiveOrThrow();
validateLoungeSharer(inviterId, lounge.getId());
validateAlreadyInvited(userId, lounge.getId());

if (isExistUserInLounge(user.getId(), lounge.getId())) {
throw new LoungeException(ALREADY_INVITED_USER_EXCEPTION);
if (isExistUserInLounge(userId, lounge.getId())) {
throw new LoungeException(ALREADY_EXISTS_LOUNGE_USER_EXCEPTION);
}

if (isMaximumCountLounge(user.getId())) {
if (isOverMaximumCountLounge(userId)) {
throw new LoungeException(MAXIMUM_LOUNGE_LIMIT_EXCEEDED_EXCEPTION);
}
}
Expand All @@ -98,7 +101,14 @@ private boolean isExistUserInLounge(Long userId, Long loungeId) {
return loungeSharerRepository.existsActiveLoungeSharerByUserIdAndLoungeId(userId, loungeId);
}

private boolean isMaximumCountLounge(Long userId) {
return loungeSharerRepository.countActiveLoungeSharerByUserId(userId) == MAX_LOUNGE_COUNT;
private boolean isOverMaximumCountLounge(Long userId) {
return loungeSharerRepository.countActiveLoungeSharerByUserId(userId) >= MAX_LOUNGE_COUNT;
}
}

private void validateAlreadyInvited(Long userId, Long loungeId) {
boolean isAlreadyInvited = loungeSharerRepository.existsLoungeSharerByUserIdAndLoungeId(userId, loungeId);
if (isAlreadyInvited) {
throw new LoungeException(ALREADY_INVITED_LOUNGE_USER_EXCEPTION);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,23 @@ SELECT CASE WHEN EXISTS (
""")
boolean existsActiveLoungeSharerByUserIdAndLoungeId(@Param("userId") Long userId, @Param("loungeId") Long loungeId);

@Query("""
SELECT CASE WHEN EXISTS (
SELECT 1
FROM LoungeSharer ls
WHERE ls.user.id = :userId
AND ls.lounge.id = :loungeId
) THEN true ELSE false END
""")
boolean existsLoungeSharerByUserIdAndLoungeId(@Param("userId") Long userId, @Param("loungeId") Long loungeId);

List<LoungeSharer> findByLounge_IdAndUser_NicknameContaining(Long loungeId, String nickname);

@Query("""
SELECT COUNT(ls) FROM LoungeSharer ls
JOIN ls.lounge l WHERE l.status = 'ACTIVE'
JOIN ls.lounge l
WHERE l.status = 'ACTIVE'
AND ls.status = 'ACTIVE'
AND ls.user.id = :userId
""")
long countActiveLoungeSharerByUserId(@Param("userId") Long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public enum LoungeExceptionType implements BaseExceptionType {
NOT_ACTIVE_LOUNGE_EXCEPTION("활성 상태가 아닌 라운지입니다.", HttpStatus.NOT_FOUND),
INVALID_LOUNGE_OWNER_EXCEPTION("유효하지 않은 라운지 주인입니다.", HttpStatus.FORBIDDEN),
INVALID_LOUNGE_SHARER_EXCEPTION("유효하지 않은 라운지 공유자입니다.", HttpStatus.FORBIDDEN),
ALREADY_INVITED_USER_EXCEPTION("이미 라운지에 소속된 유저입니다.", HttpStatus.BAD_REQUEST),
ALREADY_EXISTS_LOUNGE_USER_EXCEPTION("이미 라운지에 소속된 유저입니다.", HttpStatus.CONFLICT),
ALREADY_INVITED_LOUNGE_USER_EXCEPTION("이미 라운지에 초대된 유저입니다.", HttpStatus.CONFLICT),
ALREADY_DELETED_LOUNGE_EXCEPTION("이미 삭제된 라운지입니다.", HttpStatus.NOT_FOUND),
MAXIMUM_LOUNGE_LIMIT_EXCEEDED_EXCEPTION("라운지 개수는 최대 4개를 초과할 수 없습니다.", HttpStatus.BAD_REQUEST),
NOT_ALLOW_LOUNGE_WITHDRAW_EXCEPTION("라운지 생성자는 탈퇴할 수 없습니다.", HttpStatus.BAD_REQUEST);
Expand Down

0 comments on commit 86746a0

Please sign in to comment.