From 3f509e10f991747da6f44e01fdad29cb92e1a604 Mon Sep 17 00:00:00 2001 From: kim_sang_ june <79149384+drbug2000@users.noreply.github.com> Date: Mon, 23 Sep 2024 16:27:10 +0900 Subject: [PATCH 01/11] [test] voice room service test : create voice room --- .../dto/VoiceRoom/PostVoiceRoomDto.java | 2 + .../VoiceRoom/VoiceRoomServiceTest.java | 143 ++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 src/test/java/space/space_spring/domain/VoiceRoom/VoiceRoomServiceTest.java diff --git a/src/main/java/space/space_spring/dto/VoiceRoom/PostVoiceRoomDto.java b/src/main/java/space/space_spring/dto/VoiceRoom/PostVoiceRoomDto.java index a3c0dc41..833df893 100644 --- a/src/main/java/space/space_spring/dto/VoiceRoom/PostVoiceRoomDto.java +++ b/src/main/java/space/space_spring/dto/VoiceRoom/PostVoiceRoomDto.java @@ -2,10 +2,12 @@ import jakarta.validation.constraints.NotNull; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; public class PostVoiceRoomDto { @Getter + @Builder public static class Request{ @NotNull(message = "name is mandatory") private String name; diff --git a/src/test/java/space/space_spring/domain/VoiceRoom/VoiceRoomServiceTest.java b/src/test/java/space/space_spring/domain/VoiceRoom/VoiceRoomServiceTest.java new file mode 100644 index 00000000..91f95083 --- /dev/null +++ b/src/test/java/space/space_spring/domain/VoiceRoom/VoiceRoomServiceTest.java @@ -0,0 +1,143 @@ +package space.space_spring.domain.VoiceRoom; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito.*; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.test.util.ReflectionTestUtils; +import space.space_spring.dao.UserDao; +import space.space_spring.dao.UserSpaceDao; +import space.space_spring.dao.VoiceRoomDao; +import space.space_spring.dao.VoiceRoomRepository; +import space.space_spring.dto.VoiceRoom.PostVoiceRoomDto; +import space.space_spring.entity.*; +import space.space_spring.entity.enumStatus.UserSignupType; +import space.space_spring.service.VoiceRoomService; +import space.space_spring.util.LiveKitUtils; +import space.space_spring.util.space.SpaceUtils; + + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class VoiceRoomServiceTest { + @InjectMocks + private VoiceRoomService voiceRoomService; + + @Mock + private VoiceRoomRepository voiceRoomRepository; + + @Mock + private VoiceRoomDao voiceRoomDao; + + @Mock + // 삭제 예정 + private SpaceUtils spaceUtils; + + @Mock + private LiveKitUtils liveKitUtils; + + @Mock + private UserDao userDao; + + @Mock + private UserSpaceDao userSpaceDao; + + + private User user1; + private User user2; + private User user3; + private User user4; + private User user5; + private User user6; + private User user7; + private Space testSpace; + private PayRequest testPayRequest_user1; + private PayRequest testPayRequest_user2; + private PayRequestTarget testPayRequestTarget_user1; + private PayRequestTarget testPayRequestTarget_user2; + private PayRequestTarget testPayRequestTarget_user3; + private PayRequestTarget testPayRequestTarget_user4; + + @BeforeEach + public void 테스트_셋업() { + /** + * : user2, 3은 정산 완료 X & user4는 정산 완료 + * : user1은 정산 완료 + */ + user1 = new User(); + user1.saveUser("test1@test.com", "abcDEF123!@", "user1", UserSignupType.LOCAL); + + user2 = new User(); + user2.saveUser("test2@test.com", "abcDEF123!@", "user2", UserSignupType.LOCAL); + + user3 = new User(); + user3.saveUser("test3@test.com", "abcDEF123!@", "user3", UserSignupType.LOCAL); + + user4 = new User(); + user4.saveUser("test4@test.com", "abcDEF123!@", "user4", UserSignupType.LOCAL); + + testSpace = new Space(); + testSpace.saveSpace("testSpace", "test_profile_img_url"); + + testPayRequest_user1 = new PayRequest(); + testPayRequest_user1.savePayRequest(user1, testSpace, 30000, "우리은행", "111-111-111", 0, false); + + testPayRequestTarget_user2 = new PayRequestTarget(); + testPayRequestTarget_user2.savePayRequestTarget(testPayRequest_user1, user2.getUserId(), 10000, false); + + testPayRequestTarget_user3 = new PayRequestTarget(); + testPayRequestTarget_user3.savePayRequestTarget(testPayRequest_user1, user3.getUserId(), 10000, false); + + testPayRequestTarget_user4 = new PayRequestTarget(); + testPayRequestTarget_user4.savePayRequestTarget(testPayRequest_user1, user4.getUserId(), 10000, true); + + testPayRequest_user2 = new PayRequest(); + testPayRequest_user2.savePayRequest(user2, testSpace, 10000, "국민은행", "111-111-111", 0, true); + + testPayRequestTarget_user1 = new PayRequestTarget(); + testPayRequestTarget_user1.savePayRequestTarget(testPayRequest_user2, user1.getUserId(), 10000, true); + + /** + * 추가로 user5,6,7 생성 + */ + user5 = new User(); + user6 = new User(); + user7 = new User(); + user5.saveUser("test5@test.com", "abcDEF123!@", "user5", UserSignupType.LOCAL); + user6.saveUser("test6@test.com", "abcDEF123!@", "user6", UserSignupType.LOCAL); + user7.saveUser("test7@test.com", "abcDEF123!@", "user7", UserSignupType.LOCAL); + + } + + @Test + @DisplayName("create VoiceRoom test") + void voiceRoom_create_test(){ + //given + int spaceOrder =1; + String voiceRoomName = "voiceRoom"; + Long spaceId = 2L; + //when + //when(testSpace.getSpaceId()).thenReturn(1L); + //testSpace. + ReflectionTestUtils.setField(testSpace,"spaceId",spaceId); + when(spaceUtils.findSpaceBySpaceId(testSpace.getSpaceId())).thenReturn(testSpace); + when(voiceRoomRepository.findMaxOrderBySpace(testSpace)).thenReturn(spaceOrder); + when(voiceRoomDao.createVoiceRoom(voiceRoomName,spaceOrder+1,testSpace)).thenReturn(1L); + + + PostVoiceRoomDto.Request request = PostVoiceRoomDto.Request.builder().name(voiceRoomName).build(); + Long voiceRoomId = voiceRoomService.createVoiceRoom(testSpace.getSpaceId(),request); + + //then + assertThat(voiceRoomId).isEqualTo(1L); + + } +} From 8567dec86fe2a1c9f647543c7dee1bbeb50f5294 Mon Sep 17 00:00:00 2001 From: kim_sang_ june <79149384+drbug2000@users.noreply.github.com> Date: Fri, 27 Sep 2024 18:00:49 +0900 Subject: [PATCH 02/11] [refactoring] add Todo comment --- .../space/space_spring/service/VoiceRoomService.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/space/space_spring/service/VoiceRoomService.java b/src/main/java/space/space_spring/service/VoiceRoomService.java index 7c9b709f..8879cd8e 100644 --- a/src/main/java/space/space_spring/service/VoiceRoomService.java +++ b/src/main/java/space/space_spring/service/VoiceRoomService.java @@ -121,7 +121,10 @@ public List getVoiceRoomInfoListConcurrency(long //해당 space VoiceRoom 가져오기 (VoiceRoom List) //Todo 가져오기에 limit 적용 + //todo 일급 객체 `voiceRoomEntityList`로 변경 List voiceRoomDataList = findBySpaceId(spaceId); + //todo voiceRoomEntityList 객체에 책임 위임 + //todo RoomDtoList 일급 객체 생성 List roomDtoList = RoomDto.convertRoomDtoListByVoiceRoom(voiceRoomDataList); //VoiceRoom과 Room mapping //#1 Response 받아오기 @@ -136,6 +139,8 @@ public List getVoiceRoomInfoListConcurrency(long for(RoomDto roomDto : roomDtoList){ roomDto.setActiveRoom(roomResponses); } + //ToDo setRoomDto 함수를 RoomDtoList 객체로 이동 + //todo 책임을 위임해도 이 병렬처리 코드가 잘 동작할까? List> roomDtoFutureList = roomDtoList.stream() .map(r->CompletableFuture.runAsync(()->setRoomDto(r,roomResponses,req),taskExecutor) //.exceptionally(ex->{throws ex;}) @@ -160,7 +165,7 @@ public List getVoiceRoomInfoListConcurrency(long } //return null; } - + //todo 해당 함수의 책임을 RoomDto에게 위임 private void setRoomDto(RoomDto roomDto,List roomResponses,GetVoiceRoomList.Request req){ roomDto.setActiveRoom(roomResponses); @@ -222,7 +227,7 @@ public boolean updateVoiceRoom(List updateRoomList){ //Todo 입력된 order가 유효한지 확인 필요 - + //Todo 병렬적으로 update하도록 수정 for(PatchVoiceRoom.UpdateRoom updateRoom : updateRoomList){ VoiceRoom voiceRoom = voiceRoomRepository.findById(updateRoom.getRoomId()).get(); String newName =updateRoom.getName(); From c1cd862122e8a4ebf50f63e0928c2c780d45dc66 Mon Sep 17 00:00:00 2001 From: kim_sang_ june <79149384+drbug2000@users.noreply.github.com> Date: Tue, 1 Oct 2024 12:54:36 +0900 Subject: [PATCH 03/11] [refactoring] change setActive func --- .../dto/VoiceRoom/VoiceRoomDtoList.java | 113 ++++++++++++++++++ .../dto/VoiceRoom/VoiceRoomEntityList.java | 22 ++++ 2 files changed, 135 insertions(+) create mode 100644 src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java create mode 100644 src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomEntityList.java diff --git a/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java b/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java new file mode 100644 index 00000000..80b5c293 --- /dev/null +++ b/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java @@ -0,0 +1,113 @@ +package space.space_spring.dto.VoiceRoom; + +import livekit.LivekitModels; +import lombok.RequiredArgsConstructor; +import org.springframework.core.task.TaskExecutor; +import org.springframework.stereotype.Service; +import space.space_spring.controller.VoiceRoomController; +import space.space_spring.dao.UserDao; +import space.space_spring.dao.UserSpaceDao; +import space.space_spring.dao.VoiceRoomRepository; +import space.space_spring.entity.Space; +import space.space_spring.entity.User; +import space.space_spring.util.LiveKitUtils; + +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; +@Service +@RequiredArgsConstructor +public class VoiceRoomDtoList { + private List roomDtoList; + private final LiveKitUtils liveKitUtils; + private final VoiceRoomRepository voiceRoomRepository; + private final UserSpaceDao userSpaceDao; + private final UserDao userDao; + private final TaskExecutor taskExecutor; + public void RoomDtoList(List roomDtos){ + this.roomDtoList=roomDtos; + } + + public void setActiveRoom(){ + //#1 Response 받아오기 + + List roomResponsesTemp = liveKitUtils.getRoomList(); + + //불변 list로 변환 + List roomResponses = Collections.unmodifiableList(roomResponsesTemp); + /** + * 병렬 처리 적용대상 1 + */ + //#2 Room과 mapping 시키기 + for(RoomDto roomDto : roomDtoList){ + roomDto.setActiveRoom(roomResponses); + } + //ToDo setRoomDto 함수를 RoomDtoList 객체로 이동 + //todo 책임을 위임해도 이 병렬처리 코드가 잘 동작할까? + List> roomDtoFutureList = roomDtoList.stream() + .map(r->CompletableFuture.runAsync(()->setRoomDto(r,roomResponses,req),taskExecutor) + //.exceptionally(ex->{throws ex;}) + ) + .collect(Collectors.toList()); + + + // 모든 Future의 완료를 기다림 + CompletableFuture allOf = CompletableFuture.allOf( + roomDtoFutureList.toArray(new CompletableFuture[0])); + + // 결과 수집 및 출력 + allOf.join(); + //ToDo Response로 convert + //#1 Active/inActive 분리 + } + + public void setRoomDto (RoomDto roomDto,List roomResponses,GetVoiceRoomList.Request req){ + roomDto.setActiveRoom(roomResponses); + + if(!req.isShowParticipant()){ + return; + } + + if(roomDto.getNumParticipants()==0){ + //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 + System.out.print("\n[DEBUG]Participant Number : 0\n"); + roomDto.setParticipantDTOList(Collections.emptyList()); + return; + } + //participantDto List 가져오기 + List participantDtoList = getParticipantDtoListById(roomDto.getId()); + + //RoomDto에 값 집어넣기 + //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 + if(participantDtoList==null||participantDtoList.isEmpty()){ + System.out.print("\n\n[DEBUG]participant response is empty or null"+participantDtoList.toString()+ + "participant number is \n\n"); + roomDto.setParticipantDTOList(Collections.emptyList()); + }else { + roomDto.setParticipantDTOList(participantDtoList); + } + + } + + private List getParticipantDtoListById(long voiceRoomId){ + Space space = voiceRoomRepository.findById(voiceRoomId).getSpace(); + List participantDtoList = liveKitUtils.getParticipantInfo(String.valueOf(voiceRoomId)); + if(participantDtoList==null||participantDtoList.isEmpty()){ + return Collections.emptyList(); + } + for(ParticipantDto participantDto: participantDtoList){ + //profileIamge 집어넣기 + participantDto.setProfileImage(findProfileImageByUserId(participantDto.getUserSpaceId())); + //userSpaceId 집어 넣기 + User user = userDao.findUserByUserId(participantDto.getId()); + participantDto.setUserSpaceId(userSpaceDao.findUserSpaceByUserAndSpace(user,space).get().getUserSpaceId()); + } + return participantDtoList; + } + private String findProfileImageByUserId(Long userSpaceId){ + return userSpaceDao.findProfileImageById(userSpaceId).orElse(""); + } + + +} diff --git a/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomEntityList.java b/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomEntityList.java new file mode 100644 index 00000000..11a30826 --- /dev/null +++ b/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomEntityList.java @@ -0,0 +1,22 @@ +package space.space_spring.dto.VoiceRoom; + +import space.space_spring.entity.VoiceRoom; + +import java.util.List; +import java.util.stream.Collectors; + +public class VoiceRoomEntityList { + + private List voiceRoomEntityList; + + public void VoiceRoomList(List voiceRoomEntityList) { + this.voiceRoomEntityList = voiceRoomEntityList; + } + + public List convertRoomDto(){ + if(this.voiceRoomEntityList==null||this.voiceRoomEntityList.isEmpty()){return null;} + return this.voiceRoomEntityList.stream() + .map(RoomDto::convertRoom) + .collect(Collectors.toList()); + } +} \ No newline at end of file From 1ab4d975e502523a2ffd01df2ddcf8ac814d4173 Mon Sep 17 00:00:00 2001 From: kim_sang_ june <79149384+drbug2000@users.noreply.github.com> Date: Tue, 1 Oct 2024 12:57:16 +0900 Subject: [PATCH 04/11] [feat] ask roomList By roomNameList --- src/main/java/space/space_spring/util/LiveKitUtils.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/space/space_spring/util/LiveKitUtils.java b/src/main/java/space/space_spring/util/LiveKitUtils.java index 8984ec69..f9c36e88 100644 --- a/src/main/java/space/space_spring/util/LiveKitUtils.java +++ b/src/main/java/space/space_spring/util/LiveKitUtils.java @@ -28,13 +28,13 @@ public class LiveKitUtils { private final RestTemplate restTemplate; - public List getRoomList() { + public List getRoomList(List roomNameList) { String liveKitHost = hostUrl; System.out.print("[liveKit Url]:"+hostUrl); RoomServiceClient roomServiceClient = RoomServiceClient.createClient(liveKitHost, apiKey, apiSecretKey); try { - Response> response = roomServiceClient.listRooms().execute(); + Response> response = roomServiceClient.listRooms(roomNameList).execute(); System.out.print(response.body()); //response.body().get(0). @@ -48,6 +48,10 @@ public List getRoomList() { } + public List getRoomList(){ + return getRoomList(null); + } + public List getParticipantInfo(String roomName) { String liveKitHost = hostUrl; RoomServiceClient roomServiceClient = RoomServiceClient.createClient(liveKitHost, apiKey, apiSecretKey); From 55f682c3e603e90af102c8a0f084dfd65f901ed3 Mon Sep 17 00:00:00 2001 From: kim_sang_ june <79149384+drbug2000@users.noreply.github.com> Date: Tue, 1 Oct 2024 12:58:26 +0900 Subject: [PATCH 05/11] [refactoring] add convert func in RoomDto --- .../space_spring/dto/VoiceRoom/RoomDto.java | 18 +- .../dto/VoiceRoom/VoiceRoomDtoList.java | 167 +++++++++--------- 2 files changed, 101 insertions(+), 84 deletions(-) diff --git a/src/main/java/space/space_spring/dto/VoiceRoom/RoomDto.java b/src/main/java/space/space_spring/dto/VoiceRoom/RoomDto.java index 5183274a..32621f0a 100644 --- a/src/main/java/space/space_spring/dto/VoiceRoom/RoomDto.java +++ b/src/main/java/space/space_spring/dto/VoiceRoom/RoomDto.java @@ -99,10 +99,7 @@ public void setActiveRoom(List liveKitRoomList){ boolean find = false; for(LivekitModels.Room resRoom : liveKitRoomList){ if(String.valueOf(this.id).equals( resRoom.getName() )){ - this.numParticipants = resRoom.getNumParticipants(); - this.sid = resRoom.getSid(); - this.metadata = resRoom.getMetadata(); - this.startTime= resRoom.getCreationTime(); + setActiveRoom(resRoom,true); find = true; break; } @@ -111,6 +108,19 @@ public void setActiveRoom(List liveKitRoomList){ if(!find){this.numParticipants=0;} //return this; } + + public void setActiveRoom(LivekitModels.Room resRoom,boolean checkId){ + if(checkId || String.valueOf(this.id).equals( resRoom.getName() )){ + this.numParticipants = resRoom.getNumParticipants(); + this.sid = resRoom.getSid(); + this.metadata = resRoom.getMetadata(); + this.startTime= resRoom.getCreationTime(); + + } + } + public void setActiveRoom(LivekitModels.Room resRoom){ + setActiveRoom(resRoom,false); + } private static boolean EqualRoomIdByNameTag(String roomName,long Id){ return roomName.endsWith("#"+String.valueOf(Id)); } diff --git a/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java b/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java index 80b5c293..32637ba9 100644 --- a/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java +++ b/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java @@ -14,100 +14,107 @@ import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class VoiceRoomDtoList { private List roomDtoList; - private final LiveKitUtils liveKitUtils; - private final VoiceRoomRepository voiceRoomRepository; - private final UserSpaceDao userSpaceDao; - private final UserDao userDao; - private final TaskExecutor taskExecutor; +// private final LiveKitUtils liveKitUtils; +// private final VoiceRoomRepository voiceRoomRepository; +// private final UserSpaceDao userSpaceDao; +// private final UserDao userDao; +// private final TaskExecutor taskExecutor; public void RoomDtoList(List roomDtos){ this.roomDtoList=roomDtos; } - public void setActiveRoom(){ - //#1 Response 받아오기 - - List roomResponsesTemp = liveKitUtils.getRoomList(); - - //불변 list로 변환 - List roomResponses = Collections.unmodifiableList(roomResponsesTemp); - /** - * 병렬 처리 적용대상 1 - */ - //#2 Room과 mapping 시키기 - for(RoomDto roomDto : roomDtoList){ - roomDto.setActiveRoom(roomResponses); + public void setActiveRoom(Map roomResponse){ + for(RoomDto room : this.roomDtoList){ + LivekitModels.Room resRoom= roomResponse.get(room.getId()); + room.setActiveRoom(resRoom); } - //ToDo setRoomDto 함수를 RoomDtoList 객체로 이동 - //todo 책임을 위임해도 이 병렬처리 코드가 잘 동작할까? - List> roomDtoFutureList = roomDtoList.stream() - .map(r->CompletableFuture.runAsync(()->setRoomDto(r,roomResponses,req),taskExecutor) - //.exceptionally(ex->{throws ex;}) - ) - .collect(Collectors.toList()); - - // 모든 Future의 완료를 기다림 - CompletableFuture allOf = CompletableFuture.allOf( - roomDtoFutureList.toArray(new CompletableFuture[0])); - - // 결과 수집 및 출력 - allOf.join(); - //ToDo Response로 convert - //#1 Active/inActive 분리 } - public void setRoomDto (RoomDto roomDto,List roomResponses,GetVoiceRoomList.Request req){ - roomDto.setActiveRoom(roomResponses); - - if(!req.isShowParticipant()){ - return; - } - - if(roomDto.getNumParticipants()==0){ - //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 - System.out.print("\n[DEBUG]Participant Number : 0\n"); - roomDto.setParticipantDTOList(Collections.emptyList()); - return; - } - //participantDto List 가져오기 - List participantDtoList = getParticipantDtoListById(roomDto.getId()); - - //RoomDto에 값 집어넣기 - //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 - if(participantDtoList==null||participantDtoList.isEmpty()){ - System.out.print("\n\n[DEBUG]participant response is empty or null"+participantDtoList.toString()+ - "participant number is \n\n"); - roomDto.setParticipantDTOList(Collections.emptyList()); - }else { - roomDto.setParticipantDTOList(participantDtoList); - } - - } - - private List getParticipantDtoListById(long voiceRoomId){ - Space space = voiceRoomRepository.findById(voiceRoomId).getSpace(); - List participantDtoList = liveKitUtils.getParticipantInfo(String.valueOf(voiceRoomId)); - if(participantDtoList==null||participantDtoList.isEmpty()){ - return Collections.emptyList(); - } - for(ParticipantDto participantDto: participantDtoList){ - //profileIamge 집어넣기 - participantDto.setProfileImage(findProfileImageByUserId(participantDto.getUserSpaceId())); - //userSpaceId 집어 넣기 - User user = userDao.findUserByUserId(participantDto.getId()); - participantDto.setUserSpaceId(userSpaceDao.findUserSpaceByUserAndSpace(user,space).get().getUserSpaceId()); - } - return participantDtoList; - } - private String findProfileImageByUserId(Long userSpaceId){ - return userSpaceDao.findProfileImageById(userSpaceId).orElse(""); - } +// public void setActiveRoom(List roomResponsesTemp){ +// //#1 Response 받아오기 +// +// //불변 list로 변환 +// List roomResponses = Collections.unmodifiableList(roomResponsesTemp); +// /** +// * 병렬 처리 적용대상 1 +// */ +// //#2 Room과 mapping 시키기 +// for(RoomDto roomDto : roomDtoList){ +// roomDto.setActiveRoom(roomResponses); +// } +// //ToDo setRoomDto 함수를 RoomDtoList 객체로 이동 +// //todo 책임을 위임해도 이 병렬처리 코드가 잘 동작할까? +// List> roomDtoFutureList = roomDtoList.stream() +// .map(r->CompletableFuture.runAsync(()->setRoomDto(r,roomResponses,req),taskExecutor) +// //.exceptionally(ex->{throws ex;}) +// ) +// .collect(Collectors.toList()); +// +// +// // 모든 Future의 완료를 기다림 +// CompletableFuture allOf = CompletableFuture.allOf( +// roomDtoFutureList.toArray(new CompletableFuture[0])); +// +// // 결과 수집 및 출력 +// allOf.join(); +// //ToDo Response로 convert +// //#1 Active/inActive 분리 +// } + +// public void setRoomDto (RoomDto roomDto,List roomResponses,GetVoiceRoomList.Request req){ +// roomDto.setActiveRoom(roomResponses); +// +// if(!req.isShowParticipant()){ +// return; +// } +// +// if(roomDto.getNumParticipants()==0){ +// //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 +// System.out.print("\n[DEBUG]Participant Number : 0\n"); +// roomDto.setParticipantDTOList(Collections.emptyList()); +// return; +// } +// //participantDto List 가져오기 +// List participantDtoList = getParticipantDtoListById(roomDto.getId()); +// +// //RoomDto에 값 집어넣기 +// //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 +// if(participantDtoList==null||participantDtoList.isEmpty()){ +// System.out.print("\n\n[DEBUG]participant response is empty or null"+participantDtoList.toString()+ +// "participant number is \n\n"); +// roomDto.setParticipantDTOList(Collections.emptyList()); +// }else { +// roomDto.setParticipantDTOList(participantDtoList); +// } +// +// } +// +// private List getParticipantDtoListById(long voiceRoomId){ +// Space space = voiceRoomRepository.findById(voiceRoomId).getSpace(); +// List participantDtoList = liveKitUtils.getParticipantInfo(String.valueOf(voiceRoomId)); +// if(participantDtoList==null||participantDtoList.isEmpty()){ +// return Collections.emptyList(); +// } +// for(ParticipantDto participantDto: participantDtoList){ +// //profileIamge 집어넣기 +// participantDto.setProfileImage(findProfileImageByUserId(participantDto.getUserSpaceId())); +// //userSpaceId 집어 넣기 +// User user = userDao.findUserByUserId(participantDto.getId()); +// participantDto.setUserSpaceId(userSpaceDao.findUserSpaceByUserAndSpace(user,space).get().getUserSpaceId()); +// } +// return participantDtoList; +// } +// private String findProfileImageByUserId(Long userSpaceId){ +// return userSpaceDao.findProfileImageById(userSpaceId).orElse(""); +// } } From 0d866e11f805f0204e05d8772d88c4d557db6fe0 Mon Sep 17 00:00:00 2001 From: kim_sang_ june <79149384+drbug2000@users.noreply.github.com> Date: Tue, 1 Oct 2024 15:43:04 +0900 Subject: [PATCH 06/11] [refactoring] add set RoomDtoList code apply on VoiceRoomService only set RoomDto not participant --- .../dto/VoiceRoom/GetVoiceRoomList.java | 2 + .../space_spring/dto/VoiceRoom/RoomDto.java | 2 + .../dto/VoiceRoom/VoiceRoomDtoList.java | 49 +++++++++++- .../space/space_spring/entity/VoiceRoom.java | 14 ++++ .../service/VoiceRoomService.java | 77 ++++++++++--------- 5 files changed, 105 insertions(+), 39 deletions(-) diff --git a/src/main/java/space/space_spring/dto/VoiceRoom/GetVoiceRoomList.java b/src/main/java/space/space_spring/dto/VoiceRoom/GetVoiceRoomList.java index e512885d..13edb903 100644 --- a/src/main/java/space/space_spring/dto/VoiceRoom/GetVoiceRoomList.java +++ b/src/main/java/space/space_spring/dto/VoiceRoom/GetVoiceRoomList.java @@ -52,6 +52,7 @@ public static VoiceRoomInfo convertRoomDto(RoomDto roomDto){ ) .build(); } + //Todo VoiceRoomDto로 이전예정 public static List convertRoomDtoList(List roomDtoList,Integer limit){ if(roomDtoList==null||roomDtoList.isEmpty()){return null;} Stream sortedStream = roomDtoList.stream() @@ -65,6 +66,7 @@ public static List convertRoomDtoList(List roomDtoList,I .collect(Collectors.toList()); } + //Todo VoiceRoomDto로 이전예정 public static List convertRoomDtoList(List roomDtoList) { return convertRoomDtoList(roomDtoList, null); } diff --git a/src/main/java/space/space_spring/dto/VoiceRoom/RoomDto.java b/src/main/java/space/space_spring/dto/VoiceRoom/RoomDto.java index 32621f0a..c7aadd5f 100644 --- a/src/main/java/space/space_spring/dto/VoiceRoom/RoomDto.java +++ b/src/main/java/space/space_spring/dto/VoiceRoom/RoomDto.java @@ -49,6 +49,7 @@ public static RoomDto convertRoom(LivekitModels.Room room){ .build(); } + //Todo VoiceRoomDtoList 로 이전 예정 public static List convertRoomDtoListByVoiceRoom(List voiceRoomList){ if(voiceRoomList==null||voiceRoomList.isEmpty()){return null;} return voiceRoomList.stream() @@ -110,6 +111,7 @@ public void setActiveRoom(List liveKitRoomList){ } public void setActiveRoom(LivekitModels.Room resRoom,boolean checkId){ + //null에 대한 예외 처리 if(checkId || String.valueOf(this.id).equals( resRoom.getName() )){ this.numParticipants = resRoom.getNumParticipants(); this.sid = resRoom.getSid(); diff --git a/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java b/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java index 32637ba9..58633d49 100644 --- a/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java +++ b/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java @@ -10,13 +10,17 @@ import space.space_spring.dao.VoiceRoomRepository; import space.space_spring.entity.Space; import space.space_spring.entity.User; +import space.space_spring.entity.VoiceRoom; import space.space_spring.util.LiveKitUtils; import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; +import java.util.stream.Stream; + @Service @RequiredArgsConstructor public class VoiceRoomDtoList { @@ -26,17 +30,56 @@ public class VoiceRoomDtoList { // private final UserSpaceDao userSpaceDao; // private final UserDao userDao; // private final TaskExecutor taskExecutor; - public void RoomDtoList(List roomDtos){ + public VoiceRoomDtoList(List roomDtos){ this.roomDtoList=roomDtos; } - public void setActiveRoom(Map roomResponse){ + public static VoiceRoomDtoList from(List voiceRoomEntityList){ + List roomDtos = convertRoomDtoListByVoiceRoom(voiceRoomEntityList); + return new VoiceRoomDtoList(roomDtos); + } + + public static List convertRoomDtoListByVoiceRoom(List voiceRoomList){ + if(voiceRoomList==null||voiceRoomList.isEmpty()){return null;} + return voiceRoomList.stream() + .map(RoomDto::convertRoom) + .collect(Collectors.toList()); + } + + private void setActiveRoom(Map roomResponse){ for(RoomDto room : this.roomDtoList){ - LivekitModels.Room resRoom= roomResponse.get(room.getId()); + LivekitModels.Room resRoom= roomResponse.get(String.valueOf(room.getId())); + if(resRoom==null){continue;} room.setActiveRoom(resRoom); } } + public void setActiveRoom(List roomResponse){ + Map roomResMap=roomResponse.stream() + .collect(Collectors.toMap( + res->res.getName(), + res->res, + (oldVal,newVal)->newVal + )); + roomResMap = Collections.unmodifiableMap(roomResMap); + setActiveRoom(roomResMap); + } + + public List convertVoicRoomInfoList(Integer limit){ + if(this.roomDtoList==null||this.roomDtoList.isEmpty()){return null;} + Stream sortedStream = this.roomDtoList.stream() + .sorted(Comparator + .comparing((RoomDto r) -> r.getNumParticipants() == 0) // Active rooms first + .thenComparing(RoomDto::getOrder)) ;// Then by order + System.out.print("limit input:"+limit); + Stream processedStream = (limit != null) ? sortedStream.limit(limit) : sortedStream; + + return processedStream.map(GetVoiceRoomList.VoiceRoomInfo::convertRoomDto) + .collect(Collectors.toList()); + } + public List convertVoicRoomInfoList() { + return convertVoicRoomInfoList( null); + } // public void setActiveRoom(List roomResponsesTemp){ // //#1 Response 받아오기 diff --git a/src/main/java/space/space_spring/entity/VoiceRoom.java b/src/main/java/space/space_spring/entity/VoiceRoom.java index aa93d303..2472dd92 100644 --- a/src/main/java/space/space_spring/entity/VoiceRoom.java +++ b/src/main/java/space/space_spring/entity/VoiceRoom.java @@ -6,6 +6,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import space.space_spring.dto.VoiceRoom.GetVoiceRoomList; +import space.space_spring.dto.VoiceRoom.RoomDto; @Entity @Table(name = "VoiceRoom") @@ -52,4 +53,17 @@ public void update(String name,Integer order){ } this.onUpdate(); } + + public RoomDto convertRoomDto(){ + return RoomDto.builder() + .name(this.getName()) + //.createdAt(voiceRoom.) + .id(this.getVoiceRoomId()) + .order(this.getOrder()) + .sid(null) + .metadata(null) + //.startTime() + .participantDTOList(null) + .build(); + } } diff --git a/src/main/java/space/space_spring/service/VoiceRoomService.java b/src/main/java/space/space_spring/service/VoiceRoomService.java index 8879cd8e..e49a7d45 100644 --- a/src/main/java/space/space_spring/service/VoiceRoomService.java +++ b/src/main/java/space/space_spring/service/VoiceRoomService.java @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.function.Function; @@ -126,12 +127,16 @@ public List getVoiceRoomInfoListConcurrency(long //todo voiceRoomEntityList 객체에 책임 위임 //todo RoomDtoList 일급 객체 생성 List roomDtoList = RoomDto.convertRoomDtoListByVoiceRoom(voiceRoomDataList); + //VoiceRoom과 Room mapping //#1 Response 받아오기 List roomResponsesTemp = liveKitUtils.getRoomList(); //불변 list로 변환 List roomResponses = Collections.unmodifiableList(roomResponsesTemp); + VoiceRoomDtoList voiceRoomDtoList=VoiceRoomDtoList.from(voiceRoomDataList); + voiceRoomDtoList.setActiveRoom(roomResponsesTemp); + /** * 병렬 처리 적용대상 1 */ @@ -159,9 +164,11 @@ public List getVoiceRoomInfoListConcurrency(long //#2 convert if(limit==null||limit<=0) { - return GetVoiceRoomList.VoiceRoomInfo.convertRoomDtoList(roomDtoList); + return voiceRoomDtoList.convertVoicRoomInfoList(); + //return GetVoiceRoomList.VoiceRoomInfo.convertRoomDtoList(roomDtoList); }else{ - return GetVoiceRoomList.VoiceRoomInfo.convertRoomDtoList(roomDtoList,limit); + return voiceRoomDtoList.convertVoicRoomInfoList(limit); + //return GetVoiceRoomList.VoiceRoomInfo.convertRoomDtoList(roomDtoList,limit); } //return null; } @@ -196,32 +203,32 @@ private void setRoomDto(RoomDto roomDto,List roomResponses,G - private Function setActiveParticipant= roomDto->{ - - if(roomDto.getNumParticipants()==0){ - //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 - System.out.print("\n[DEBUG]Participant Number : 0\n"); - roomDto.setParticipantDTOList(Collections.emptyList()); - - } - //participantDto List 가져오기 - List participantDtoList = this.getParticipantDtoListById(roomDto.getId()); -// for(ParticipantDto participantDto: participantDtoList){ -// //Todo profileIamge 집어넣기 -// participantDto.setProfileImage(findProfileImageByUserId(participantDto.getUserSpaceId())); -// } - //RoomDto에 값 집어넣기 - //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 - if(participantDtoList==null||participantDtoList.isEmpty()){ - System.out.print("\n\n[DEBUG]participant response is empty or null"+participantDtoList.toString()+ - "participant number is \n\n"); - roomDto.setParticipantDTOList(Collections.emptyList()); - }else { - roomDto.setParticipantDTOList(participantDtoList); - } - return roomDto; - - }; +// private Function setActiveParticipant= roomDto->{ +// +// if(roomDto.getNumParticipants()==0){ +// //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 +// System.out.print("\n[DEBUG]Participant Number : 0\n"); +// roomDto.setParticipantDTOList(Collections.emptyList()); +// +// } +// //participantDto List 가져오기 +// List participantDtoList = this.getParticipantDtoListById(roomDto.getId()); +//// for(ParticipantDto participantDto: participantDtoList){ +//// //Todo profileIamge 집어넣기 +//// participantDto.setProfileImage(findProfileImageByUserId(participantDto.getUserSpaceId())); +//// } +// //RoomDto에 값 집어넣기 +// //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 +// if(participantDtoList==null||participantDtoList.isEmpty()){ +// System.out.print("\n\n[DEBUG]participant response is empty or null"+participantDtoList.toString()+ +// "participant number is \n\n"); +// roomDto.setParticipantDTOList(Collections.emptyList()); +// }else { +// roomDto.setParticipantDTOList(participantDtoList); +// } +// return roomDto; +// +// }; public boolean updateVoiceRoom(List updateRoomList){ @@ -245,8 +252,6 @@ public void deleteVoiceRoom(long voiceRoomId){ VoiceRoom voiceRoom = voiceRoomRepository.findById(voiceRoomId); voiceRoom.updateInactive(); voiceRoomRepository.save(voiceRoom); - - } private String findProfileImageByUserId(Long userSpaceId){ @@ -298,12 +303,12 @@ public List getParticipantInfoListById(long //liveKitUtils.getParticipantInfo(findNameTagById(voiceRoomId)) ); } - private String findNameTagById(long id){ - VoiceRoom voiceRoom =voiceRoomRepository.findById(id); - //null pointer error 처리 - String name = voiceRoom.getName(); - return name+" #"+String.valueOf(id); - } +// private String findNameTagById(long id){ +// VoiceRoom voiceRoom =voiceRoomRepository.findById(id); +// //null pointer error 처리 +// String name = voiceRoom.getName(); +// return name+" #"+String.valueOf(id); +// } public String getToken(long spaceId,long userId,long userSpaceId,long voiceRoomId){ String userName=userSpaceDao.findUserNameById(userSpaceId); From 875bc1af1b61207b65a02a8c6ac0da8a7d04b342 Mon Sep 17 00:00:00 2001 From: kim_sang_ june <79149384+drbug2000@users.noreply.github.com> Date: Wed, 2 Oct 2024 17:05:14 +0900 Subject: [PATCH 07/11] [refactoring] delete not used import & func --- .../dto/VoiceRoom/VoiceRoomDtoList.java | 98 +------------------ .../service/VoiceRoomService.java | 6 +- 2 files changed, 5 insertions(+), 99 deletions(-) diff --git a/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java b/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java index 58633d49..708ce962 100644 --- a/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java +++ b/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java @@ -1,35 +1,19 @@ package space.space_spring.dto.VoiceRoom; import livekit.LivekitModels; -import lombok.RequiredArgsConstructor; -import org.springframework.core.task.TaskExecutor; -import org.springframework.stereotype.Service; -import space.space_spring.controller.VoiceRoomController; -import space.space_spring.dao.UserDao; -import space.space_spring.dao.UserSpaceDao; -import space.space_spring.dao.VoiceRoomRepository; -import space.space_spring.entity.Space; -import space.space_spring.entity.User; import space.space_spring.entity.VoiceRoom; -import space.space_spring.util.LiveKitUtils; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; -import java.util.concurrent.CompletableFuture; + import java.util.stream.Collectors; import java.util.stream.Stream; -@Service -@RequiredArgsConstructor public class VoiceRoomDtoList { private List roomDtoList; -// private final LiveKitUtils liveKitUtils; -// private final VoiceRoomRepository voiceRoomRepository; -// private final UserSpaceDao userSpaceDao; -// private final UserDao userDao; -// private final TaskExecutor taskExecutor; + public VoiceRoomDtoList(List roomDtos){ this.roomDtoList=roomDtos; } @@ -81,83 +65,5 @@ public List convertVoicRoomInfoList() { return convertVoicRoomInfoList( null); } -// public void setActiveRoom(List roomResponsesTemp){ -// //#1 Response 받아오기 -// -// //불변 list로 변환 -// List roomResponses = Collections.unmodifiableList(roomResponsesTemp); -// /** -// * 병렬 처리 적용대상 1 -// */ -// //#2 Room과 mapping 시키기 -// for(RoomDto roomDto : roomDtoList){ -// roomDto.setActiveRoom(roomResponses); -// } -// //ToDo setRoomDto 함수를 RoomDtoList 객체로 이동 -// //todo 책임을 위임해도 이 병렬처리 코드가 잘 동작할까? -// List> roomDtoFutureList = roomDtoList.stream() -// .map(r->CompletableFuture.runAsync(()->setRoomDto(r,roomResponses,req),taskExecutor) -// //.exceptionally(ex->{throws ex;}) -// ) -// .collect(Collectors.toList()); -// -// -// // 모든 Future의 완료를 기다림 -// CompletableFuture allOf = CompletableFuture.allOf( -// roomDtoFutureList.toArray(new CompletableFuture[0])); -// -// // 결과 수집 및 출력 -// allOf.join(); -// //ToDo Response로 convert -// //#1 Active/inActive 분리 -// } - -// public void setRoomDto (RoomDto roomDto,List roomResponses,GetVoiceRoomList.Request req){ -// roomDto.setActiveRoom(roomResponses); -// -// if(!req.isShowParticipant()){ -// return; -// } -// -// if(roomDto.getNumParticipants()==0){ -// //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 -// System.out.print("\n[DEBUG]Participant Number : 0\n"); -// roomDto.setParticipantDTOList(Collections.emptyList()); -// return; -// } -// //participantDto List 가져오기 -// List participantDtoList = getParticipantDtoListById(roomDto.getId()); -// -// //RoomDto에 값 집어넣기 -// //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 -// if(participantDtoList==null||participantDtoList.isEmpty()){ -// System.out.print("\n\n[DEBUG]participant response is empty or null"+participantDtoList.toString()+ -// "participant number is \n\n"); -// roomDto.setParticipantDTOList(Collections.emptyList()); -// }else { -// roomDto.setParticipantDTOList(participantDtoList); -// } -// -// } -// -// private List getParticipantDtoListById(long voiceRoomId){ -// Space space = voiceRoomRepository.findById(voiceRoomId).getSpace(); -// List participantDtoList = liveKitUtils.getParticipantInfo(String.valueOf(voiceRoomId)); -// if(participantDtoList==null||participantDtoList.isEmpty()){ -// return Collections.emptyList(); -// } -// for(ParticipantDto participantDto: participantDtoList){ -// //profileIamge 집어넣기 -// participantDto.setProfileImage(findProfileImageByUserId(participantDto.getUserSpaceId())); -// //userSpaceId 집어 넣기 -// User user = userDao.findUserByUserId(participantDto.getId()); -// participantDto.setUserSpaceId(userSpaceDao.findUserSpaceByUserAndSpace(user,space).get().getUserSpaceId()); -// } -// return participantDtoList; -// } -// private String findProfileImageByUserId(Long userSpaceId){ -// return userSpaceDao.findProfileImageById(userSpaceId).orElse(""); -// } - } diff --git a/src/main/java/space/space_spring/service/VoiceRoomService.java b/src/main/java/space/space_spring/service/VoiceRoomService.java index e49a7d45..5fc9acc2 100644 --- a/src/main/java/space/space_spring/service/VoiceRoomService.java +++ b/src/main/java/space/space_spring/service/VoiceRoomService.java @@ -134,16 +134,16 @@ public List getVoiceRoomInfoListConcurrency(long //불변 list로 변환 List roomResponses = Collections.unmodifiableList(roomResponsesTemp); + VoiceRoomDtoList voiceRoomDtoList=VoiceRoomDtoList.from(voiceRoomDataList); voiceRoomDtoList.setActiveRoom(roomResponsesTemp); - /** - * 병렬 처리 적용대상 1 - */ + //Todo 대체됨. 관련 함수 삭제 예정 //#2 Room과 mapping 시키기 for(RoomDto roomDto : roomDtoList){ roomDto.setActiveRoom(roomResponses); } + //ToDo setRoomDto 함수를 RoomDtoList 객체로 이동 //todo 책임을 위임해도 이 병렬처리 코드가 잘 동작할까? List> roomDtoFutureList = roomDtoList.stream() From c47b3fd1c9c256555194882e203ac778b99c2c5f Mon Sep 17 00:00:00 2001 From: kim_sang_ june <79149384+drbug2000@users.noreply.github.com> Date: Sat, 5 Oct 2024 00:31:16 +0900 Subject: [PATCH 08/11] [refactoring] Remove unused function in voiceRoom module --- .../controller/VoiceRoomController.java | 18 +-- .../dto/VoiceRoom/GetVoiceRoomList.java | 18 --- .../space_spring/dto/VoiceRoom/RoomDto.java | 11 +- .../service/VoiceRoomService.java | 114 +----------------- .../concurrency/completableFutureTest.java | 11 ++ 5 files changed, 18 insertions(+), 154 deletions(-) diff --git a/src/main/java/space/space_spring/controller/VoiceRoomController.java b/src/main/java/space/space_spring/controller/VoiceRoomController.java index c2aad928..5b7dd483 100644 --- a/src/main/java/space/space_spring/controller/VoiceRoomController.java +++ b/src/main/java/space/space_spring/controller/VoiceRoomController.java @@ -85,26 +85,16 @@ public BaseResponse getRoomListNonConCurrent( @JwtLoginAuth Long userId, //@RequestBody GetVoiceRoomList.Request voiceRoomList, @RequestParam(required = false,defaultValue = "0") Integer limit, - @RequestParam(required = false,defaultValue = "false") Boolean showParticipant, - @PathVariable("version") Integer version){ + @RequestParam(required = false,defaultValue = "false") Boolean showParticipant + ){ boolean showParticipantValue = (showParticipant != null) ? showParticipant : false; GetVoiceRoomList.Request voiceRoomList=new GetVoiceRoomList.Request(limit, showParticipant); List roomInfoList; - if(version==null){ - version =1; - } - if(version==1){ - roomInfoList= voiceRoomService.getVoiceRoomInfoListConcurrency(spaceId,voiceRoomList); - } - if(version==2){ - roomInfoList = voiceRoomService.getVoiceRoomInfoList(spaceId,voiceRoomList); - } - else{ - roomInfoList= voiceRoomService.getVoiceRoomInfoListConcurrency(spaceId,voiceRoomList); - } + + roomInfoList= voiceRoomService.getVoiceRoomInfoListConcurrency(spaceId,voiceRoomList); return new BaseResponse(new GetVoiceRoomList.Response(roomInfoList)); } diff --git a/src/main/java/space/space_spring/dto/VoiceRoom/GetVoiceRoomList.java b/src/main/java/space/space_spring/dto/VoiceRoom/GetVoiceRoomList.java index 13edb903..40661862 100644 --- a/src/main/java/space/space_spring/dto/VoiceRoom/GetVoiceRoomList.java +++ b/src/main/java/space/space_spring/dto/VoiceRoom/GetVoiceRoomList.java @@ -52,23 +52,5 @@ public static VoiceRoomInfo convertRoomDto(RoomDto roomDto){ ) .build(); } - //Todo VoiceRoomDto로 이전예정 - public static List convertRoomDtoList(List roomDtoList,Integer limit){ - if(roomDtoList==null||roomDtoList.isEmpty()){return null;} - Stream sortedStream = roomDtoList.stream() - .sorted(Comparator - .comparing((RoomDto r) -> r.getNumParticipants() == 0) // Active rooms first - .thenComparing(RoomDto::getOrder)) ;// Then by order - System.out.print("limit input:"+limit); - Stream processedStream = (limit != null) ? sortedStream.limit(limit) : sortedStream; - - return processedStream.map(VoiceRoomInfo::convertRoomDto) - .collect(Collectors.toList()); - - } - //Todo VoiceRoomDto로 이전예정 - public static List convertRoomDtoList(List roomDtoList) { - return convertRoomDtoList(roomDtoList, null); - } } } diff --git a/src/main/java/space/space_spring/dto/VoiceRoom/RoomDto.java b/src/main/java/space/space_spring/dto/VoiceRoom/RoomDto.java index c7aadd5f..9e0a530c 100644 --- a/src/main/java/space/space_spring/dto/VoiceRoom/RoomDto.java +++ b/src/main/java/space/space_spring/dto/VoiceRoom/RoomDto.java @@ -56,16 +56,7 @@ public static List convertRoomDtoListByVoiceRoom(List voiceR .map(RoomDto::convertRoom) .collect(Collectors.toList()); } -// public static Room convertRoom(LiveKitSession session){ -// return Room.builder() -// .name(session.) -// .numParticipants(room.getNumParticipants()) -// .creationTime(room.getCreationTime()) -// .sid(room.getSid()) -// .metadata(room.getMetadata()) -// .participantList(null) -// .build(); -// } + public static RoomDto convertRoom(VoiceRoom voiceRoom){ if(voiceRoom==null){return null;} return RoomDto.builder() diff --git a/src/main/java/space/space_spring/service/VoiceRoomService.java b/src/main/java/space/space_spring/service/VoiceRoomService.java index 5fc9acc2..5da558ac 100644 --- a/src/main/java/space/space_spring/service/VoiceRoomService.java +++ b/src/main/java/space/space_spring/service/VoiceRoomService.java @@ -55,61 +55,6 @@ public Long createVoiceRoom(long spaceId,PostVoiceRoomDto.Request req){ return voiceRoomDao.createVoiceRoom(name, order, targetSpace); } - public List getVoiceRoomInfoList(long spaceId,GetVoiceRoomList.Request req){ - Integer limit = req.getLimit(); - boolean showParticipant =req.isShowParticipant(); - - - //해당 space VoiceRoom 가져오기 (VoiceRoom List) - //Todo 가져오기에 limit 적용 - List voiceRoomDataList = findBySpaceId(spaceId); - List roomDtoList = RoomDto.convertRoomDtoListByVoiceRoom(voiceRoomDataList); - //VoiceRoom과 Room mapping - //#1 Response 받아오기 - List roomResponses = liveKitUtils.getRoomList(); - //#2 Room과 mapping 시키기 - for(RoomDto roomDto : roomDtoList){ - roomDto.setActiveRoom(roomResponses); - } - //participant mapping - if (showParticipant) { - for(RoomDto roomDto : roomDtoList) { - if(roomDto.getNumParticipants()==0){ - //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 - System.out.print("\n[DEBUG]Participant Number : 0\n"); - roomDto.setParticipantDTOList(Collections.emptyList()); - continue; - } - //participantDto List 가져오기 - List participantDtoList = getParticipantDtoListById(roomDto.getId()); - for(ParticipantDto participantDto: participantDtoList){ - //Todo profileIamge 집어넣기 - participantDto.setProfileImage(findProfileImageByUserId(participantDto.getUserSpaceId())); - } - //RoomDto에 값 집어넣기 - //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 - if(participantDtoList==null||participantDtoList.isEmpty()){ - System.out.print("\n\n[DEBUG]participant response is empty or null"+participantDtoList.toString()+ - "participant number is \n\n"); - roomDto.setParticipantDTOList(Collections.emptyList()); - }else { - roomDto.setParticipantDTOList(participantDtoList); - } - } - } - //ToDo Response로 convert - //#1 Active/inActive 분리 - - //#2 convert - if(limit==null||limit<=0) { - return GetVoiceRoomList.VoiceRoomInfo.convertRoomDtoList(roomDtoList); - }else{ - return GetVoiceRoomList.VoiceRoomInfo.convertRoomDtoList(roomDtoList,limit); - } - //return null; - } - - /** @@ -138,11 +83,6 @@ public List getVoiceRoomInfoListConcurrency(long VoiceRoomDtoList voiceRoomDtoList=VoiceRoomDtoList.from(voiceRoomDataList); voiceRoomDtoList.setActiveRoom(roomResponsesTemp); - //Todo 대체됨. 관련 함수 삭제 예정 - //#2 Room과 mapping 시키기 - for(RoomDto roomDto : roomDtoList){ - roomDto.setActiveRoom(roomResponses); - } //ToDo setRoomDto 함수를 RoomDtoList 객체로 이동 //todo 책임을 위임해도 이 병렬처리 코드가 잘 동작할까? @@ -172,6 +112,7 @@ public List getVoiceRoomInfoListConcurrency(long } //return null; } + //todo 해당 함수의 책임을 RoomDto에게 위임 private void setRoomDto(RoomDto roomDto,List roomResponses,GetVoiceRoomList.Request req){ roomDto.setActiveRoom(roomResponses); @@ -202,34 +143,6 @@ private void setRoomDto(RoomDto roomDto,List roomResponses,G } - -// private Function setActiveParticipant= roomDto->{ -// -// if(roomDto.getNumParticipants()==0){ -// //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 -// System.out.print("\n[DEBUG]Participant Number : 0\n"); -// roomDto.setParticipantDTOList(Collections.emptyList()); -// -// } -// //participantDto List 가져오기 -// List participantDtoList = this.getParticipantDtoListById(roomDto.getId()); -//// for(ParticipantDto participantDto: participantDtoList){ -//// //Todo profileIamge 집어넣기 -//// participantDto.setProfileImage(findProfileImageByUserId(participantDto.getUserSpaceId())); -//// } -// //RoomDto에 값 집어넣기 -// //showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력 -// if(participantDtoList==null||participantDtoList.isEmpty()){ -// System.out.print("\n\n[DEBUG]participant response is empty or null"+participantDtoList.toString()+ -// "participant number is \n\n"); -// roomDto.setParticipantDTOList(Collections.emptyList()); -// }else { -// roomDto.setParticipantDTOList(participantDtoList); -// } -// return roomDto; -// -// }; - public boolean updateVoiceRoom(List updateRoomList){ //Todo 입력된 order가 유효한지 확인 필요 @@ -278,24 +191,7 @@ private List getParticipantDtoListById(long voiceRoomId){ } return participantDtoList; } -// -// private static List getParticipantDtoListById(long voiceRoomId){ -// -// -// Space space = voiceRoomRepository.findById(voiceRoomId).getSpace(); -// List participantDtoList = liveKitUtils.getParticipantInfo(String.valueOf(voiceRoomId)); -// if(participantDtoList==null||participantDtoList.isEmpty()){ -// return Collections.emptyList(); -// } -// for(ParticipantDto participantDto: participantDtoList){ -// //profileIamge 집어넣기 -// participantDto.setProfileImage(findProfileImageByUserId(participantDto.getUserSpaceId())); -// //userSpaceId 집어 넣기 -// User user = userDao.findUserByUserId(participantDto.getId()); -// participantDto.setUserSpaceId(userSpaceDao.findUserSpaceByUserAndSpace(user,space).get().getUserSpaceId()); -// } -// return participantDtoList; -// } + public List getParticipantInfoListById(long voiceRoomId){ return GetParticipantList.ParticipantInfo.convertParticipantDtoList( @@ -303,12 +199,6 @@ public List getParticipantInfoListById(long //liveKitUtils.getParticipantInfo(findNameTagById(voiceRoomId)) ); } -// private String findNameTagById(long id){ -// VoiceRoom voiceRoom =voiceRoomRepository.findById(id); -// //null pointer error 처리 -// String name = voiceRoom.getName(); -// return name+" #"+String.valueOf(id); -// } public String getToken(long spaceId,long userId,long userSpaceId,long voiceRoomId){ String userName=userSpaceDao.findUserNameById(userSpaceId); diff --git a/src/test/java/space/space_spring/concurrency/completableFutureTest.java b/src/test/java/space/space_spring/concurrency/completableFutureTest.java index fadcb118..db951e46 100644 --- a/src/test/java/space/space_spring/concurrency/completableFutureTest.java +++ b/src/test/java/space/space_spring/concurrency/completableFutureTest.java @@ -81,6 +81,17 @@ public static List> processStringsAsync(List .collect(Collectors.toList()); } + @Test + void supplyAsyncb() throws ExecutionException, InterruptedException { + + CompletableFuture future = CompletableFuture.supplyAsync(() -> { + System.out.println("Thread task start"); + return "Thread: " + Thread.currentThread().getName(); + }); + + //System.out.println(future.get()); + System.out.println("Thread: " + Thread.currentThread().getName()); + } From 08a841b5496d73219662e20036ea7df286394130 Mon Sep 17 00:00:00 2001 From: kim_sang_ june <79149384+drbug2000@users.noreply.github.com> Date: Mon, 7 Oct 2024 03:32:35 +0900 Subject: [PATCH 09/11] [fix] PostVoiceRoom.Request refactoring --- .../space/space_spring/dto/VoiceRoom/PostVoiceRoomDto.java | 5 ++++- .../space_spring/domain/VoiceRoom/VoiceRoomServiceTest.java | 6 ++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/space/space_spring/dto/VoiceRoom/PostVoiceRoomDto.java b/src/main/java/space/space_spring/dto/VoiceRoom/PostVoiceRoomDto.java index 833df893..0395b5c6 100644 --- a/src/main/java/space/space_spring/dto/VoiceRoom/PostVoiceRoomDto.java +++ b/src/main/java/space/space_spring/dto/VoiceRoom/PostVoiceRoomDto.java @@ -4,10 +4,13 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; public class PostVoiceRoomDto { @Getter - @Builder + //@Builder + @AllArgsConstructor + @NoArgsConstructor public static class Request{ @NotNull(message = "name is mandatory") private String name; diff --git a/src/test/java/space/space_spring/domain/VoiceRoom/VoiceRoomServiceTest.java b/src/test/java/space/space_spring/domain/VoiceRoom/VoiceRoomServiceTest.java index 91f95083..b7c88409 100644 --- a/src/test/java/space/space_spring/domain/VoiceRoom/VoiceRoomServiceTest.java +++ b/src/test/java/space/space_spring/domain/VoiceRoom/VoiceRoomServiceTest.java @@ -9,7 +9,6 @@ import org.mockito.Mockito.*; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.util.ReflectionTestUtils; -import space.space_spring.dao.UserDao; import space.space_spring.dao.UserSpaceDao; import space.space_spring.dao.VoiceRoomDao; import space.space_spring.dao.VoiceRoomRepository; @@ -44,8 +43,7 @@ public class VoiceRoomServiceTest { @Mock private LiveKitUtils liveKitUtils; - @Mock - private UserDao userDao; + @Mock private UserSpaceDao userSpaceDao; @@ -133,7 +131,7 @@ void voiceRoom_create_test(){ when(voiceRoomDao.createVoiceRoom(voiceRoomName,spaceOrder+1,testSpace)).thenReturn(1L); - PostVoiceRoomDto.Request request = PostVoiceRoomDto.Request.builder().name(voiceRoomName).build(); + PostVoiceRoomDto.Request request = new PostVoiceRoomDto.Request(voiceRoomName); Long voiceRoomId = voiceRoomService.createVoiceRoom(testSpace.getSpaceId(),request); //then From dfb2a271301246e135753d729cbecda0aafc219a Mon Sep 17 00:00:00 2001 From: kim_sang_ june <79149384+drbug2000@users.noreply.github.com> Date: Mon, 7 Oct 2024 03:33:32 +0900 Subject: [PATCH 10/11] [refactoring] add id base dao in voiceRoom and UserSpace --- .../space_spring/dao/UserSpaceRepository.java | 16 ++++++++++++++++ .../space_spring/dao/VoiceRoomRepository.java | 6 ++++++ .../space_spring/service/VoiceRoomService.java | 16 +++++++++------- 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 src/main/java/space/space_spring/dao/UserSpaceRepository.java diff --git a/src/main/java/space/space_spring/dao/UserSpaceRepository.java b/src/main/java/space/space_spring/dao/UserSpaceRepository.java new file mode 100644 index 00000000..6e3383c9 --- /dev/null +++ b/src/main/java/space/space_spring/dao/UserSpaceRepository.java @@ -0,0 +1,16 @@ +package space.space_spring.dao; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import space.space_spring.entity.UserSpace; +import space.space_spring.entity.VoiceRoom; + +import java.util.List; +import java.util.Optional; + +public interface UserSpaceRepository extends JpaRepository { + @Query("SELECT r FROM UserSpace r WHERE r.user.userId = :userId AND r.space.spaceId = :spaceId AND r.status = 'ACTIVE'") + Optional findUserSpaceByUserAndSpace(@Param("userId") Long userId, @Param("spaceId") Long spaceId); + +} diff --git a/src/main/java/space/space_spring/dao/VoiceRoomRepository.java b/src/main/java/space/space_spring/dao/VoiceRoomRepository.java index d3cea243..fc169ff7 100644 --- a/src/main/java/space/space_spring/dao/VoiceRoomRepository.java +++ b/src/main/java/space/space_spring/dao/VoiceRoomRepository.java @@ -16,10 +16,16 @@ public interface VoiceRoomRepository extends JpaRepository { List findBySpace(@Param("space")Space space); @Query("SELECT MAX(r.order) FROM VoiceRoom r WHERE r.space = :space AND r.status = 'ACTIVE'") Integer findMaxOrderBySpace(@Param("space") Space space); + + @Query("SELECT MAX(r.order) FROM VoiceRoom r WHERE r.space.id = :spaceId AND r.status = 'ACTIVE'") + Integer findMaxOrderBySpaceId(@Param("spaceId") Long spaceId); @Query("SELECT CASE WHEN COUNT(v) > 0 THEN true ELSE false END FROM VoiceRoom v WHERE v.voiceRoomId = :id AND v.status = 'ACTIVE'") boolean existsByVoiceRoomId(@Param("id")long voiceRoomId); @Query("SELECT CASE WHEN COUNT(v) > 0 THEN true ELSE false END FROM VoiceRoom v WHERE v.name = :voiceRoomName AND v.status = 'ACTIVE'") boolean existsByName(@Param("voiceRoomName") String voiceRoomName); @Query("SELECT v FROM VoiceRoom v WHERE v.voiceRoomId = :id AND v.status = 'ACTIVE'") VoiceRoom findById(@Param("id") long Id); + + @Query("SELECT v FROM VoiceRoom v WHERE v.space.id = :spaceId AND v.status = 'ACTIVE'") + List findActiveVoiceRoomsBySpaceId(@Param("spaceId") Long spaceId); } diff --git a/src/main/java/space/space_spring/service/VoiceRoomService.java b/src/main/java/space/space_spring/service/VoiceRoomService.java index 2351175b..e6e726b7 100644 --- a/src/main/java/space/space_spring/service/VoiceRoomService.java +++ b/src/main/java/space/space_spring/service/VoiceRoomService.java @@ -38,6 +38,7 @@ public class VoiceRoomService { public Long createVoiceRoom(long spaceId,PostVoiceRoomDto.Request req){ Space targetSpace = spaceUtils.findSpaceBySpaceId(spaceId); Integer orderInt = voiceRoomRepository.findMaxOrderBySpace(targetSpace); + //Space currentSpace=voiceRoomRepository.findActiveVoiceRoomsBySpaceId(spaceId); int order; if(orderInt==null||orderInt==0){ order=1; @@ -62,7 +63,8 @@ public List getVoiceRoomInfoListConcurrency(long //해당 space VoiceRoom 가져오기 (VoiceRoom List) //Todo 가져오기에 limit 적용 //todo 일급 객체 `voiceRoomEntityList`로 변경 - List voiceRoomDataList = findBySpaceId(spaceId); + //List voiceRoomDataList = findBySpaceId(spaceId); + List voiceRoomDataList = voiceRoomRepository.findActiveVoiceRoomsBySpaceId(spaceId); //todo voiceRoomEntityList 객체에 책임 위임 //todo RoomDtoList 일급 객체 생성 List roomDtoList = RoomDto.convertRoomDtoListByVoiceRoom(voiceRoomDataList); @@ -164,12 +166,12 @@ public void deleteVoiceRoom(long voiceRoomId){ private String findProfileImageByUserId(Long userSpaceId){ return userSpaceDao.findProfileImageById(userSpaceId).orElse(""); } - public List findBySpaceId(long spaceId){ - return findBySpace(spaceUtils.findSpaceBySpaceId(spaceId)); - } - private List findBySpace(Space space){ - return voiceRoomRepository.findBySpace(space); - } +// public List findBySpaceId(long spaceId){ +// return findBySpace(spaceUtils.findSpaceBySpaceId(spaceId)); +// } +// private List findBySpace(Space space){ +// return voiceRoomRepository.findBySpace(space); +// } private List getParticipantDtoListById(long voiceRoomId){ Space space = voiceRoomRepository.findById(voiceRoomId).getSpace(); List participantDtoList = liveKitUtils.getParticipantInfo(String.valueOf(voiceRoomId)); From 6b9348cafe6a6e46329b3124c20bcb7de59cf175 Mon Sep 17 00:00:00 2001 From: kim_sang_ june <79149384+drbug2000@users.noreply.github.com> Date: Tue, 8 Oct 2024 20:37:44 +0900 Subject: [PATCH 11/11] [refactoring] rename VoicRoomDtoList->VoiceRoomListDto --- .../{VoiceRoomDtoList.java => VoiceRoomListDto.java} | 8 ++++---- .../java/space/space_spring/service/VoiceRoomService.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) rename src/main/java/space/space_spring/dto/VoiceRoom/{VoiceRoomDtoList.java => VoiceRoomListDto.java} (92%) diff --git a/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java b/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomListDto.java similarity index 92% rename from src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java rename to src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomListDto.java index 708ce962..ffc5ad9e 100644 --- a/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomDtoList.java +++ b/src/main/java/space/space_spring/dto/VoiceRoom/VoiceRoomListDto.java @@ -11,16 +11,16 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -public class VoiceRoomDtoList { +public class VoiceRoomListDto { private List roomDtoList; - public VoiceRoomDtoList(List roomDtos){ + public VoiceRoomListDto(List roomDtos){ this.roomDtoList=roomDtos; } - public static VoiceRoomDtoList from(List voiceRoomEntityList){ + public static VoiceRoomListDto from(List voiceRoomEntityList){ List roomDtos = convertRoomDtoListByVoiceRoom(voiceRoomEntityList); - return new VoiceRoomDtoList(roomDtos); + return new VoiceRoomListDto(roomDtos); } public static List convertRoomDtoListByVoiceRoom(List voiceRoomList){ diff --git a/src/main/java/space/space_spring/service/VoiceRoomService.java b/src/main/java/space/space_spring/service/VoiceRoomService.java index e6e726b7..09ab1d90 100644 --- a/src/main/java/space/space_spring/service/VoiceRoomService.java +++ b/src/main/java/space/space_spring/service/VoiceRoomService.java @@ -76,8 +76,8 @@ public List getVoiceRoomInfoListConcurrency(long //불변 list로 변환 List roomResponses = Collections.unmodifiableList(roomResponsesTemp); - VoiceRoomDtoList voiceRoomDtoList=VoiceRoomDtoList.from(voiceRoomDataList); - voiceRoomDtoList.setActiveRoom(roomResponsesTemp); + VoiceRoomListDto voiceRoomListDto = VoiceRoomListDto.from(voiceRoomDataList); + voiceRoomListDto.setActiveRoom(roomResponsesTemp); //ToDo setRoomDto 함수를 RoomDtoList 객체로 이동 @@ -100,10 +100,10 @@ public List getVoiceRoomInfoListConcurrency(long //#2 convert if(limit==null||limit<=0) { - return voiceRoomDtoList.convertVoicRoomInfoList(); + return voiceRoomListDto.convertVoicRoomInfoList(); //return GetVoiceRoomList.VoiceRoomInfo.convertRoomDtoList(roomDtoList); }else{ - return voiceRoomDtoList.convertVoicRoomInfoList(limit); + return voiceRoomListDto.convertVoicRoomInfoList(limit); //return GetVoiceRoomList.VoiceRoomInfo.convertRoomDtoList(roomDtoList,limit); } //return null;