-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feat/#136 통계 관련 기능 구현
- Loading branch information
Showing
19 changed files
with
459 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...end/domain/news/service/NewsCategory.java → ...kend/domain/news/entity/NewsCategory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
backend/core/src/main/java/com/rollthedice/backend/domain/statistics/api/StatisticsApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.rollthedice.backend.domain.statistics.api; | ||
|
||
import com.rollthedice.backend.domain.statistics.dto.response.CategoryStatisticsResponse; | ||
import com.rollthedice.backend.domain.statistics.dto.response.DateViewStatisticsResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
|
||
import java.util.List; | ||
|
||
public interface StatisticsApi { | ||
@Operation( | ||
summary = "최근 일주일 날짜별 뉴스 조회수 조회", | ||
description = "최근 일주일간 날짜별로 뉴스 조회수를 조회합니다.", | ||
security = {@SecurityRequirement(name = "access_token")}, | ||
tags = {"Statistics"} | ||
) | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "요청에 성공하였습니다." | ||
) | ||
List<DateViewStatisticsResponse> getViewsOfDates(); | ||
|
||
@Operation( | ||
summary = "카테고리별 조회수 조회", | ||
description = "카테고리별 조회수를 조회합니다.", | ||
security = {@SecurityRequirement(name = "access_token")}, | ||
tags = {"Statistics"} | ||
) | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "요청에 성공하였습니다." | ||
) | ||
List<CategoryStatisticsResponse> getCategoryStatistics(); | ||
} |
34 changes: 34 additions & 0 deletions
34
...ore/src/main/java/com/rollthedice/backend/domain/statistics/api/StatisticsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.rollthedice.backend.domain.statistics.api; | ||
|
||
import com.rollthedice.backend.domain.statistics.dto.response.CategoryStatisticsResponse; | ||
import com.rollthedice.backend.domain.statistics.dto.response.DateViewStatisticsResponse; | ||
import com.rollthedice.backend.domain.statistics.service.StatisticsService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("statistics") | ||
public class StatisticsController implements StatisticsApi { | ||
private final StatisticsService statisticsService; | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@GetMapping("/per-dates") | ||
@Override | ||
public List<DateViewStatisticsResponse> getViewsOfDates() { | ||
return statisticsService.getViewsOfDates(); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@GetMapping("/categories") | ||
@Override | ||
public List<CategoryStatisticsResponse> getCategoryStatistics() { | ||
return statisticsService.getCategoryStatistics(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...va/com/rollthedice/backend/domain/statistics/dto/response/CategoryStatisticsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.rollthedice.backend.domain.statistics.dto.response; | ||
|
||
import lombok.*; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CategoryStatisticsResponse { | ||
private Long views; | ||
private String category; | ||
} |
15 changes: 15 additions & 0 deletions
15
...va/com/rollthedice/backend/domain/statistics/dto/response/DateViewStatisticsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.rollthedice.backend.domain.statistics.dto.response; | ||
|
||
import lombok.*; | ||
|
||
import java.time.LocalDate; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class DateViewStatisticsResponse { | ||
private Long views; | ||
private LocalDate dateTime; | ||
} |
11 changes: 11 additions & 0 deletions
11
...n/java/com/rollthedice/backend/domain/statistics/repository/ReadNewsCustomRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.rollthedice.backend.domain.statistics.repository; | ||
|
||
import com.rollthedice.backend.domain.member.entity.Member; | ||
|
||
import java.time.LocalDate; | ||
|
||
public interface ReadNewsCustomRepository { | ||
Long getCountOfReadNewsByDate(Member member, LocalDate date); | ||
|
||
Long getCountOfReadNewsByCategory(Member member, String category); | ||
} |
38 changes: 38 additions & 0 deletions
38
...va/com/rollthedice/backend/domain/statistics/repository/ReadNewsCustomRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.rollthedice.backend.domain.statistics.repository; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import com.rollthedice.backend.domain.member.entity.Member; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.time.LocalDate; | ||
|
||
import static com.rollthedice.backend.domain.news.entity.QNews.news; | ||
import static com.rollthedice.backend.domain.news.entity.QReadNews.readNews; | ||
|
||
@RequiredArgsConstructor | ||
public class ReadNewsCustomRepositoryImpl implements ReadNewsCustomRepository { | ||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public Long getCountOfReadNewsByDate(Member member, LocalDate date) { | ||
return queryFactory | ||
.select(readNews.count()) | ||
.from(readNews) | ||
.where(readNews.member.eq(member) | ||
.and(readNews.createdAt.between( | ||
date.atStartOfDay(), | ||
date.plusDays(1).atStartOfDay().minusNanos(1)))) | ||
.fetchOne(); | ||
} | ||
|
||
@Override | ||
public Long getCountOfReadNewsByCategory(Member member, String category) { | ||
return queryFactory | ||
.select(readNews.count()) | ||
.from(readNews) | ||
.join(readNews.news, news) | ||
.where(readNews.member.eq(member) | ||
.and(news.category.eq(category)) | ||
).fetchOne(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...re/src/main/java/com/rollthedice/backend/domain/statistics/service/StatisticsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.rollthedice.backend.domain.statistics.service; | ||
|
||
import com.rollthedice.backend.domain.member.entity.Member; | ||
import com.rollthedice.backend.domain.news.entity.NewsCategory; | ||
import com.rollthedice.backend.domain.news.repository.ReadNewsRepository; | ||
import com.rollthedice.backend.domain.statistics.dto.response.CategoryStatisticsResponse; | ||
import com.rollthedice.backend.domain.statistics.dto.response.DateViewStatisticsResponse; | ||
import com.rollthedice.backend.global.oauth2.service.AuthService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class StatisticsService { | ||
private final ReadNewsRepository readNewsRepository; | ||
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(member, LocalDate.now().minusDays(day))) | ||
.dateTime(LocalDate.now().minusDays(day)).build())); | ||
return responses; | ||
} | ||
|
||
public List<CategoryStatisticsResponse> getCategoryStatistics() { | ||
Member member = authService.getMember(); | ||
List<CategoryStatisticsResponse> responses = new ArrayList<>(); | ||
for (NewsCategory category : NewsCategory.values()) { | ||
long views = readNewsRepository.getCountOfReadNewsByCategory(member, category.getName()); | ||
CategoryStatisticsResponse response = CategoryStatisticsResponse.builder() | ||
.category(category.getName()) | ||
.views(views).build(); | ||
responses.add(response); | ||
} | ||
return responses; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
backend/core/src/main/java/com/rollthedice/backend/global/config/QuerydslConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.rollthedice.backend.global.config; | ||
|
||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
|
||
@Configuration | ||
public class QuerydslConfig { | ||
|
||
private final EntityManager em; | ||
|
||
@Autowired | ||
public QuerydslConfig(EntityManager em) { | ||
this.em = em; | ||
} | ||
|
||
@Bean | ||
public JPAQueryFactory jpaQueryFactory() { | ||
return new JPAQueryFactory(em); | ||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
...n/news/controller/NewsControllerTest.java → ...d/domain/news/api/NewsControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
.../src/test/java/com/rollthedice/backend/domain/news/repository/ReadNewsRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.rollthedice.backend.domain.news.repository; | ||
|
||
import com.rollthedice.backend.domain.member.entity.Member; | ||
import com.rollthedice.backend.domain.member.repository.MemberRepository; | ||
import com.rollthedice.backend.domain.news.entity.News; | ||
import com.rollthedice.backend.domain.news.entity.NewsCategory; | ||
import com.rollthedice.backend.domain.news.entity.ReadNews; | ||
import com.rollthedice.backend.support.RepositoryTest; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.time.LocalDate; | ||
|
||
import static com.rollthedice.backend.domain.member.MemberFixture.MEMBER; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@DisplayName("ReadNewsRepository의 ") | ||
@RepositoryTest | ||
public class ReadNewsRepositoryTest { | ||
@Autowired | ||
private ReadNewsRepository readNewsRepository; | ||
@Autowired | ||
private NewsRepository newsRepository; | ||
@Autowired | ||
private MemberRepository memberRepository; | ||
|
||
private Member member; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
member = memberRepository.save(MEMBER()); | ||
} | ||
|
||
@Test | ||
@DisplayName("하루동안 조회한 뉴스 개수를 반환할 수 있는가") | ||
void getCountOfReadNewsByDate() { | ||
//given | ||
int expect = 5; | ||
for (int i = 0; i < expect; i++) { | ||
readNewsRepository.save(ReadNews.builder().member(member).news(newsRepository.save(News.builder().id((long) i).build())).build()); | ||
} | ||
|
||
//when | ||
Long result = readNewsRepository.getCountOfReadNewsByDate(member, LocalDate.now()); | ||
|
||
//then | ||
assertThat(result).isEqualTo(expect); | ||
} | ||
|
||
@Test | ||
@DisplayName("회원이 카테고리별로 조회한 뉴스 개수를 반환할 수 있는가") | ||
void getCountOfReadNewsByCategory() { | ||
//given | ||
int expect = 5; | ||
String category = NewsCategory.SCIENCE.getName(); | ||
for (int i = 0; i < expect; i++) { | ||
readNewsRepository.save(ReadNews.builder().member(member) | ||
.news(newsRepository.save( | ||
News.builder().id((long) i).category(category).build())).build()); | ||
} | ||
|
||
//when | ||
Long result = readNewsRepository.getCountOfReadNewsByCategory(member, category); | ||
|
||
//then | ||
assertThat(result).isEqualTo(expect); | ||
} | ||
} |
Oops, something went wrong.