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

채팅방 정보 가져오기 API 구현 #636

Merged
merged 27 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
535669d
feat: 채팅방 타입별 속성을 반환하기 위한 추상화 구현레이어 클래스
ksk0605 Oct 11, 2024
de14419
feat: BetAttributeManager 구현
ksk0605 Oct 11, 2024
e6c8c39
feat: MoimAttributeManager 구현
ksk0605 Oct 11, 2024
e46dd3a
feat: ParticipantResolver, ParticipantResolverRegistry 구현
ksk0605 Oct 11, 2024
ed25b6b
test: DisplayName 추가
ksk0605 Oct 11, 2024
f6f0b48
test: darakbangmember nickname 주입방식 수정
ksk0605 Oct 11, 2024
ccce1a5
feat: MoimParticipantResolver 구현
ksk0605 Oct 11, 2024
b9d75bf
feat: BetParticipantResolver 구현
ksk0605 Oct 11, 2024
b8edfe3
feat: attributes 구현체에 title 필드 추가, 구현체 별 getAttributes 구현
ksk0605 Oct 11, 2024
05d1202
feat: ChatRoomDetails, ChatRoomDetailsFinder 구현
ksk0605 Oct 11, 2024
3384bff
feat: ChatRoomService 구현
ksk0605 Oct 11, 2024
0d3a652
feat: ChatRoomDetailsResponse 구현
ksk0605 Oct 11, 2024
f6099f9
refactor: findChatRoomDetails 메서드 시그니처에 darakbangId 추가, Transactional 설정
ksk0605 Oct 12, 2024
923e4b3
refactor: 참여자 응답 json key 이름 변경
ksk0605 Oct 12, 2024
ea6da3a
feat: 모임 시간 나노초 제거 포매팅 기능 추가
ksk0605 Oct 12, 2024
8157a78
feat: equals & hashcode 재정의
ksk0605 Oct 12, 2024
871d3cc
feat: ChatPreviewResponses json 응답 key 이름 변경
ksk0605 Oct 12, 2024
5eb1a17
refactor: ChatRoomDetailsFinder Transactional, 테스트 코드 추가
ksk0605 Oct 12, 2024
8b559e1
fix: ChatPreviewResponse 변경에 따른 오류 수정
ksk0605 Oct 12, 2024
882b66b
feat: ChatRoomController 구현
ksk0605 Oct 12, 2024
3d1cb8e
refactor: 채팅방 프리뷰, 채팅방 열기 기능을 ChatRoom 관련 책임으로 분리
ksk0605 Oct 12, 2024
c880c8a
fix: 테스트 코드 NPE 해결
ksk0605 Oct 12, 2024
6fb693a
test: DisplayName 추가
ksk0605 Oct 12, 2024
528911c
refactor: Loser 도메인을 Participant 도메인을 재사용하도록 변경
ksk0605 Oct 14, 2024
13aad20
refactor: readOnly 옵션 추가
ksk0605 Oct 14, 2024
c171766
refactor: 필드값이 널을 허용하지 않도록 수정
ksk0605 Oct 14, 2024
06c3cc1
merge: 변경사항 반영
ksk0605 Oct 14, 2024
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
4 changes: 4 additions & 0 deletions backend/src/main/java/mouda/backend/bet/domain/Bet.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public long getLoserId() {
return loserId;
}

public boolean isLoser(long otherId) {
return loserId == otherId;
}

public long getId() {
return betDetails.getId();
}
Expand Down
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);
}
}
21 changes: 0 additions & 21 deletions backend/src/main/java/mouda/backend/chat/business/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,16 @@
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;
import mouda.backend.chat.domain.ChatPreview;
import mouda.backend.chat.domain.ChatRoom;
import mouda.backend.chat.domain.ChatRoomType;
import mouda.backend.chat.domain.ChatWithAuthor;
import mouda.backend.chat.domain.Chats;
import mouda.backend.chat.implement.ChatPreviewManager;
import mouda.backend.chat.implement.ChatPreviewManagerRegistry;
import mouda.backend.chat.implement.ChatRoomFinder;
import mouda.backend.chat.implement.ChatRoomWriter;
import mouda.backend.chat.implement.ChatWriter;
import mouda.backend.chat.presentation.request.ChatCreateRequest;
import mouda.backend.chat.presentation.request.DateTimeConfirmRequest;
import mouda.backend.chat.presentation.request.LastReadChatRequest;
import mouda.backend.chat.presentation.request.PlaceConfirmRequest;
import mouda.backend.chat.presentation.response.ChatFindUnloadedResponse;
import mouda.backend.chat.presentation.response.ChatPreviewResponses;
import mouda.backend.darakbangmember.domain.DarakbangMember;
import mouda.backend.moim.domain.Moim;
import mouda.backend.moim.implement.finder.MoimFinder;
Expand All @@ -35,9 +29,7 @@ public class ChatService {
private final ChatRoomFinder chatRoomFinder;
private final ChatWriter chatWriter;
private final MoimWriter moimWriter;
private final ChatPreviewManagerRegistry chatPreviewManagerRegistry;
private final MoimFinder moimFinder;
private final ChatRoomWriter chatRoomWriter;

public void createChat(
long darakbangId,
Expand Down Expand Up @@ -86,24 +78,11 @@ public void confirmDateTime(long darakbangId, long chatRoomId, DateTimeConfirmRe
// notificationService.notifyToMembers(NotificationType.MOIM_TIME_CONFIRMED, darakbangId, moim, darakbangMember);
}

public ChatPreviewResponses findChatPreview(DarakbangMember darakbangMember, ChatRoomType chatRoomType) {
ChatPreviewManager manager = chatPreviewManagerRegistry.getManager(chatRoomType);
List<ChatPreview> chatPreviews = manager.create(darakbangMember);

return ChatPreviewResponses.toResponse(chatPreviews);
}

public void updateLastReadChat(
long darakbangId, long chatRoomId, LastReadChatRequest request, DarakbangMember darakbangMember
) {
ChatRoom chatRoom = chatRoomFinder.read(darakbangId, chatRoomId, darakbangMember);

chatWriter.updateLastReadChat(chatRoom, darakbangMember, request.lastReadChatId());
}

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);
}
}
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 backend/src/main/java/mouda/backend/chat/domain/BetAttributes.java
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;
}
}
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();
}
}
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 backend/src/main/java/mouda/backend/chat/domain/Participant.java
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);
}
}
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);
}
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);
}
}
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;
}
}
Loading