Skip to content

Commit

Permalink
refactor: 날짜별 읽은 뉴스 조회수 반환 로직에 로그인 유저 파라미터 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjy committed Jun 17, 2024
1 parent f8b8136 commit e3217d2
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.time.LocalDate;

public interface ReadNewsCustomRepository {
Long getCountOfReadNewsByDate(LocalDate date);
Long getCountOfReadNewsByDate(Member member, LocalDate date);

Long getCountOfReadNewsByCategory(Member member, String category);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ public class ReadNewsCustomRepositoryImpl implements ReadNewsCustomRepository {
private final JPAQueryFactory queryFactory;

@Override
public Long getCountOfReadNewsByDate(LocalDate date) {
public Long getCountOfReadNewsByDate(Member member, LocalDate date) {
return queryFactory
.select(readNews.count())
.from(readNews)
.where(readNews.createdAt.between(
date.atStartOfDay(),
date.plusDays(1).atStartOfDay().minusNanos(1)))
.where(readNews.member.eq(member)
.and(readNews.createdAt.between(
date.atStartOfDay(),
date.plusDays(1).atStartOfDay().minusNanos(1))))
.fetchOne();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public class StatisticsService {
private final AuthService authService;

public List<DateViewStatisticsResponse> getViewsOfDates() {
Member member = authService.getMember();
List<DateViewStatisticsResponse> responses = new ArrayList<>();
IntStream.range(0, 7)
.forEach(day -> responses.add(DateViewStatisticsResponse.builder()
.views(readNewsRepository.getCountOfReadNewsByDate(LocalDate.now().minusDays(day)))
.views(readNewsRepository.getCountOfReadNewsByDate(member, LocalDate.now().minusDays(day)))
.dateTime(LocalDate.now().minusDays(day)).build()));
return responses;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void getCountOfReadNewsByDate() {
}

//when
Long result = readNewsRepository.getCountOfReadNewsByDate(LocalDate.now());
Long result = readNewsRepository.getCountOfReadNewsByDate(member, LocalDate.now());

//then
assertThat(result).isEqualTo(expect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class StatisticsServiceTest extends LoginTest {
void getViewsOfDates() {
//given
int oneWeek = 7;
given(readNewsRepository.getCountOfReadNewsByDate(LocalDate.now())).willReturn(anyLong());
given(readNewsRepository.getCountOfReadNewsByDate(loginUser, LocalDate.now())).willReturn(1L);

//when
List<DateViewStatisticsResponse> result = statisticsService.getViewsOfDates();
Expand Down

0 comments on commit e3217d2

Please sign in to comment.