Skip to content

Commit

Permalink
Merge pull request #162 from KUIT-Space/refactoring/#158/voice-room-d…
Browse files Browse the repository at this point in the history
…omain/dto,repo

Refactoring voice room 객체 정리 및 미사용 코드 삭제
  • Loading branch information
drbug2000 authored Oct 30, 2024
2 parents 504d56d + 6b9348c commit e90ee3e
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 173 deletions.
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);

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);

}
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

0 comments on commit e90ee3e

Please sign in to comment.