-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
채팅방 정보 가져오기 API 구현
- Loading branch information
Showing
35 changed files
with
1,408 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
backend/src/main/java/mouda/backend/chat/business/ChatRoomService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package mouda.backend.chat.business; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mouda.backend.chat.domain.ChatPreview; | ||
import mouda.backend.chat.domain.ChatRoomDetails; | ||
import mouda.backend.chat.domain.ChatRoomType; | ||
import mouda.backend.chat.implement.ChatPreviewManager; | ||
import mouda.backend.chat.implement.ChatPreviewManagerRegistry; | ||
import mouda.backend.chat.implement.ChatRoomDetailsFinder; | ||
import mouda.backend.chat.implement.ChatRoomWriter; | ||
import mouda.backend.chat.presentation.response.ChatPreviewResponses; | ||
import mouda.backend.chat.presentation.response.ChatRoomDetailsResponse; | ||
import mouda.backend.darakbangmember.domain.DarakbangMember; | ||
import mouda.backend.moim.domain.Moim; | ||
import mouda.backend.moim.implement.finder.MoimFinder; | ||
import mouda.backend.moim.implement.writer.MoimWriter; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class ChatRoomService { | ||
|
||
private final ChatRoomDetailsFinder chatRoomDetailsFinder; | ||
private final ChatPreviewManagerRegistry chatPreviewManagerRegistry; | ||
private final ChatRoomWriter chatRoomWriter; | ||
private final MoimFinder moimFinder; | ||
private final MoimWriter moimWriter; | ||
|
||
@Transactional(readOnly = true) | ||
public ChatRoomDetailsResponse findChatRoomDetails(long darakbangId, long chatRoomId, DarakbangMember darakbangMember) { | ||
ChatRoomDetails chatRoomDetails = chatRoomDetailsFinder.find(darakbangId, chatRoomId, darakbangMember); | ||
|
||
return ChatRoomDetailsResponse.from(chatRoomDetails); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public ChatPreviewResponses findChatPreview(DarakbangMember darakbangMember, ChatRoomType chatRoomType) { | ||
ChatPreviewManager manager = chatPreviewManagerRegistry.getManager(chatRoomType); | ||
List<ChatPreview> chatPreviews = manager.create(darakbangMember); | ||
|
||
return ChatPreviewResponses.toResponse(chatPreviews); | ||
} | ||
|
||
public void openChatRoom(Long darakbangId, Long moimId, DarakbangMember darakbangMember) { | ||
Moim moim = moimFinder.read(moimId, darakbangId); | ||
moimWriter.openChatByMoimer(moim, darakbangMember); | ||
chatRoomWriter.append(moimId, darakbangId, ChatRoomType.MOIM); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/mouda/backend/chat/domain/Attributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mouda.backend.chat.domain; | ||
|
||
import java.util.Map; | ||
|
||
public interface Attributes { | ||
Map<String, Object> getAttributes(); | ||
} |
36 changes: 36 additions & 0 deletions
36
backend/src/main/java/mouda/backend/chat/domain/BetAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package mouda.backend.chat.domain; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class BetAttributes implements Attributes { | ||
|
||
private final String title; | ||
private final boolean isLoser; | ||
private final long betId; | ||
private final Participant loser; | ||
|
||
public BetAttributes(String title, Boolean isLoser, Long betId, Participant loser) { | ||
this.title = title; | ||
this.isLoser = isLoser; | ||
this.betId = betId; | ||
this.loser = loser; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getAttributes() { | ||
Map<String, Object> attributes = new HashMap<>(); | ||
attributes.put("title", title); | ||
attributes.put("isLoser", isLoser); | ||
attributes.put("betId", betId); | ||
attributes.put("loser", Map.of( | ||
"nickname", loser.getNickname(), | ||
"profile", loser.getProfile(), | ||
"role", loser.getRole() | ||
)); | ||
return attributes; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
backend/src/main/java/mouda/backend/chat/domain/ChatRoomDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package mouda.backend.chat.domain; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ChatRoomDetails { | ||
private final long id; | ||
private final ChatRoomType chatRoomType; | ||
private final Attributes attributes; | ||
private final List<Participant> participants; | ||
|
||
public ChatRoomDetails(long id, ChatRoomType chatRoomType, Attributes attributes, List<Participant> participants) { | ||
this.id = id; | ||
this.chatRoomType = chatRoomType; | ||
this.attributes = attributes; | ||
this.participants = participants; | ||
} | ||
|
||
public String getTitle() { | ||
return (String)getAttributes().get("title"); | ||
} | ||
|
||
public Map<String, Object> getAttributes() { | ||
return attributes.getAttributes(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
backend/src/main/java/mouda/backend/chat/domain/MoimAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package mouda.backend.chat.domain; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class MoimAttributes implements Attributes { | ||
|
||
private final String title; | ||
private final String place; | ||
private final Boolean isMoimer; | ||
private final Boolean isStarted; | ||
private final String description; | ||
private final String date; | ||
private final String time; | ||
private final Long moimId; | ||
|
||
public MoimAttributes(String title, String place, Boolean isMoimer, Boolean isStarted, String description, String date, String time, Long moimId) { | ||
this.title = title; | ||
this.place = place; | ||
this.isMoimer = isMoimer; | ||
this.isStarted = isStarted; | ||
this.description = description; | ||
this.date = date; | ||
this.time = time; | ||
this.moimId = moimId; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getAttributes() { | ||
Map<String, Object> attributes = new HashMap<>(); | ||
attributes.put("title", title); | ||
attributes.put("place", place); | ||
attributes.put("isMoimer", isMoimer); | ||
attributes.put("isStarted", isStarted); | ||
attributes.put("description", description); | ||
attributes.put("date", date); | ||
attributes.put("time", time); | ||
attributes.put("moimId", moimId); | ||
return attributes; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
backend/src/main/java/mouda/backend/chat/domain/Participant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package mouda.backend.chat.domain; | ||
|
||
import java.util.Objects; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class Participant { | ||
|
||
private final String nickname; | ||
private final String profile; | ||
private final String role; | ||
|
||
public Participant(String nickname, String profile, String role) { | ||
this.nickname = nickname; | ||
this.profile = profile; | ||
this.role = role; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
Participant that = (Participant)o; | ||
return Objects.equals(nickname, that.nickname) && Objects.equals(profile, that.profile) && Objects.equals(role, that.role); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(nickname, profile, role); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
backend/src/main/java/mouda/backend/chat/implement/AttributeManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mouda.backend.chat.implement; | ||
|
||
import mouda.backend.chat.domain.Attributes; | ||
import mouda.backend.chat.domain.ChatRoom; | ||
import mouda.backend.chat.domain.ChatRoomType; | ||
import mouda.backend.darakbangmember.domain.DarakbangMember; | ||
|
||
public interface AttributeManager { | ||
|
||
boolean support(ChatRoomType chatRoomType); | ||
|
||
Attributes create(ChatRoom chatRoom, DarakbangMember darakbangMember); | ||
} |
22 changes: 22 additions & 0 deletions
22
backend/src/main/java/mouda/backend/chat/implement/AttributeManagerRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package mouda.backend.chat.implement; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mouda.backend.chat.domain.ChatRoomType; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class AttributeManagerRegistry { | ||
|
||
private final List<AttributeManager> attributeManagers; | ||
|
||
public AttributeManager getManager(ChatRoomType chatRoomType) { | ||
return attributeManagers.stream() | ||
.filter(attributeManager -> attributeManager.support(chatRoomType)) | ||
.findFirst() | ||
.orElseThrow(IllegalArgumentException::new); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
backend/src/main/java/mouda/backend/chat/implement/BetAttributeManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package mouda.backend.chat.implement; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mouda.backend.bet.domain.Bet; | ||
import mouda.backend.bet.domain.BetRole; | ||
import mouda.backend.bet.implement.BetFinder; | ||
import mouda.backend.bet.infrastructure.BetDarakbangMemberRepository; | ||
import mouda.backend.chat.domain.Attributes; | ||
import mouda.backend.chat.domain.BetAttributes; | ||
import mouda.backend.chat.domain.ChatRoom; | ||
import mouda.backend.chat.domain.ChatRoomType; | ||
import mouda.backend.chat.domain.Participant; | ||
import mouda.backend.darakbangmember.domain.DarakbangMember; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class BetAttributeManager implements AttributeManager { | ||
|
||
private final BetFinder betFinder; | ||
private final BetDarakbangMemberRepository betDarakbangMemberRepository; | ||
|
||
@Override | ||
public boolean support(ChatRoomType chatRoomType) { | ||
return chatRoomType == ChatRoomType.BET; | ||
} | ||
|
||
@Override | ||
public Attributes create(ChatRoom chatRoom, DarakbangMember darakbangMember) { | ||
Bet bet = betFinder.find(darakbangMember.getDarakbang().getId(), chatRoom.getTargetId()); | ||
boolean isLoser = bet.isLoser(darakbangMember.getId()); | ||
Participant loser = getLoser(bet, darakbangMember.getId()); | ||
return new BetAttributes(bet.getBetDetails().getTitle(), isLoser, bet.getId(), loser); | ||
} | ||
|
||
private Participant getLoser(Bet bet, long requestDarakbangMemberId) { | ||
DarakbangMember darakbangMember = betDarakbangMemberRepository.findByBetIdAndDarakbangMemberId(bet.getId(), bet.getLoserId()) | ||
.orElseThrow(IllegalArgumentException::new).getDarakbangMember(); // TODO: 예외 처리 리팩토링 | ||
BetRole betRole = getBetRole(requestDarakbangMemberId, bet.getMoimerId()); | ||
return new Participant(darakbangMember.getNickname(), darakbangMember.getProfile(), betRole.toString()); | ||
} | ||
|
||
private BetRole getBetRole(long requestDarakbangMemberId, long moimerId) { | ||
return moimerId == requestDarakbangMemberId ? BetRole.MOIMER : BetRole.MOIMEE; | ||
} | ||
} |
Oops, something went wrong.