Skip to content

Commit

Permalink
Merge pull request #162 from swm-nodriversomabus/BUS-204-Matching-owner
Browse files Browse the repository at this point in the history
Bus 204 matching owner
  • Loading branch information
namhyo01 authored Oct 27, 2023
2 parents f3117d7 + f3321e6 commit 525c7ea
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.example.api.chatroom.domain.ChatRoom;
import com.example.api.chatroom.dto.CreateChatRoomDto;
import com.example.api.matching.domain.MatchingApplication;

public interface CreateChatRoomUsecase {
ChatRoom createRoom(CreateChatRoomDto createChatRoomDto);
ChatRoom createMatchingChatRoom(MatchingApplication matchingApplication);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.example.api.chatroom.application.port.out.FindChatRoomListPort;
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.matching.domain.MatchingApplication;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.admin.AdminClient;
Expand Down Expand Up @@ -46,6 +48,23 @@ public ChatRoom createRoom(CreateChatRoomDto createChatRoomDto) {
return chatRoom;
}

/**
* createMatchingApplication Step 2
* @param matchingApplication (데이터)
* @return ChatRoom
*/
@Override
@Transactional
public ChatRoom createMatchingChatRoom(MatchingApplication matchingApplication) {
CreateChatRoomDto createChatRoomDto = CreateChatRoomDto.builder()
.masterId(matchingApplication.getUserId())
.chatroomName("매칭 신청") // 이거 바꿔야 함
.type(ChatRoomEnum.Normal)
.isActive(true)
.build();
return this.createRoom(createChatRoomDto);
}

@Override
public List<ChatRoom> chatRoomList(UUID userId, Pageable pageable) {
return findChatRoomListPort.getChatRoomList(userId, pageable).stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.api.matching.adapter.in.rest;

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.common.exception.CustomException;
import com.example.api.common.type.ApplicationStateEnum;
Expand All @@ -9,6 +10,7 @@
import com.example.api.matching.application.port.in.*;
import com.example.api.matching.domain.MatchingApplication;
import com.example.api.matching.dto.*;
import com.example.api.member.application.port.in.AddMemberChatRoomUsecase;
import com.example.api.user.application.port.in.FindUserUsecase;
import com.example.api.user.dto.FindUserDto;
import com.example.api.user.dto.UserAuthorityCheckDto;
Expand All @@ -35,21 +37,32 @@ public class MatchingController {
private final DeleteMatchingUsecase deleteMatchingUsecase;
private final MatchingApplicationUsecase matchingApplicationUsecase;
private final LikeUsecase likeUsecase;
private final CreateChatRoomUsecase createChatRoomUsecase;
private final AddMemberChatRoomUsecase addMemberChatRoomUsecase;

/**
* 새 매칭 생성
* @param matchingDto (데이터)
* @return MatchingDto
* @param saveMatchingDto (데이터)
* @return FindMatchingDto
*/
@Operation(summary = "Create matching", description = "새로운 매칭을 생성한다.")
@PostMapping("/matching")
public FindMatchingDto createMatching(@RequestBody SaveMatchingDto matchingDto) {
public FindMatchingDto createMatching(@RequestBody SaveMatchingDto saveMatchingDto) {
SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication();
if (securityUser == null) {
log.error("MatchingController::createMatching: Login is needed");
throw new CustomException(ErrorCodeEnum.LOGIN_IS_NOT_DONE);
}
return saveMatchingUsecase.createMatching(securityUser.getUserId(), matchingDto);
FindMatchingDto findMatchingDto = saveMatchingUsecase.createMatching(securityUser.getUserId(), saveMatchingDto);

SaveMatchingApplicationDto saveMatchingApplicationDto = SaveMatchingApplicationDto.builder()
.userId(securityUser.getUserId())
.matchingId(findMatchingDto.getMatchingId())
.state(ApplicationStateEnum.Owner)
.isActive(true)
.build();
matchingApplicationUsecase.createMatchingApplicationData(securityUser.getUserId(), saveMatchingApplicationDto);
return findMatchingDto;
}

/**
Expand Down Expand Up @@ -77,8 +90,8 @@ public ChatRoom createMatchingApplication(@RequestBody SaveMatchingApplicationDt
}

MatchingApplication matchingApplication = matchingApplicationUsecase.createMatchingApplicationData(securityUser.getUserId(), matchingApplicationDto);
ChatRoom chatRoom = matchingApplicationUsecase.createMatchingChatRoom(matchingApplication);
return matchingApplicationUsecase.setupMatchingChatRoom(matchingApplication, chatRoom);
ChatRoom chatRoom = createChatRoomUsecase.createMatchingChatRoom(matchingApplication);
return addMemberChatRoomUsecase.setupMatchingChatRoom(matchingApplication, chatRoom);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,4 @@ public List<MatchingApplicationEntity> getByUserIdIsAndStateEquals(UUID userId,
public List<MatchingApplicationEntity> getByMatchingIdIsAndStateEquals(Long matchingId, ApplicationStateEnum state) {
return matchingApplicationRepository.getByMatchingIdAndStateEquals(matchingId, state);
}

@Override
public MatchingApplication updateMatchingApplication(MatchingApplication matchingApplication) {
MatchingApplicationEntity matchingApplicationData = matchingApplicationRepository.save(matchingMapper.toEntity(matchingApplication));
return matchingMapper.toDomain(matchingApplicationData);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.api.matching.application.port.in;

import com.example.api.chatroom.domain.ChatRoom;
import com.example.api.common.type.ApplicationStateEnum;
import com.example.api.matching.domain.MatchingApplication;
import com.example.api.matching.dto.SaveMatchingApplicationDto;
Expand All @@ -12,8 +11,6 @@

public interface MatchingApplicationUsecase {
MatchingApplication createMatchingApplicationData(UUID userId, SaveMatchingApplicationDto matchingApplicationDto);
ChatRoom createMatchingChatRoom(MatchingApplication matchingApplication);
ChatRoom setupMatchingChatRoom(MatchingApplication matchingApplication, ChatRoom chatRoom);
List<FindMatchingDto> getByUserIdIsAndStateEquals(UUID userId, ApplicationStateEnum state);
List<FindUserDto> getByMatchingIdIsAndStateEquals(Long matchingId, ApplicationStateEnum state);
String getUserStatus(UUID userId, Long matchingId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ public interface MatchingApplicationPort {
Optional<MatchingApplicationEntity> getByMatchingApplicationPK(MatchingApplicationPK matchingApplicationPK);
List<MatchingApplicationEntity> getByUserIdIsAndStateEquals(UUID userId, ApplicationStateEnum state);
List<MatchingApplicationEntity> getByMatchingIdIsAndStateEquals(Long matchingId, ApplicationStateEnum state);
MatchingApplication updateMatchingApplication(MatchingApplication matchingApplication);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.example.api.matching.service;

import com.example.api.chatroom.domain.ChatRoom;
import com.example.api.chatroom.dto.CreateChatRoomDto;
import com.example.api.chatroom.service.ChatRoomService;
import com.example.api.chatroom.type.ChatRoomEnum;
import com.example.api.common.type.ApplicationStateEnum;
import com.example.api.fcm.dto.FcmDto;
import com.example.api.fcm.service.FcmService;
Expand All @@ -14,11 +10,8 @@
import com.example.api.matching.application.port.out.FindMatchingPort;
import com.example.api.matching.application.port.out.MatchingApplicationPort;
import com.example.api.matching.domain.MatchingApplication;
import com.example.api.matching.dto.FindMatchingApplicationDto;
import com.example.api.matching.dto.FindMatchingDto;
import com.example.api.matching.dto.SaveMatchingApplicationDto;
import com.example.api.member.dto.AddMemberDto;
import com.example.api.member.service.MemberService;
import com.example.api.user.adapter.out.persistence.UserMapperInterface;
import com.example.api.user.application.port.out.FindUserPort;
import com.example.api.user.dto.FindUserDto;
Expand All @@ -39,8 +32,6 @@ public class MatchingApplicationService implements MatchingApplicationUsecase {
private final FindUserPort findUserPort;
private final FindMatchingPort findMatchingPort;
private final MatchingApplicationPort matchingApplicationPort;
private final ChatRoomService chatRoomService;
private final MemberService memberService;
private final FcmService fcmService;

/**
Expand All @@ -57,48 +48,6 @@ public MatchingApplication createMatchingApplicationData(UUID userId, SaveMatchi
}
return matchingApplicationPort.createMatchingApplication(matchingApplication);
}

/**
* createMatchingApplication Step 2
* @param matchingApplication (데이터)
* @return ChatRoom
*/
@Override
@Transactional
public ChatRoom createMatchingChatRoom(MatchingApplication matchingApplication) {
CreateChatRoomDto createChatRoomDto = CreateChatRoomDto.builder()
.masterId(matchingApplication.getUserId())
.chatroomName("매칭 신청") // 이거 바꿔야 함
.type(ChatRoomEnum.Normal)
.isActive(true)
.build();
return chatRoomService.createRoom(createChatRoomDto);
}

/**
* createMatchingApplication Step 3
* @param matchingApplication (데이터)
* @param chatRoom (데이터)
*/
@Override
@Transactional
public ChatRoom setupMatchingChatRoom(MatchingApplication matchingApplication, ChatRoom chatRoom) {
List<UUID> memberIds = new ArrayList<>();
memberIds.add(matchingApplication.getUserId());
UUID matchingWriterId = findMatchingPort.getByMatchingId(matchingApplication.getMatchingId())
.orElseThrow(NoSuchElementException::new)
.getWriterId();
memberIds.add(matchingWriterId);

AddMemberDto addMemberDto = AddMemberDto.builder()
.chatroomId(chatRoom.getChatroomId())
.memberIds(memberIds)
.build();
memberService.addMember(addMemberDto);

chatRoom.setMembers(memberIds);
return chatRoom;
}

@Override
public List<FindMatchingDto> getByUserIdIsAndStateEquals(UUID userId, ApplicationStateEnum state) {
Expand Down Expand Up @@ -131,12 +80,6 @@ public String getUserStatus(UUID userId, Long matchingId) {
return "None";
}
}

@Transactional
public FindMatchingApplicationDto updateMatchingApplication(SaveMatchingApplicationDto matchingApplicationDto) {
MatchingApplication matchingApplication = matchingApplicationPort.updateMatchingApplication(matchingMapper.toDomain(matchingApplicationDto));
return matchingMapper.toDto(matchingApplication);
}

@Override
@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.example.api.member.application.port.in;

import com.example.api.chatroom.domain.ChatRoom;
import com.example.api.matching.domain.MatchingApplication;
import com.example.api.member.dto.AddMemberDto;

import java.util.List;

public interface AddMemberChatRoomUsecase {
void addMember(AddMemberDto addMemberDto);
}
ChatRoom setupMatchingChatRoom(MatchingApplication matchingApplication, ChatRoom chatRoom);
}
29 changes: 29 additions & 0 deletions src/main/java/com/example/api/member/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.example.api.chatroom.application.port.out.RetrieveChatRoomPort;
import com.example.api.chatroom.domain.ChatRoom;
import com.example.api.matching.application.port.out.FindMatchingPort;
import com.example.api.matching.domain.MatchingApplication;
import com.example.api.member.application.port.in.AddMemberChatRoomUsecase;
import com.example.api.member.application.port.out.AddMemberChatRoomPort;
import com.example.api.member.domain.Member;
Expand All @@ -13,13 +15,15 @@

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.UUID;

@Service
@Slf4j
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class MemberService implements AddMemberChatRoomUsecase {
private final FindMatchingPort findMatchingPort;
private final AddMemberChatRoomPort addMemberChatRoomPort;
private final RetrieveChatRoomPort retrieveChatRoomPort;

Expand All @@ -42,4 +46,29 @@ public void addMember(AddMemberDto addMemberDto) {
}
addMemberChatRoomPort.addMember(members, chatRoom);
}

/**
* createMatchingApplication Step 3
* @param matchingApplication (데이터)
* @param chatRoom (데이터)
*/
@Override
@Transactional
public ChatRoom setupMatchingChatRoom(MatchingApplication matchingApplication, ChatRoom chatRoom) {
List<UUID> memberIds = new ArrayList<>();
memberIds.add(matchingApplication.getUserId());
UUID matchingWriterId = findMatchingPort.getByMatchingId(matchingApplication.getMatchingId())
.orElseThrow(NoSuchElementException::new)
.getWriterId();
memberIds.add(matchingWriterId);

AddMemberDto addMemberDto = AddMemberDto.builder()
.chatroomId(chatRoom.getChatroomId())
.memberIds(memberIds)
.build();
this.addMember(addMemberDto);

chatRoom.setMembers(memberIds);
return chatRoom;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,4 @@ void getByMatchingIdIsAndStateEqualsTest() {
matchingApplicationPersistenceAdapter.getByMatchingIdIsAndStateEquals(1L, ApplicationStateEnum.Approved);
verify(matchingApplicationRepository, times(1)).getByMatchingIdAndStateEquals(1L, ApplicationStateEnum.Approved);
}

@Test
void updateMatchingApplicationTest() {
matchingApplication.setState(ApplicationStateEnum.Approved);
matchingApplicationPersistenceAdapter.updateMatchingApplication(matchingApplication);
verify(matchingApplicationRepository, times(1)).save(matchingMapper.toEntity(matchingApplication));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,4 @@ void getByMatchingIdIsAndStateEqualsTest() {
matchingApplicationService.getByMatchingIdIsAndStateEquals(1L, ApplicationStateEnum.Approved);
verify(matchingApplicationPort, times(1)).getByMatchingIdIsAndStateEquals(1L, ApplicationStateEnum.Approved);
}

@Test
void updateMatchingApplicationTest() {
matchingApplication.setState(ApplicationStateEnum.Approved);
matchingApplicationService.updateMatchingApplication(matchingApplication);
verify(matchingApplicationPort, times(1)).updateMatchingApplication(matchingMapper.toDomain(matchingApplication));
}
}

0 comments on commit 525c7ea

Please sign in to comment.