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

feat: 방 전체 목록 조회 기능 구현 #109

Merged
merged 10 commits into from
Nov 20, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.moabam.api.application.auth.mapper.AuthMapper;
import com.moabam.api.domain.member.Member;
import com.moabam.api.domain.member.repository.MemberRepository;
import com.moabam.api.domain.member.repository.MemberSearchRepository;
import com.moabam.api.dto.auth.AuthorizationTokenInfoResponse;
import com.moabam.api.dto.auth.LoginResponse;
import com.moabam.global.error.exception.NotFoundException;
Expand All @@ -26,7 +25,6 @@
public class MemberService {

private final MemberRepository memberRepository;
private final MemberSearchRepository memberSearchRepository;

public Member getById(Long memberId) {
return memberRepository.findById(memberId)
Expand All @@ -53,11 +51,6 @@ private String createRandomNickName() {
new SecureRandom());
}

public Member getManager(Long roomId) {
return memberSearchRepository.findManager(roomId)
.orElseThrow(() -> new NotFoundException(MEMBER_NOT_FOUND));
}

public List<Member> getRoomMembers(List<Long> memberIds) {
return memberRepository.findAllById(memberIds);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.moabam.api.application.room;

import static com.moabam.global.error.model.ErrorMessage.PARTICIPANT_NOT_FOUND;
import static com.moabam.global.error.model.ErrorMessage.ROOM_DETAILS_ERROR;
import static com.moabam.global.common.util.GlobalConstant.*;
import static com.moabam.global.error.model.ErrorMessage.*;
import static org.apache.commons.lang3.StringUtils.*;

import java.time.LocalDate;
import java.time.Period;
Expand All @@ -22,9 +23,11 @@
import com.moabam.api.domain.room.DailyRoomCertification;
import com.moabam.api.domain.room.Participant;
import com.moabam.api.domain.room.Room;
import com.moabam.api.domain.room.RoomType;
import com.moabam.api.domain.room.Routine;
import com.moabam.api.domain.room.repository.CertificationsSearchRepository;
import com.moabam.api.domain.room.repository.ParticipantSearchRepository;
import com.moabam.api.domain.room.repository.RoomSearchRepository;
import com.moabam.api.domain.room.repository.RoutineSearchRepository;
import com.moabam.api.dto.room.CertificationImageResponse;
import com.moabam.api.dto.room.MyRoomResponse;
Expand All @@ -33,9 +36,12 @@
import com.moabam.api.dto.room.RoomHistoryResponse;
import com.moabam.api.dto.room.RoomsHistoryResponse;
import com.moabam.api.dto.room.RoutineResponse;
import com.moabam.api.dto.room.SearchAllRoomResponse;
import com.moabam.api.dto.room.SearchAllRoomsResponse;
import com.moabam.api.dto.room.TodayCertificateRankResponse;
import com.moabam.global.error.exception.NotFoundException;

import jakarta.annotation.Nullable;
import lombok.RequiredArgsConstructor;

@Service
Expand All @@ -46,6 +52,7 @@ public class RoomSearchService {
private final CertificationsSearchRepository certificationsSearchRepository;
private final ParticipantSearchRepository participantSearchRepository;
private final RoutineSearchRepository routineSearchRepository;
private final RoomSearchRepository roomSearchRepository;
private final MemberService memberService;
private final RoomCertificationService roomCertificationService;

Expand All @@ -55,7 +62,7 @@ public RoomDetailsResponse getRoomDetails(Long memberId, Long roomId) {
.orElseThrow(() -> new NotFoundException(PARTICIPANT_NOT_FOUND));
Room room = participant.getRoom();

String managerNickname = memberService.getManager(roomId).getNickname();
String managerNickname = room.getManagerNickname();
List<DailyMemberCertification> dailyMemberCertifications =
certificationsSearchRepository.findSortedDailyMemberCertifications(roomId, today);
List<RoutineResponse> routineResponses = getRoutineResponses(roomId);
Expand Down Expand Up @@ -105,6 +112,35 @@ public RoomsHistoryResponse getJoinHistory(Long memberId) {
return RoomMapper.toRoomsHistoryResponse(roomHistoryResponses);
}

public SearchAllRoomsResponse searchAllRooms(@Nullable RoomType roomType, @Nullable Long roomId) {
List<SearchAllRoomResponse> searchAllRoomResponses = new ArrayList<>();
List<Room> rooms = new ArrayList<>(roomSearchRepository.findAllWithNoOffset(roomType, roomId));

boolean hasNext = false;

if (rooms.size() > ROOM_FIXED_SEARCH_SIZE) {
hasNext = true;
rooms.remove(ROOM_FIXED_SEARCH_SIZE);
}

List<Long> roomIds = rooms.stream().map(Room::getId).toList();
Copy link
Member

Choose a reason for hiding this comment

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

C: StreamUtils 적용할 수 있을듯!

List<Routine> routines = routineSearchRepository.findAllByRoomIds(roomIds);

for (Room room : rooms) {
List<Routine> filteredRoutines = routines.stream()
.filter(routine -> routine.getRoom().getId().equals(room.getId()))
.toList();

boolean isPassword = !isEmpty(room.getPassword());

searchAllRoomResponses.add(
RoomMapper.toSearchAllRoomResponse(room, RoutineMapper.toRoutineResponses(filteredRoutines),
isPassword));
}

return RoomMapper.toSearchAllRoomsResponse(hasNext, searchAllRoomResponses);
}

private List<RoutineResponse> getRoutineResponses(Long roomId) {
List<Routine> roomRoutines = routineSearchRepository.findAllByRoomId(roomId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class RoomService {
private final MemberService memberService;

@Transactional
public Long createRoom(Long memberId, CreateRoomRequest createRoomRequest) {
public Long createRoom(Long memberId, String nickname, CreateRoomRequest createRoomRequest) {
Room room = RoomMapper.toRoomEntity(createRoomRequest);
List<Routine> routines = RoutineMapper.toRoutineEntities(room, createRoomRequest.routines());
Participant participant = Participant.builder()
Expand All @@ -59,6 +59,7 @@ public Long createRoom(Long memberId, CreateRoomRequest createRoomRequest) {
increaseRoomCount(memberId, room.getRoomType());

participant.enableManager();
room.changeManagerNickname(nickname);
Room savedRoom = roomRepository.save(room);
routineRepository.saveAll(routines);
participantRepository.save(participant);
Expand Down Expand Up @@ -128,6 +129,10 @@ public void mandateRoomManager(Long managerId, Long roomId, Long memberId) {
Participant memberParticipant = getParticipant(memberId, roomId);
validateManagerAuthorization(managerParticipant);

Room room = managerParticipant.getRoom();
Member member = memberService.getById(memberParticipant.getMemberId());
room.changeManagerNickname(member.getNickname());

managerParticipant.disableManager();
memberParticipant.enableManager();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.moabam.api.dto.room.RoomHistoryResponse;
import com.moabam.api.dto.room.RoomsHistoryResponse;
import com.moabam.api.dto.room.RoutineResponse;
import com.moabam.api.dto.room.SearchAllRoomResponse;
import com.moabam.api.dto.room.SearchAllRoomsResponse;
import com.moabam.api.dto.room.TodayCertificateRankResponse;

import lombok.AccessLevel;
Expand Down Expand Up @@ -86,4 +88,29 @@ public static RoomsHistoryResponse toRoomsHistoryResponse(List<RoomHistoryRespon
.roomHistory(roomHistoryResponses)
.build();
}

public static SearchAllRoomResponse toSearchAllRoomResponse(Room room, List<RoutineResponse> routineResponses,
boolean isPassword) {
return SearchAllRoomResponse.builder()
.id(room.getId())
.title(room.getTitle())
.image(room.getRoomImage())
.isPassword(isPassword)
.managerNickname(room.getManagerNickname())
.level(room.getLevel())
.roomType(room.getRoomType())
.certifyTime(room.getCertifyTime())
.currentUserCount(room.getCurrentUserCount())
.maxUserCount(room.getMaxUserCount())
.routine(routineResponses)
.build();
}

public static SearchAllRoomsResponse toSearchAllRoomsResponse(boolean hasNext,
List<SearchAllRoomResponse> searchAllRoomResponses) {
return SearchAllRoomsResponse.builder()
.hasNext(hasNext)
.rooms(searchAllRoomResponses)
.build();
}
}

This file was deleted.

7 changes: 7 additions & 0 deletions src/main/java/com/moabam/api/domain/room/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public class Room extends BaseTimeEntity {
@Column(name = "room_image", length = 500)
private String roomImage;

@Column(name = "manager_nickname", length = 30)
private String managerNickname;

@Builder
private Room(Long id, String title, String password, RoomType roomType, int certifyTime, int maxUserCount) {
this.id = id;
Expand Down Expand Up @@ -111,6 +114,10 @@ public void changePassword(String password) {
this.password = password;
}

public void changeManagerNickname(String managerNickname) {
this.managerNickname = managerNickname;
}

public void changeMaxCount(int maxUserCount) {
if (maxUserCount < this.currentUserCount) {
throw new BadRequestException(ROOM_MAX_USER_COUNT_MODIFY_FAIL);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.moabam.api.domain.room.repository;

import static com.moabam.api.domain.room.QRoom.*;
import static com.moabam.global.common.util.GlobalConstant.*;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.moabam.api.domain.room.Room;
import com.moabam.api.domain.room.RoomType;
import com.moabam.global.common.util.DynamicQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;

@Repository
@RequiredArgsConstructor
public class RoomSearchRepository {

private final JPAQueryFactory jpaQueryFactory;

public List<Room> findAllWithNoOffset(RoomType roomType, Long roomId) {
return jpaQueryFactory.selectFrom(room)
.where(
DynamicQuery.generateEq(roomType, room.roomType::eq),
DynamicQuery.generateEq(roomId, room.id::lt)
)
.orderBy(room.id.desc())
.limit(ROOM_FIXED_SEARCH_SIZE + 1L)
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ public List<Routine> findAllByRoomId(Long roomId) {
)
.fetch();
}

public List<Routine> findAllByRoomIds(List<Long> roomIds) {
return jpaQueryFactory
.selectFrom(routine)
.where(
routine.room.id.in(roomIds)
)
.fetch();
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/moabam/api/dto/room/SearchAllRoomResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.moabam.api.dto.room;

import java.util.List;

import com.moabam.api.domain.room.RoomType;

import lombok.Builder;

@Builder
public record SearchAllRoomResponse(
Long id,
String title,
String image,
boolean isPassword,
String managerNickname,
int level,
RoomType roomType,
int certifyTime,
int currentUserCount,
int maxUserCount,
List<RoutineResponse> routine
) {

}
13 changes: 13 additions & 0 deletions src/main/java/com/moabam/api/dto/room/SearchAllRoomsResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.moabam.api.dto.room;

import java.util.List;

import lombok.Builder;

@Builder
public record SearchAllRoomsResponse(
boolean hasNext,
List<SearchAllRoomResponse> rooms
) {

}
13 changes: 12 additions & 1 deletion src/main/java/com/moabam/api/presentation/RoomController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -18,12 +19,14 @@
import com.moabam.api.application.room.RoomCertificationService;
import com.moabam.api.application.room.RoomSearchService;
import com.moabam.api.application.room.RoomService;
import com.moabam.api.domain.room.RoomType;
import com.moabam.api.dto.room.CreateRoomRequest;
import com.moabam.api.dto.room.EnterRoomRequest;
import com.moabam.api.dto.room.ModifyRoomRequest;
import com.moabam.api.dto.room.MyRoomsResponse;
import com.moabam.api.dto.room.RoomDetailsResponse;
import com.moabam.api.dto.room.RoomsHistoryResponse;
import com.moabam.api.dto.room.SearchAllRoomsResponse;
import com.moabam.global.auth.annotation.CurrentMember;
import com.moabam.global.auth.model.AuthorizationMember;

Expand All @@ -44,7 +47,7 @@ public class RoomController {
public Long createRoom(@CurrentMember AuthorizationMember authorizationMember,
@Valid @RequestBody CreateRoomRequest createRoomRequest) {

return roomService.createRoom(authorizationMember.id(), createRoomRequest);
return roomService.createRoom(authorizationMember.id(), authorizationMember.nickname(), createRoomRequest);
}

@PutMapping("/{roomId}")
Expand Down Expand Up @@ -112,4 +115,12 @@ public MyRoomsResponse getMyRooms(@CurrentMember AuthorizationMember authorizati
public RoomsHistoryResponse getJoinHistory(@CurrentMember AuthorizationMember authorizationMember) {
return roomSearchService.getJoinHistory(authorizationMember.id());
}

@GetMapping
@ResponseStatus(HttpStatus.OK)
public SearchAllRoomsResponse searchAllRooms(@RequestParam(value = "type", required = false) RoomType roomType,
@RequestParam(value = "roomId", required = false) Long roomId) {

return roomSearchService.searchAllRooms(roomType, roomId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class GlobalConstant {
public static final String SPACE = " ";
public static final int ONE_HOUR = 1;
public static final int HOURS_IN_A_DAY = 24;

public static final String KNOCK_KEY = "room_%s_member_%s_knocks_%s";
public static final String FIREBASE_PATH = "config/moabam-firebase.json";
public static final int ROOM_FIXED_SEARCH_SIZE = 10;
public static final int LEVEL_DIVISOR = 10;
}
Loading