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

Refactoring voice room 객체 정리 및 미사용 코드 삭제 #162

Merged
merged 13 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,16 @@ public BaseResponse<GetVoiceRoomList.Response> 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<GetVoiceRoomList.VoiceRoomInfo> 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);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

비즈니스 로직을 전부 서비스단으로 넘겨버리니 컨트롤러의 코드가 훨씬 깔끔해진것 같습니다!!

return new BaseResponse<GetVoiceRoomList.Response>(new GetVoiceRoomList.Response(roomInfoList));
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/space/space_spring/dao/UserSpaceRepository.java
Original file line number Diff line number Diff line change
@@ -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<UserSpace, Long> {
@Query("SELECT r FROM UserSpace r WHERE r.user.userId = :userId AND r.space.spaceId = :spaceId AND r.status = 'ACTIVE'")
Optional<UserSpace> findUserSpaceByUserAndSpace(@Param("userId") Long userId, @Param("spaceId") Long spaceId);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 UserSpaceDao의 코드를 작성하신 UserSpaceRepository 로 넘기도록 하겠습니다

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 다만, 꼭 id 값으로 Entity를 가져오는 것이 정답은 아닌 것 같아서 고민중입니다.
객체 간의 상관관계를 명확히 하는 용도로 Entity를 따로 가져오는 경우도 있다고 합니다.
추가적으로 함수이름에 Id를 포함시켜 헷갈리지 않게 수정해야겠네요

}
6 changes: 6 additions & 0 deletions src/main/java/space/space_spring/dao/VoiceRoomRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ public interface VoiceRoomRepository extends JpaRepository<VoiceRoom,Long> {
List<VoiceRoom> 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<VoiceRoom> findActiveVoiceRoomsBySpaceId(@Param("spaceId") Long spaceId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,5 @@ public static VoiceRoomInfo convertRoomDto(RoomDto roomDto){
)
.build();
}
public static List<VoiceRoomInfo> convertRoomDtoList(List<RoomDto> roomDtoList,Integer limit){
if(roomDtoList==null||roomDtoList.isEmpty()){return null;}
Stream<RoomDto> 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<RoomDto> processedStream = (limit != null) ? sortedStream.limit(limit) : sortedStream;

return processedStream.map(VoiceRoomInfo::convertRoomDto)
.collect(Collectors.toList());

}
public static List<VoiceRoomInfo> convertRoomDtoList(List<RoomDto> roomDtoList) {
return convertRoomDtoList(roomDtoList, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

public class PostVoiceRoomDto {
@Getter
//@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class Request{
@NotNull(message = "name is mandatory")
private String name;
Expand Down
31 changes: 17 additions & 14 deletions src/main/java/space/space_spring/dto/VoiceRoom/RoomDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,14 @@ public static RoomDto convertRoom(LivekitModels.Room room){
.build();
}

//Todo VoiceRoomDtoList 로 이전 예정
public static List<RoomDto> convertRoomDtoListByVoiceRoom(List<VoiceRoom> voiceRoomList){
if(voiceRoomList==null||voiceRoomList.isEmpty()){return null;}
return voiceRoomList.stream()
.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()
Expand Down Expand Up @@ -99,10 +91,7 @@ public void setActiveRoom(List<LivekitModels.Room> 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;
}
Expand All @@ -111,6 +100,20 @@ public void setActiveRoom(List<LivekitModels.Room> liveKitRoomList){
if(!find){this.numParticipants=0;}
//return this;
}

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();
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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<VoiceRoom> voiceRoomEntityList;

public void VoiceRoomList(List<VoiceRoom> voiceRoomEntityList) {
this.voiceRoomEntityList = voiceRoomEntityList;
}

public List<RoomDto> convertRoomDto(){
if(this.voiceRoomEntityList==null||this.voiceRoomEntityList.isEmpty()){return null;}
return this.voiceRoomEntityList.stream()
.map(RoomDto::convertRoom)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package space.space_spring.dto.VoiceRoom;

import livekit.LivekitModels;
import space.space_spring.entity.VoiceRoom;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class VoiceRoomListDto {
private List<RoomDto> roomDtoList;

public VoiceRoomListDto(List<RoomDto> roomDtos){
this.roomDtoList=roomDtos;
}

public static VoiceRoomListDto from(List<VoiceRoom> voiceRoomEntityList){
List<RoomDto> roomDtos = convertRoomDtoListByVoiceRoom(voiceRoomEntityList);
return new VoiceRoomListDto(roomDtos);
}

public static List<RoomDto> convertRoomDtoListByVoiceRoom(List<VoiceRoom> voiceRoomList){
if(voiceRoomList==null||voiceRoomList.isEmpty()){return null;}
return voiceRoomList.stream()
.map(RoomDto::convertRoom)
.collect(Collectors.toList());
}

private void setActiveRoom(Map<String,LivekitModels.Room> roomResponse){
for(RoomDto room : this.roomDtoList){
LivekitModels.Room resRoom= roomResponse.get(String.valueOf(room.getId()));
if(resRoom==null){continue;}
room.setActiveRoom(resRoom);
}

}
public void setActiveRoom(List<LivekitModels.Room> roomResponse){
Map<String,LivekitModels.Room> roomResMap=roomResponse.stream()
.collect(Collectors.toMap(
res->res.getName(),
res->res,
(oldVal,newVal)->newVal
));
roomResMap = Collections.unmodifiableMap(roomResMap);
setActiveRoom(roomResMap);
}

public List<GetVoiceRoomList.VoiceRoomInfo> convertVoicRoomInfoList(Integer limit){
if(this.roomDtoList==null||this.roomDtoList.isEmpty()){return null;}
Stream<RoomDto> 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<RoomDto> processedStream = (limit != null) ? sortedStream.limit(limit) : sortedStream;

return processedStream.map(GetVoiceRoomList.VoiceRoomInfo::convertRoomDto)
.collect(Collectors.toList());
}
public List<GetVoiceRoomList.VoiceRoomInfo> convertVoicRoomInfoList() {
return convertVoicRoomInfoList( null);
}


}
14 changes: 14 additions & 0 deletions src/main/java/space/space_spring/entity/VoiceRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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();
}
}
Loading
Loading