Skip to content

Commit

Permalink
Merge pull request #137 from Team-GAJI/feature/#136-refactor-to-roomi…
Browse files Browse the repository at this point in the history
…d/GAJI-139

♻️ [refactor] 스터디룸 게시글(트러블슈팅 게시글, 게시글, 정보나눔 게시글) pathvriable 을 roomId 형태로 변경
  • Loading branch information
mmingoo authored Aug 20, 2024
2 parents 2d52d91 + c0ff182 commit 6e572dc
Show file tree
Hide file tree
Showing 21 changed files with 199 additions and 94 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gaji.service.domain.roomBoard.repository;

import gaji.service.domain.enums.RoomPostType;
import gaji.service.domain.roomBoard.entity.RoomBoard;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
Expand All @@ -8,5 +9,6 @@

@Repository
public interface RoomBoardRepository extends JpaRepository<RoomBoard, Long> {
Optional<RoomBoard> findRoomBoardByRoomIdAndRoomPostType(Long roomId, RoomPostType roomPostType);
Optional<RoomBoard> findByRoomId(Long roomId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@
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 RoomInfoPostRepository extends JpaRepository<RoomInfoPost, Long> {
@Query("SELECT new gaji.service.domain.roomBoard.web.dto.RoomPostResponseDto$InfoPostSummaryDto(" +
"r.id, r.title, r.studyMate.user.nickname, r.createdAt, r.viewCount, SIZE(r.infoPostCommentList)) " +
"FROM RoomInfoPost r " +
"WHERE r.roomBoard.id = :boardId AND r.id < :lastPostId " +
"ORDER BY r.createdAt DESC")
"WHERE r.roomBoard.id = :boardId AND r.createdAt <= :lastCreatedAt " +
"ORDER BY r.createdAt DESC, r.id DESC")
List<RoomPostResponseDto.InfoPostSummaryDto> findInfoPostSummariesForInfiniteScroll(
@Param("boardId") Long boardId,
@Param("lastPostId") Long lastPostId,
@Param("lastCreatedAt") LocalDateTime lastCreatedAt,
Pageable pageable);

@Query("SELECT CASE " +
"WHEN EXISTS (SELECT 1 FROM RoomInfoPost r WHERE r.roomBoard.id = :boardId AND r.id = :postId) " +
"THEN (SELECT r.createdAt FROM RoomInfoPost r WHERE r.roomBoard.id = :boardId AND r.id = :postId) " +
"ELSE (SELECT MAX(r.createdAt) FROM RoomInfoPost r WHERE r.roomBoard.id = :boardId) " +
"END")
Optional<LocalDateTime> findCreatedAtByIdOrEarliest(@Param("boardId") Long boardId, @Param("postId") Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@
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 RoomPostRepository extends JpaRepository<RoomPost, Long> {
@Query("SELECT new gaji.service.domain.roomBoard.web.dto.RoomPostResponseDto$PostSummaryDto(" +
"r.id, r.title, r.studyMate.user.nickname, r.createdAt, r.viewCount, SIZE(r.postCommentList)) " +
"FROM RoomPost r " +
"WHERE r.roomBoard.id = :boardId AND r.id < :lastPostId " +
"ORDER BY r.createdAt DESC")
"WHERE r.roomBoard.id = :boardId AND r.createdAt <= :lastCreatedAt " +
"ORDER BY r.createdAt DESC, r.id DESC")
List<PostSummaryDto> findPostSummariesForInfiniteScroll(
@Param("boardId") Long boardId,
@Param("lastPostId") Long lastPostId,
@Param("lastCreatedAt") LocalDateTime lastCreatedAt,
Pageable pageable);

@Query("SELECT CASE " +
"WHEN EXISTS (SELECT 1 FROM RoomPost r WHERE r.roomBoard.id = :boardId AND r.id = :postId) " +
"THEN (SELECT r.createdAt FROM RoomPost r WHERE r.roomBoard.id = :boardId AND r.id = :postId) " +
"ELSE (SELECT MAX(r.createdAt) FROM RoomPost r WHERE r.roomBoard.id = :boardId) " +
"END")
Optional<LocalDateTime> findCreatedAtByIdOrEarliest(@Param("boardId") Long boardId, @Param("postId") Long postId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@
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 RoomTroublePostRepository extends JpaRepository<RoomTroublePost, Long> {
@Query("SELECT new gaji.service.domain.roomBoard.web.dto.RoomPostResponseDto$TroublePostSummaryDto(" +
"r.id, r.title, r.studyMate.user.nickname, r.createdAt, r.viewCount, SIZE(r.troublePostCommentList)) " +
"FROM RoomTroublePost r " +
"WHERE r.roomBoard.id = :boardId AND r.id < :lastPostId " +
"ORDER BY r.createdAt DESC")
"WHERE r.roomBoard.id = :boardId AND r.createdAt <= :lastCreatedAt " +
"ORDER BY r.createdAt DESC, r.id DESC")
List<RoomPostResponseDto.TroublePostSummaryDto> findTroublePostSummariesForInfiniteScroll(
@Param("boardId") Long boardId,
@Param("lastPostId") Long lastPostId,
@Param("lastCreatedAt") LocalDateTime lastCreatedAt,
Pageable pageable);
}

@Query("SELECT CASE " +
"WHEN EXISTS (SELECT 1 FROM RoomTroublePost r WHERE r.roomBoard.id = :boardId AND r.id = :postId) " +
"THEN (SELECT r.createdAt FROM RoomTroublePost r WHERE r.roomBoard.id = :boardId AND r.id = :postId) " +
"ELSE (SELECT MAX(r.createdAt) FROM RoomTroublePost r WHERE r.roomBoard.id = :boardId) " +
"END")
Optional<LocalDateTime> findCreatedAtByIdOrEarliest(@Param("boardId") Long boardId, @Param("postId") Long postId);}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ List<TroublePostComment> findCommentsWithReplies(@Param("postId") Long postId,
"AND c.isReply = false " +
"ORDER BY c.createdAt ASC, c.id ASC")
Page<TroublePostComment> findOldestComments(@Param("postId") Long postId, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public RoomPostResponseDto.toCreateRoomInfoPostIdDTO createRoomInfoPostIdDTO(Lon
StudyMate studyMate = studyMateQueryService.findByUserIdAndRoomId(user.getId(), roomId);

// 스터디룸 게시판 확인 또는 생성
RoomBoard roomBoard = roomBoardRepository.findByRoomId(roomId)
RoomBoard roomBoard = roomBoardRepository.findRoomBoardByRoomIdAndRoomPostType(roomId, RoomPostType.ROOM_INFORMATION_POST)
.orElseGet(() -> {
RoomBoard newRoomBoard = RoomBoard.builder()
.room(room)
.roomPostType(RoomPostType.ROOM_TROUBLE_POST)
.roomPostType(RoomPostType.ROOM_INFORMATION_POST)
.name(room.getName())
.build();
return roomBoardRepository.save(newRoomBoard);
Expand Down
Loading

0 comments on commit 6e572dc

Please sign in to comment.