Skip to content

Commit

Permalink
fix: 랜덤 조회 예외 케이스 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Beakjiyeon committed Apr 6, 2024
1 parent 06fdf2f commit abe78c4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -35,10 +37,13 @@ public List<AddBoardResponseDto> getMyBoards(@LoginUsers CustomUserDetails userD
/**
* 사용자가 보지 않은 글 중에서 랜덤 3개 골라 주기
* 밤 12시에 적재된 사용자가 읽지 않은 글을 조회
* 만약 배치 후 가입한 사용자라면, 자기가 쓰지 않은 최신글
*/
@GetMapping("/random")
public List<AddBoardResponseDto> getRandomBoards(@LoginUsers CustomUserDetails userDetails) {
return boardService.getRandomBoards(userDetails.getMemberId());
List<AddBoardResponseDto> result = boardService.getRandomBoards(userDetails.getMemberId());

return result;
}

@PostMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,8 +36,10 @@ public List<Board> findNotReadBoardsByMemberId(Long memberId, List<Long> 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();

Expand Down Expand Up @@ -78,4 +81,13 @@ public List<Board> findBoardWithHashtag(Long boardId) {
.fetch();

}

public List<Board> findCurrentBoards(Long memberId) {
ArrayList<Long> tmps = new ArrayList<>();
tmps.add(memberId);
return queryFactory
.selectFrom(board)
.where(board.memeber.id.notIn(tmps))
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,19 @@ public List<AddBoardResponseDto> getMyBoards(Long memberId) {

@Transactional(readOnly = true)
public List<AddBoardResponseDto> getRandomBoards(Long memberId) {
return getTodayBoardByMemberId(memberId);
List<AddBoardResponseDto> result = getTodayBoardByMemberId(memberId);
if (result.size() < 3) {
List<Board> 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;
}


Expand Down Expand Up @@ -131,7 +143,7 @@ public BoardTmpResponse modifyBoard(Long boardId, AddBoardRequestDto addBoardReq
List<Hashtag> originHashContents = board.getHashtags();


for(int i =0;i<newHashContents.size();i++) {
for (int i = 0; i < newHashContents.size(); i++) {
hashtagRepository.save(Hashtag.createHashtag(newHashContents.get(i), board));
}

Expand All @@ -155,4 +167,12 @@ public BoardTmpResponse getBoardWithHashTag(Long boardId) {


}

public List<Board> getCurrentBoards(Long memberId) {
List<Board> result = boardRepositoryImpl.findCurrentBoards(memberId);
return result;
// return result.stream()
// .map(AddBoardResponseDto::from)
// .toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Board> boards = new ArrayList<>();

@Builder(access = AccessLevel.PRIVATE)
Expand Down

0 comments on commit abe78c4

Please sign in to comment.