Skip to content

Commit

Permalink
♻️ 공지사항 스크롤 방식 변경
Browse files Browse the repository at this point in the history
무한 스크롤 방식으로 변경
  • Loading branch information
mmingoo committed Aug 20, 2024
1 parent 880e7ae commit c0ff182
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ public interface RoomEventRepository extends JpaRepository<RoomEvent,Long> {
Optional<RoomEvent> findRoomEventById(Long roomId);

List<RoomEvent> findByRoom(Room room);

int countByRoomId(Long roomId);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
package gaji.service.domain.room.repository;

import gaji.service.domain.room.entity.RoomNotice;
import gaji.service.domain.room.web.dto.RoomResponseDto;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface RoomNoticeRepository extends JpaRepository<RoomNotice, Long> {
@Modifying
@Query("UPDATE RoomNotice rn SET rn.confirmCount = rn.confirmCount + 1 WHERE rn.id = :noticeId")
void incrementConfirmCount(Long noticeId);
@Query("SELECT NEW gaji.service.domain.room.web.dto.RoomResponseDto$NoticeDto(" +
"rn.id, sm.user.name, rn.title, rn.body, CAST(COUNT(nc) AS Long), rn.createdAt, rn.viewCount) " +
"FROM RoomNotice rn " +
"JOIN rn.studyMate sm " +
"LEFT JOIN NoticeConfirmation nc ON nc.roomNotice.id = rn.id " +
"WHERE sm.room.id = :roomId AND rn.createdAt <= :lastCreatedAt " +
"GROUP BY rn.id, sm.user.name, rn.title, rn.body, rn.createdAt, rn.viewCount " +
"ORDER BY rn.createdAt DESC, rn.id DESC")
List<RoomResponseDto.NoticeDto> findNoticeSummariesForInfiniteScroll(
@Param("roomId") Long roomId,
@Param("lastCreatedAt") LocalDateTime lastCreatedAt,
Pageable pageable);

@Modifying
@Query("UPDATE RoomNotice rn SET rn.confirmCount = rn.confirmCount - 1 WHERE rn.id = :noticeId AND rn.confirmCount > 0")
void decrementConfirmCount(Long noticeId);
@Query("SELECT CASE " +
"WHEN EXISTS (SELECT 1 FROM RoomNotice rn WHERE rn.studyMate.room.id = :roomId AND rn.id = :noticeId) " +
"THEN (SELECT rn.createdAt FROM RoomNotice rn WHERE rn.studyMate.room.id = :roomId AND rn.id = :noticeId) " +
"ELSE (SELECT MAX(rn.createdAt) FROM RoomNotice rn WHERE rn.studyMate.room.id = :roomId) " +
"END")
Optional<LocalDateTime> findCreatedAtByIdOrEarliest(@Param("roomId") Long roomId, @Param("noticeId") Long noticeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import gaji.service.domain.room.entity.Room;
import gaji.service.domain.room.entity.RoomEvent;
import gaji.service.domain.room.web.dto.RoomResponseDto;
import gaji.service.domain.user.entity.User;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
Expand All @@ -14,9 +13,13 @@ public interface RoomQueryService {

RoomEvent findRoomEventByRoomIdAndWeeks(Long roomId, Integer weeks);

List<RoomResponseDto.NoticeDto> getNotices(Long roomId, int page, int size);
// List<RoomResponseDto.NoticeDto> getNotices(Long roomId, int page, int size);
//
// RoomResponseDto.NoticeDto getNoticeDetail(Long roomId, Long noticeId);
//
// List<RoomResponseDto.NoticeDto> getNextNotices(Long roomId, Long lastNoticeId, int size);

RoomResponseDto.NoticeDto getNoticeDetail(Long roomId, Long noticeId);
List<RoomResponseDto.NoticeDto> getNextNotices(Long roomId, Long lastNoticeId, int size);

@Transactional(readOnly = true)
RoomResponseDto.WeeklyStudyInfoDTO getWeeklyStudyInfo(Long roomId, Integer weeks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import gaji.service.domain.room.entity.RoomEvent;
import gaji.service.domain.room.repository.*;
import gaji.service.domain.room.web.dto.RoomResponseDto;
import gaji.service.domain.user.entity.User;
import gaji.service.global.converter.DateConverter;
import gaji.service.global.exception.RestApiException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.web.access.WebInvocationPrivilegeEvaluator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.webjars.NotFoundException;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -29,6 +32,7 @@ public class RoomQueryServiceImpl implements RoomQueryService {
private final WeeklyUserProgressRepository weeklyUserProgressRepository;
private final NoticeConfirmationRepository noticeConfirmationRepository;
private final WebInvocationPrivilegeEvaluator privilegeEvaluator;
private final RoomNoticeRepository roomNoticeRepository;

// @Override
// public RoomEvent findRoomEventById(Long roomId){
Expand All @@ -49,28 +53,32 @@ public RoomEvent findRoomEventByRoomIdAndWeeks(Long roomId, Integer weeks) {
.orElseThrow(() -> new RestApiException(RoomErrorStatus._ROOM_EVENT_NOT_FOUND));
}

@Override
public List<RoomResponseDto.NoticeDto> getNotices(Long roomId, int page, int size) {
return roomQueryRepository.getNotices(roomId, page, size);
}
// @Override
// public List<RoomResponseDto.NoticeDto> getNotices(Long roomId, int page, int size) {
// return roomQueryRepository.getNotices(roomId, page, size);
// }

@Override
@Transactional(readOnly = false) // readOnly = false로 설정
public RoomResponseDto.NoticeDto getNoticeDetail(Long roomId, Long noticeId) {
RoomResponseDto.NoticeDto notice = roomQueryRepository.getNotices(roomId, 1, Integer.MAX_VALUE).stream()
.filter(n -> n.getId().equals(noticeId))
.findFirst()
.orElseThrow(() -> new NotFoundException("Notice not found"));

// viewCount 증가
roomQueryRepository.incrementViewCount (noticeId);

// 증가된 viewCount를 반영하기 위해 notice 객체 업데이트
notice.setViewCount(notice.getViewCount() + 1);

return notice;
public List<RoomResponseDto.NoticeDto> getNextNotices(Long roomId, Long lastNoticeId, int size) {
LocalDateTime lastCreatedAt;
if (lastNoticeId == 0) {
lastCreatedAt = LocalDateTime.now();
} else {
lastCreatedAt = roomNoticeRepository.findCreatedAtByIdOrEarliest(roomId, lastNoticeId)
.orElseThrow(() -> new RestApiException(RoomErrorStatus._NOTICE_NOT_FOUND));
}

Sort sort = Sort.by(Sort.Direction.DESC, "createdAt", "id");
Pageable pageable = PageRequest.of(0, size, sort);

List<RoomResponseDto.NoticeDto> notices = roomNoticeRepository.findNoticeSummariesForInfiniteScroll(roomId, lastCreatedAt, pageable);

LocalDateTime now = LocalDateTime.now();
for (RoomResponseDto.NoticeDto notice : notices) {
notice.setTimeSincePosted(DateConverter.convertToRelativeTimeFormat(notice.getCreatedAt()));
}
return notices;
}

@Override
@Transactional(readOnly = true)
public RoomResponseDto.WeeklyStudyInfoDTO getWeeklyStudyInfo(Long roomId, Integer weeks) {
Expand Down Expand Up @@ -105,7 +113,7 @@ public List<RoomResponseDto.UserProgressDTO> getUserProgressByRoomEventId(Long r
public RoomResponseDto.RoomMainDto getMainStudyRoom(Long roomId) {

RoomResponseDto.RoomMainDto mainStudyRoom = roomQueryRepository.getMainStudyRoom(roomId);
return mainStudyRoom.setWeekCount(roomEventRepository.countByRoomId(roomId));
return mainStudyRoom;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import gaji.service.global.base.BaseResponse;
import gaji.service.jwt.service.TokenProviderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -40,17 +42,17 @@ public BaseResponse<RoomResponseDto.RoomNoticeDto> NoticeController(
}

@GetMapping("/{roomId}/notices")
@Operation(summary = "스터디룸 공지 목록 조회 API")
public BaseResponse<RoomResponseDto.NoticeDtoList> getNotices(
@PathVariable Long roomId,
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "5") int size) {
List<RoomResponseDto.NoticeDto> notices = roomQueryService.getNotices(roomId, page, size);
return BaseResponse.onSuccess(
new RoomResponseDto.NoticeDtoList(notices)
);
@Operation(summary = "스터디룸 공지 무한 스크롤 조회", description = "공지를 무한 스크롤 방식으로 조회합니다.")
@ApiResponse(responseCode = "200", description = "조회 성공")
public BaseResponse<List<RoomResponseDto.NoticeDto>> getNextNotices(
@PathVariable @Parameter(description = "스터디룸 ID") Long roomId,
@RequestParam @Parameter(description = "마지막으로 로드된 공지 ID") Long lastNoticeId,
@RequestParam(defaultValue = "5") @Parameter(description = "조회할 공지 수") int size) {
List<RoomResponseDto.NoticeDto> notices = roomQueryService.getNextNotices(roomId, lastNoticeId, size);
return BaseResponse.onSuccess(notices);
}


// @GetMapping("/notice/{noticeId}")
// @Operation(summary = "특정 공지사항을 조회하는 API")
// public ResponseEntity<RoomResponseDto.NoticeDto> getNoticeDetail(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public static class RoomNoticeDto {
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class RoomMainDto {
private String name;
private LocalDate startDay;
Expand All @@ -119,7 +118,6 @@ public static class RoomMainDto {
private LocalDate recruitEndDay;
private Long daysLeftForRecruit;
private Long applicantCount;
private int weekCount;

public RoomMainDto(String name, LocalDate startDay, LocalDate endDay,
LocalDate recruitStartDay, LocalDate recruitEndDay,
Expand All @@ -132,12 +130,6 @@ public RoomMainDto(String name, LocalDate startDay, LocalDate endDay,
this.applicantCount = applicantCount;
this.daysLeftForRecruit = Math.max(daysLeftForRecruit, 0L);
}

public RoomMainDto setWeekCount(int weekCount){
this.weekCount=weekCount;
return this;
}

}

@Builder
Expand Down

0 comments on commit c0ff182

Please sign in to comment.