From be653dc7401a0be79131bd4749ca6074caa91e53 Mon Sep 17 00:00:00 2001 From: Lemonade255 Date: Mon, 13 Nov 2023 14:24:08 +0900 Subject: [PATCH] =?UTF-8?q?feat(BE):=20=EB=A7=A4=EC=B9=AD=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EC=8B=9C=20=EC=B1=84=ED=8C=85=EB=B0=A9=20=EC=97=B0?= =?UTF-8?q?=EA=B2=B0=20BUS-208-matching-chatroom-connect=20#202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/chatroom/type/ChatRoomEnum.java | 8 ++++-- .../adapter/in/rest/MatchingController.java | 17 +++++++++++ .../out/persistence/MatchingEntity.java | 3 ++ .../example/api/matching/domain/Matching.java | 1 + .../api/matching/dto/FindMatchingDto.java | 3 ++ .../api/matching/dto/SaveMatchingDto.java | 3 ++ .../service/MatchingApplicationService.java | 11 ++++++++ .../adapter/in/rest/MemberController.java | 4 +-- .../persistence/MemberPersistentAdapter.java | 2 +- .../port/in/AddMemberChatRoomUsecase.java | 5 +++- .../port/out/AddMemberChatRoomPort.java | 2 +- .../api/member/service/MemberService.java | 28 +++++++++++++------ 12 files changed, 71 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/example/api/chatroom/type/ChatRoomEnum.java b/src/main/java/com/example/api/chatroom/type/ChatRoomEnum.java index 92d28b0..03c00ff 100644 --- a/src/main/java/com/example/api/chatroom/type/ChatRoomEnum.java +++ b/src/main/java/com/example/api/chatroom/type/ChatRoomEnum.java @@ -8,8 +8,10 @@ @AllArgsConstructor @Getter public enum ChatRoomEnum { - Normal("일반"), - Inquery("상담"); + Normal("일반", 1), + Matching("매칭", 2), + Inquiry("상담", 3); private final String type; -} + private final Integer typeCode; +} \ No newline at end of file diff --git a/src/main/java/com/example/api/matching/adapter/in/rest/MatchingController.java b/src/main/java/com/example/api/matching/adapter/in/rest/MatchingController.java index 8e3ec7e..fb2e98a 100644 --- a/src/main/java/com/example/api/matching/adapter/in/rest/MatchingController.java +++ b/src/main/java/com/example/api/matching/adapter/in/rest/MatchingController.java @@ -3,6 +3,8 @@ import com.example.api.auth.domain.SecurityUser; import com.example.api.chatroom.application.port.in.CreateChatRoomUsecase; import com.example.api.chatroom.domain.ChatRoom; +import com.example.api.chatroom.dto.CreateChatRoomDto; +import com.example.api.chatroom.type.ChatRoomEnum; import com.example.api.common.exception.CustomException; import com.example.api.common.type.ApplicationStateEnum; import com.example.api.common.type.ErrorCodeEnum; @@ -55,8 +57,22 @@ public FindMatchingDto createMatching(@Valid @RequestBody SaveMatchingDto saveMa log.error("MatchingController::createMatching: Login is needed"); throw new CustomException(ErrorCodeEnum.LOGIN_IS_NOT_DONE); } + + // 매칭 전용 채팅방 생성 + CreateChatRoomDto createChatRoomDto = CreateChatRoomDto.builder() + .masterId(securityUser.getUserId()) + .chatroomName(saveMatchingDto.getTitle()) + .type(ChatRoomEnum.Matching) + .isActive(true) + .build(); + ChatRoom chatRoom = createChatRoomUsecase.createRoom(createChatRoomDto); + addMemberChatRoomUsecase.addMember(chatRoom.getChatroomId(), securityUser.getUserId()); + saveMatchingDto.setChatRoomId(chatRoom.getChatroomId()); + + // 매칭 데이터 저장 FindMatchingDto findMatchingDto = saveMatchingUsecase.createMatching(securityUser.getUserId(), saveMatchingDto); + // Owner 등록 SaveMatchingApplicationDto saveMatchingApplicationDto = SaveMatchingApplicationDto.builder() .userId(securityUser.getUserId()) .matchingId(findMatchingDto.getMatchingId()) @@ -64,6 +80,7 @@ public FindMatchingDto createMatching(@Valid @RequestBody SaveMatchingDto saveMa .isActive(true) .build(); matchingApplicationUsecase.createMatchingApplicationData(securityUser.getUserId(), saveMatchingApplicationDto); + return findMatchingDto; } diff --git a/src/main/java/com/example/api/matching/adapter/out/persistence/MatchingEntity.java b/src/main/java/com/example/api/matching/adapter/out/persistence/MatchingEntity.java index bff8d60..59dcadc 100644 --- a/src/main/java/com/example/api/matching/adapter/out/persistence/MatchingEntity.java +++ b/src/main/java/com/example/api/matching/adapter/out/persistence/MatchingEntity.java @@ -25,6 +25,9 @@ public class MatchingEntity extends BaseEntity { @Column(nullable = false) private UUID writerId; + @Column(nullable = false) + private UUID chatRoomId; + @Column(nullable = false) @Enumerated(EnumType.STRING) private MatchingTypeEnum type; diff --git a/src/main/java/com/example/api/matching/domain/Matching.java b/src/main/java/com/example/api/matching/domain/Matching.java index a438303..1cd09b6 100644 --- a/src/main/java/com/example/api/matching/domain/Matching.java +++ b/src/main/java/com/example/api/matching/domain/Matching.java @@ -14,6 +14,7 @@ public class Matching { private Long matchingId; private UUID writerId; + private UUID chatRoomId; private MatchingTypeEnum type; private String title; private String place; diff --git a/src/main/java/com/example/api/matching/dto/FindMatchingDto.java b/src/main/java/com/example/api/matching/dto/FindMatchingDto.java index b5b0bf5..1944866 100644 --- a/src/main/java/com/example/api/matching/dto/FindMatchingDto.java +++ b/src/main/java/com/example/api/matching/dto/FindMatchingDto.java @@ -21,6 +21,9 @@ public class FindMatchingDto { @NotNull private UUID writerId; + @NotNull + private UUID chatRoomId; + @NotNull private MatchingTypeEnum type; diff --git a/src/main/java/com/example/api/matching/dto/SaveMatchingDto.java b/src/main/java/com/example/api/matching/dto/SaveMatchingDto.java index dd577be..e6a6d61 100644 --- a/src/main/java/com/example/api/matching/dto/SaveMatchingDto.java +++ b/src/main/java/com/example/api/matching/dto/SaveMatchingDto.java @@ -7,6 +7,7 @@ import lombok.*; import java.time.LocalDateTime; +import java.util.UUID; @Getter @Setter @@ -15,6 +16,8 @@ @NoArgsConstructor @AllArgsConstructor public class SaveMatchingDto { + private UUID chatRoomId; + @NotNull private MatchingTypeEnum type; diff --git a/src/main/java/com/example/api/matching/service/MatchingApplicationService.java b/src/main/java/com/example/api/matching/service/MatchingApplicationService.java index 98026cd..ee85f48 100644 --- a/src/main/java/com/example/api/matching/service/MatchingApplicationService.java +++ b/src/main/java/com/example/api/matching/service/MatchingApplicationService.java @@ -16,6 +16,7 @@ import com.example.api.matching.domain.MatchingApplication; import com.example.api.matching.dto.FindMatchingDto; import com.example.api.matching.dto.SaveMatchingApplicationDto; +import com.example.api.member.service.MemberService; import com.example.api.user.adapter.out.persistence.UserEntity; import com.example.api.user.adapter.out.persistence.UserMapperInterface; import com.example.api.user.application.port.out.FindUserPort; @@ -37,6 +38,7 @@ public class MatchingApplicationService implements MatchingApplicationUsecase { private final FindUserPort findUserPort; private final FindMatchingPort findMatchingPort; private final MatchingApplicationPort matchingApplicationPort; + private final MemberService memberService; private final FcmService fcmService; /** @@ -126,9 +128,18 @@ public void processMatchingApplication(SaveMatchingApplicationDto matchingApplic log.error("MatchingApplicationService::processMatchingApplication: Data not found"); throw new CustomException(ErrorCodeEnum.APPLICATION_NOT_FOUND); } + MatchingApplication matchingApplication = matchingMapper.toDomain(matchingApplicationEntity.get()); matchingApplication.setState(state); matchingApplicationPort.saveMatchingApplication(matchingApplication); + + // 신청 수락 시 매칭 채팅방에 멤버 초대 + if (state.equals(ApplicationStateEnum.Approved)) { + MatchingEntity matchingEntity = findMatchingPort.getByMatchingId(matchingApplication.getMatchingId()).get(); + memberService.addMember(matchingEntity.getChatRoomId(), matchingApplication.getUserId()); + } + + // 푸시 알림 전송 FcmDto fcmDto = FcmDto.builder() .userId(matchingApplication.getUserId()) .title(state.equals(ApplicationStateEnum.Approved) ? "신청 수락" : "신청 거절") diff --git a/src/main/java/com/example/api/member/adapter/in/rest/MemberController.java b/src/main/java/com/example/api/member/adapter/in/rest/MemberController.java index 8a36f9f..d971071 100644 --- a/src/main/java/com/example/api/member/adapter/in/rest/MemberController.java +++ b/src/main/java/com/example/api/member/adapter/in/rest/MemberController.java @@ -26,7 +26,7 @@ public class MemberController { * 방 생성 후, 유저들을 초대해 들어오는 경우 * @param addMemberDto (데이터) */ - @Operation(summary = "Add member", description = "채팅방에 사용자를 초대한다.") + @Operation(summary = "Add members", description = "채팅방에 사용자를 초대한다.") @PostMapping("/members") public void addMembers(@Valid @RequestBody AddMemberDto addMemberDto) { SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication(); @@ -34,6 +34,6 @@ public void addMembers(@Valid @RequestBody AddMemberDto addMemberDto) { log.error("MemberController::addMembers: Login is needed"); throw new CustomException(ErrorCodeEnum.LOGIN_IS_NOT_DONE); } - addMemberChatRoomUsecase.addMember(addMemberDto); + addMemberChatRoomUsecase.addMembers(addMemberDto); } } \ No newline at end of file diff --git a/src/main/java/com/example/api/member/adapter/out/persistence/MemberPersistentAdapter.java b/src/main/java/com/example/api/member/adapter/out/persistence/MemberPersistentAdapter.java index df90f70..59ee0dd 100644 --- a/src/main/java/com/example/api/member/adapter/out/persistence/MemberPersistentAdapter.java +++ b/src/main/java/com/example/api/member/adapter/out/persistence/MemberPersistentAdapter.java @@ -20,7 +20,7 @@ public class MemberPersistentAdapter implements AddMemberChatRoomPort { private final MemberRepository memberRepository; @Override - public void addMember(List members, ChatRoom chatRoom) { + public void addMembers(List members, ChatRoom chatRoom) { memberRepository.saveAll(memberMapper.fromListDomainToEntity(members, chatRoomMapper.toEntity(chatRoom))); } } \ No newline at end of file diff --git a/src/main/java/com/example/api/member/application/port/in/AddMemberChatRoomUsecase.java b/src/main/java/com/example/api/member/application/port/in/AddMemberChatRoomUsecase.java index a46a529..8623e0b 100644 --- a/src/main/java/com/example/api/member/application/port/in/AddMemberChatRoomUsecase.java +++ b/src/main/java/com/example/api/member/application/port/in/AddMemberChatRoomUsecase.java @@ -4,7 +4,10 @@ import com.example.api.matching.domain.MatchingApplication; import com.example.api.member.dto.AddMemberDto; +import java.util.UUID; + public interface AddMemberChatRoomUsecase { - void addMember(AddMemberDto addMemberDto); + void addMember(UUID chatRoomId, UUID userId); + void addMembers(AddMemberDto addMemberDto); ChatRoom setupMatchingChatRoom(MatchingApplication matchingApplication, ChatRoom chatRoom); } \ No newline at end of file diff --git a/src/main/java/com/example/api/member/application/port/out/AddMemberChatRoomPort.java b/src/main/java/com/example/api/member/application/port/out/AddMemberChatRoomPort.java index 918d585..d68acbc 100644 --- a/src/main/java/com/example/api/member/application/port/out/AddMemberChatRoomPort.java +++ b/src/main/java/com/example/api/member/application/port/out/AddMemberChatRoomPort.java @@ -6,5 +6,5 @@ import java.util.List; public interface AddMemberChatRoomPort { - void addMember(List members, ChatRoom chatRoom); + void addMembers(List members, ChatRoom chatRoom); } \ No newline at end of file diff --git a/src/main/java/com/example/api/member/service/MemberService.java b/src/main/java/com/example/api/member/service/MemberService.java index 6162d0f..174b302 100644 --- a/src/main/java/com/example/api/member/service/MemberService.java +++ b/src/main/java/com/example/api/member/service/MemberService.java @@ -13,10 +13,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; -import java.util.List; -import java.util.NoSuchElementException; -import java.util.UUID; +import java.util.*; @Service @Slf4j @@ -26,14 +23,29 @@ public class MemberService implements AddMemberChatRoomUsecase { private final FindMatchingPort findMatchingPort; private final AddMemberChatRoomPort addMemberChatRoomPort; private final RetrieveChatRoomPort retrieveChatRoomPort; + + /** + * 채팅방의 한 명의 멤버 추가 + * @param chatRoomId (채팅방 ID) + * @param userId (사용자 ID) + */ + @Override + @Transactional + public void addMember(UUID chatRoomId, UUID userId) { + AddMemberDto addMemberDto = AddMemberDto.builder() + .chatroomId(chatRoomId) + .memberIds(new ArrayList<>(Collections.singletonList(userId))) + .build(); + this.addMembers(addMemberDto); + } /** - * 채팅방에 멤버 추가 + * 채팅방에 여러 명의 멤버 추가 * @param addMemberDto (데이터) */ @Override @Transactional - public void addMember(AddMemberDto addMemberDto) { + public void addMembers(AddMemberDto addMemberDto) { List members = new ArrayList<>(); ChatRoom chatRoom = retrieveChatRoomPort.retrieveChatRoom(addMemberDto.getChatroomId()); for (UUID userId: addMemberDto.getMemberIds()) { @@ -44,7 +56,7 @@ public void addMember(AddMemberDto addMemberDto) { .build(); members.add(member); } - addMemberChatRoomPort.addMember(members, chatRoom); + addMemberChatRoomPort.addMembers(members, chatRoom); } /** @@ -66,7 +78,7 @@ public ChatRoom setupMatchingChatRoom(MatchingApplication matchingApplication, C .chatroomId(chatRoom.getChatroomId()) .memberIds(memberIds) .build(); - this.addMember(addMemberDto); + this.addMembers(addMemberDto); chatRoom.setMembers(memberIds); return chatRoom;