From abe78c4ed3fcf842f666d88d70b93253ce8504de Mon Sep 17 00:00:00 2001 From: Beakjiyeon Date: Sun, 7 Apr 2024 00:19:52 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=9E=9C=EB=8D=A4=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20=EC=98=88=EC=99=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/BoardController.java | 7 +++++- .../board/repository/BoardRepositoryImpl.java | 16 +++++++++++-- .../domain/board/service/BoardService.java | 24 +++++++++++++++++-- .../daybook/domain/member/domain/Member.java | 2 +- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/unit/daybook/domain/board/controller/BoardController.java b/src/main/java/com/unit/daybook/domain/board/controller/BoardController.java index 81fa6e7..848e1dc 100644 --- a/src/main/java/com/unit/daybook/domain/board/controller/BoardController.java +++ b/src/main/java/com/unit/daybook/domain/board/controller/BoardController.java @@ -3,8 +3,10 @@ import com.unit.daybook.domain.board.dto.request.AddBoardRequestDto; import com.unit.daybook.domain.board.dto.response.AddBoardResponseDto; import com.unit.daybook.domain.board.dto.response.BoardTmpResponse; +import com.unit.daybook.domain.board.entity.ReadBoard; import com.unit.daybook.domain.board.service.BoardService; import com.unit.daybook.domain.common.annotation.LoginUsers; +import com.unit.daybook.domain.member.domain.Member; import com.unit.daybook.global.config.security.CustomUserDetails; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -35,10 +37,13 @@ public List getMyBoards(@LoginUsers CustomUserDetails userD /** * 사용자가 보지 않은 글 중에서 랜덤 3개 골라 주기 * 밤 12시에 적재된 사용자가 읽지 않은 글을 조회 + * 만약 배치 후 가입한 사용자라면, 자기가 쓰지 않은 최신글 */ @GetMapping("/random") public List getRandomBoards(@LoginUsers CustomUserDetails userDetails) { - return boardService.getRandomBoards(userDetails.getMemberId()); + List result = boardService.getRandomBoards(userDetails.getMemberId()); + + return result; } @PostMapping diff --git a/src/main/java/com/unit/daybook/domain/board/repository/BoardRepositoryImpl.java b/src/main/java/com/unit/daybook/domain/board/repository/BoardRepositoryImpl.java index a436edc..8bab861 100644 --- a/src/main/java/com/unit/daybook/domain/board/repository/BoardRepositoryImpl.java +++ b/src/main/java/com/unit/daybook/domain/board/repository/BoardRepositoryImpl.java @@ -5,6 +5,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; +import java.util.ArrayList; import java.util.List; import static com.unit.daybook.domain.board.entity.QBoard.board; @@ -35,8 +36,10 @@ public List findNotReadBoardsByMemberId(Long memberId, List aleadyR .from(board) .join(board.memeber, member).fetchJoin() .where( - member.id.eq(memberId) - .and(board.boardId.notIn(aleadyReadBoardIds)) + // member.id.eq(memberId) + //.and( + board.boardId.notIn(aleadyReadBoardIds) + //) ) .fetch(); @@ -78,4 +81,13 @@ public List findBoardWithHashtag(Long boardId) { .fetch(); } + + public List findCurrentBoards(Long memberId) { + ArrayList tmps = new ArrayList<>(); + tmps.add(memberId); + return queryFactory + .selectFrom(board) + .where(board.memeber.id.notIn(tmps)) + .fetch(); + } } diff --git a/src/main/java/com/unit/daybook/domain/board/service/BoardService.java b/src/main/java/com/unit/daybook/domain/board/service/BoardService.java index e6d65ee..8a35de9 100644 --- a/src/main/java/com/unit/daybook/domain/board/service/BoardService.java +++ b/src/main/java/com/unit/daybook/domain/board/service/BoardService.java @@ -65,7 +65,19 @@ public List getMyBoards(Long memberId) { @Transactional(readOnly = true) public List getRandomBoards(Long memberId) { - return getTodayBoardByMemberId(memberId); + List result = getTodayBoardByMemberId(memberId); + if (result.size() < 3) { + List boards = getCurrentBoards(memberId); + // read-board 에도 적재 + Member member = memberRepository.findById(memberId).orElseThrow(() -> new RuntimeException(memberId + "not found")); + readBoardRepository.save(ReadBoard.createReadBoard(member, boards.get(0))); + readBoardRepository.save(ReadBoard.createReadBoard(member, boards.get(1))); + readBoardRepository.save(ReadBoard.createReadBoard(member, boards.get(2))); + result = boards.stream() + .map(AddBoardResponseDto::from) + .toList(); + } + return result; } @@ -131,7 +143,7 @@ public BoardTmpResponse modifyBoard(Long boardId, AddBoardRequestDto addBoardReq List originHashContents = board.getHashtags(); - for(int i =0;i getCurrentBoards(Long memberId) { + List result = boardRepositoryImpl.findCurrentBoards(memberId); + return result; +// return result.stream() +// .map(AddBoardResponseDto::from) +// .toList(); + } } diff --git a/src/main/java/com/unit/daybook/domain/member/domain/Member.java b/src/main/java/com/unit/daybook/domain/member/domain/Member.java index 53d9234..685fcf1 100644 --- a/src/main/java/com/unit/daybook/domain/member/domain/Member.java +++ b/src/main/java/com/unit/daybook/domain/member/domain/Member.java @@ -36,7 +36,7 @@ public class Member extends BaseTimeEntity { private LocalDateTime lastLoginAt; - @OneToMany(fetch = FetchType.LAZY) + @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) private List boards = new ArrayList<>(); @Builder(access = AccessLevel.PRIVATE)