Skip to content

Commit 474748a

Browse files
committed
Merge branch 'dev' of https://github.com/CommitField/commitField into feat/#20
2 parents 0713f02 + e00466b commit 474748a

File tree

3 files changed

+100
-19
lines changed

3 files changed

+100
-19
lines changed

src/main/java/cmf/commitField/domain/commit/totalCommit/controller/TotalCommitController.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import cmf.commitField.domain.commit.totalCommit.dto.TotalCommitResponseDto;
44
import cmf.commitField.domain.commit.totalCommit.service.TotalCommitService;
5+
import cmf.commitField.domain.user.entity.CustomOAuth2User;
56
import lombok.RequiredArgsConstructor;
7+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
68
import org.springframework.web.bind.annotation.GetMapping;
7-
import org.springframework.web.bind.annotation.PathVariable;
89
import org.springframework.web.bind.annotation.RestController;
910

1011
import java.time.LocalDateTime;
@@ -15,41 +16,62 @@
1516
public class TotalCommitController {
1617
private final TotalCommitService totalCommitService;
1718

18-
@GetMapping("/api/commits/{username}")
19-
public TotalCommitResponseDto getTotalCommits(@PathVariable String username) {
19+
// @GetMapping("/api/commits/{username}")
20+
// public TotalCommitResponseDto getTotalCommits(@PathVariable String username) {
21+
// return totalCommitService.getTotalCommitCount(username);
22+
// }
23+
24+
// 로그인한 사용자의 username 이용
25+
@GetMapping("/api/commits")
26+
public TotalCommitResponseDto getTotalCommits(@AuthenticationPrincipal CustomOAuth2User oAuth2User) {
27+
String username = oAuth2User.getName(); // CustomOAuth2User의 getName()은 user.getUsername()을 반환
28+
2029
return totalCommitService.getTotalCommitCount(username);
2130
}
2231

2332
// 봄 시즌(3/1 - 5/31)
24-
@GetMapping("/api/commits/{username}/spring")
25-
public TotalCommitResponseDto getSpringSeasonCommits(@PathVariable String username) {
33+
// @GetMapping("/api/commits/{username}/spring")
34+
// public TotalCommitResponseDto getSpringSeasonCommits(@PathVariable String username) {
35+
// int currentYear = LocalDateTime.now().getYear(); // 현재는 테스트용으로 2024 대입
36+
// LocalDateTime since = LocalDateTime.of(2024, 3, 1, 0, 0);
37+
// LocalDateTime until = LocalDateTime.of(2024, 5, 31, 23, 59, 59);
38+
// return totalCommitService.getSeasonCommits(username, since, until);
39+
// }
40+
41+
// 마찬가지로 로그인한 사용자의 username 이용
42+
@GetMapping("/api/commits/spring")
43+
public TotalCommitResponseDto getSpringSeasonCommits(@AuthenticationPrincipal CustomOAuth2User oAuth2User) {
44+
String username = oAuth2User.getName();
2645
int currentYear = LocalDateTime.now().getYear(); // 현재는 테스트용으로 2024 대입
2746
LocalDateTime since = LocalDateTime.of(2024, 3, 1, 0, 0);
2847
LocalDateTime until = LocalDateTime.of(2024, 5, 31, 23, 59, 59);
2948
return totalCommitService.getSeasonCommits(username, since, until);
3049
}
3150

3251
// 여름 시즌(6/1 - 8/31)
33-
@GetMapping("/api/commits/{username}/summer")
34-
public TotalCommitResponseDto getSummerSeasonCommits(@PathVariable String username) {
52+
@GetMapping("/api/commits/summer")
53+
public TotalCommitResponseDto getSummerSeasonCommits(@AuthenticationPrincipal CustomOAuth2User oAuth2User) {
54+
String username = oAuth2User.getName();
3555
int currentYear = LocalDateTime.now().getYear(); // 현재는 테스트용으로 2024 대입
3656
LocalDateTime since = LocalDateTime.of(2024, 6, 1, 0, 0);
3757
LocalDateTime until = LocalDateTime.of(2024, 8, 31, 23, 59, 59);
3858
return totalCommitService.getSeasonCommits(username, since, until);
3959
}
4060

4161
// 가을 시즌(9/1 - 11/30)
42-
@GetMapping("/api/commits/{username}/fall")
43-
public TotalCommitResponseDto getFallSeasonCommits(@PathVariable String username) {
62+
@GetMapping("/api/commits/fall")
63+
public TotalCommitResponseDto getFallSeasonCommits(@AuthenticationPrincipal CustomOAuth2User oAuth2User) {
64+
String username = oAuth2User.getName();
4465
int currentYear = LocalDateTime.now().getYear(); // 현재는 테스트용으로 2024 대입
4566
LocalDateTime since = LocalDateTime.of(2024, 9, 1, 0, 0);
4667
LocalDateTime until = LocalDateTime.of(2024, 11, 30, 23, 59, 59);
4768
return totalCommitService.getSeasonCommits(username, since, until);
4869
}
4970

5071
// 겨울 시즌(이전 년도 12/1 - 다음 년도 2/28)
51-
@GetMapping("/api/commits/{username}/winter")
52-
public TotalCommitResponseDto getWinterSeasonCommits(@PathVariable String username) {
72+
@GetMapping("/api/commits/winter")
73+
public TotalCommitResponseDto getWinterSeasonCommits(@AuthenticationPrincipal CustomOAuth2User oAuth2User) {
74+
String username = oAuth2User.getName();
5375
int currentYear = LocalDateTime.now().getYear(); // 현재는 테스트용으로 2024 대입
5476
LocalDateTime since = LocalDateTime.of(2024 - 1, 12, 1, 0, 0);
5577
LocalDateTime until = LocalDateTime.of(2024, 2, 1, 23, 59, 59)

src/main/java/cmf/commitField/domain/commit/totalCommit/service/TotalCommitService.java

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,87 @@ public class TotalCommitService {
3232
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
3333
.build();
3434

35-
// 기존 메서드
35+
// // 기존 메서드
36+
// public TotalCommitResponseDto getTotalCommitCount(String username) {
37+
// Map<String, String> requestBody = Map.of(
38+
// "query", String.format(
39+
// "query { user(login: \"%s\") { contributionsCollection { totalCommitContributions restrictedContributionsCount } } }",
40+
// username
41+
// )
42+
// );
43+
//
44+
// TotalCommitGraphQLResponse response = webClient.post()
45+
// .header("Authorization", "bearer " + PAT)
46+
// .bodyValue(requestBody)
47+
// .retrieve()
48+
// .bodyToMono(TotalCommitGraphQLResponse.class)
49+
// .block();
50+
//
51+
// TotalCommitGraphQLResponse.ContributionsCollection contributions =
52+
// response.getData().getUser().getContributionsCollection();
53+
//
54+
// return new TotalCommitResponseDto(
55+
// contributions.getTotalCommitContributions(),
56+
// contributions.getRestrictedContributionsCount()
57+
// );
58+
// // streak 계산 부분 추가
59+
// List<LocalDate> commitDates = extractCommitDates(contributions.getContributionCalendar());
60+
// StreakResult streaks = calculateStreaks(commitDates);
61+
//
62+
// return new TotalCommitResponseDto(
63+
// contributions.getTotalCommitContributions(),
64+
// contributions.getRestrictedContributionsCount(),
65+
// streaks.currentStreak,
66+
// streaks.maxStreak
67+
// );
68+
// }
69+
70+
// 연속 커밋 수 정보도 같이 반환
3671
public TotalCommitResponseDto getTotalCommitCount(String username) {
37-
Map<String, String> requestBody = Map.of(
38-
"query", String.format(
39-
"query { user(login: \"%s\") { contributionsCollection { totalCommitContributions restrictedContributionsCount } } }",
40-
username
41-
)
72+
// GraphQL 쿼리를 수정하여 contributionCalendar도 함께 요청
73+
String query = String.format(
74+
"query { user(login: \"%s\") { contributionsCollection { " +
75+
"totalCommitContributions restrictedContributionsCount " +
76+
"contributionCalendar { totalContributions weeks { contributionDays { contributionCount date } } } " +
77+
"} } }",
78+
username
4279
);
4380

81+
Map<String, String> requestBody = Map.of("query", query);
82+
4483
TotalCommitGraphQLResponse response = webClient.post()
4584
.header("Authorization", "bearer " + PAT)
4685
.bodyValue(requestBody)
4786
.retrieve()
4887
.bodyToMono(TotalCommitGraphQLResponse.class)
4988
.block();
5089

90+
if (response == null || response.getData() == null || response.getData().getUser() == null) {
91+
throw new RuntimeException("Failed to fetch GitHub data");
92+
}
93+
5194
TotalCommitGraphQLResponse.ContributionsCollection contributions =
5295
response.getData().getUser().getContributionsCollection();
5396

97+
// streak 기본값 설정
98+
int currentStreak = 0;
99+
int maxStreak = 0;
100+
101+
// contributionCalendar가 존재하는 경우에만 streak 계산
102+
if (contributions.getContributionCalendar() != null) {
103+
List<LocalDate> commitDates = extractCommitDates(contributions.getContributionCalendar());
104+
if (!commitDates.isEmpty()) {
105+
StreakResult streaks = calculateStreaks(commitDates);
106+
currentStreak = streaks.currentStreak;
107+
maxStreak = streaks.maxStreak;
108+
}
109+
}
110+
54111
return new TotalCommitResponseDto(
55112
contributions.getTotalCommitContributions(),
56-
contributions.getRestrictedContributionsCount()
113+
contributions.getRestrictedContributionsCount(),
114+
currentStreak,
115+
maxStreak
57116
);
58117
}
59118

src/main/resources/application-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ spring:
1111
autoconfigure: # 로컬에서 실행할 때는 Redis와 Session 설정을 제외
1212
exclude:
1313
- org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
14-
- org.springframework.boot.autoconfigure.session.SessionAutoConfiguration
14+
- org.springframework.boot.autoconfigure.session.SessionAutoConfiguration

0 commit comments

Comments
 (0)