-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Leets-Official/2-fearture-searchrank
2 fearture searchrank
- Loading branch information
Showing
14 changed files
with
267 additions
and
25 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
82 changes: 82 additions & 0 deletions
82
src/main/java/com/leets/commitatobe/domain/commit/usecase/ExpService.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,82 @@ | ||
package com.leets.commitatobe.domain.commit.usecase; | ||
|
||
import com.leets.commitatobe.domain.commit.domain.Commit; | ||
import com.leets.commitatobe.domain.commit.domain.repository.CommitRepository; | ||
import com.leets.commitatobe.domain.tier.domain.Tier; | ||
import com.leets.commitatobe.domain.tier.domain.repository.TierRepository; | ||
import com.leets.commitatobe.domain.user.domain.User; | ||
import com.leets.commitatobe.domain.user.domain.repository.UserRepository; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class ExpService { | ||
private final CommitRepository commitRepository; | ||
private final UserRepository userRepository; | ||
private final TierRepository tierRepository; | ||
|
||
public void calculateAndSaveExp(String githubId){ | ||
User user=userRepository.findByGithubId(githubId) | ||
.orElseThrow(()->new UsernameNotFoundException("ํด๋นํ๋ ๊นํ๋ธ ๋๋ค์๊ณผ ์ผ์นํ๋ ์ ์ ๋ฅผ ์ฐพ์ ์ ์์: " +githubId)); | ||
List<Commit> commits=commitRepository.findAllByUserOrderByCommitDateAsc(user);//์ฌ์ฉ์์ ๋ชจ๋ ์ปค๋ฐ์ ๋ ์ง ์ค๋ฆ์ฐจ์์ผ๋ก ๋ถ๋ฌ์จ๋ค. | ||
|
||
int consecutiveDays=0;//์ฐ์ ์ปค๋ฐ ์ผ์ | ||
LocalDateTime lastCommitDate=null;//๋ง์ง๋ง ์ปค๋ฐ ๋ ์ง | ||
int totalExp=user.getExp()!=null?user.getExp():0;//์ฌ์ฉ์์ ํ์ฌ ๊ฒฝํ์น, user.getExp()๊ฐ null์ธ ๊ฒฝ์ฐ 0์ผ๋ก ์ด๊ธฐํ | ||
int dailyBonusExp=100;//๋ฐ์ผ๋ฆฌ ๋ณด๋์ค ๊ฒฝํ์น | ||
int bonusExpIncrease=10;//๋ณด๋์ค ๊ฒฝํ์น ์ฆ๊ฐ๋ | ||
int totalCommitCount=0;//์ด ์ปค๋ฐ ํ์ | ||
int todayCommitCount=0;//์ค๋ ์ปค๋ฐ ํ์ | ||
|
||
LocalDate today=LocalDate.now();//์ค๋ ๋ ์ง | ||
|
||
for(Commit commit:commits){//๊ฐ ์ปค๋ฐ์ ๋ฐ๋ณตํด์ ๊ณ์ฐ | ||
if(commit.isCalculated()) continue;//์ด๋ฏธ ๊ณ์ฐ๋ ์ปค๋ฐ | ||
LocalDateTime commitDate=commit.getCommitDate();//์ปค๋ฐ๋ ์ง๋ฅผ ๊ฐ์ ธ์ ์๊ฐ ์ค์ | ||
|
||
if(lastCommitDate != null && commitDate.isEqual(lastCommitDate.plusDays(1))){ | ||
// ๋ง์ง๋ง ์ปค๋ฐ ๋ ์ง๊ฐ ์กด์ฌํ๊ณ , ํ์ฌ ์ปค๋ฐ ๋ ์ง๊ฐ ๋ง์ง๋ง ์ปค๋ฐ ๋ ์ง์ ๋ค์ ๋ ๊ณผ ๊ฐ์ผ๋ฉด ์ฐ์ ์ปค๋ฐ์ผ๋ก ๊ฐ์ฃผํฉ๋๋ค. | ||
consecutiveDays++;//์ฐ์ ์ปค๋ฐ ์ผ์ 1 ์ฆ๊ฐ | ||
} | ||
else{ | ||
consecutiveDays=0;//์ฐ์ ์ปค๋ฐ ์ผ์ ์ด๊ธฐํ | ||
} | ||
int commitExp=commit.getCnt()*5;//ํ๋ฃจ ์ปค๋ฐ ๊ฒฝํ์น | ||
int bonusExp=dailyBonusExp+consecutiveDays*bonusExpIncrease;//๋ณด๋์ค ๊ฒฝํ์น ๊ณ์ฐ | ||
totalExp+=commitExp+bonusExp;//์ด ๊ฒฝํ์น ์ ๋ฐ์ดํธ | ||
totalCommitCount+=commit.getCnt();//์ด ์ปค๋ฐ ํ์ | ||
|
||
if(commitDate.toLocalDate().isEqual(today)){ | ||
todayCommitCount=commit.getCnt();//์ค๋๋ ์ง์ ์ปค๋ฐ ๊ฐ์ ์นด์ดํธ | ||
} | ||
|
||
commit.updateStatusToCalculated(true);//์ปค๋ฐ ๊ณ์ฐ ์ฌ๋ถ๋ฅผ true๋ก ํด์ ๋ค์ ๊ฒ์ฐ์์ ์ ์ธ | ||
lastCommitDate=commitDate;//๋ง์ง๋ง ์ปค๋ฐ๋ ์ง๋ฅผ ํ์ฌ ์ปค๋ฐ๋ ์ง๋ก ์ ๋ฐ์ดํธ | ||
} | ||
user.updateExp(totalExp);//์ฌ์ฉ์ ๊ฒฝํ์น ์ ๋ฐ์ดํธ | ||
Tier tier=determineTier(user.getExp());//๊ฒฝํ์น์ ๋ฐ๋ฅธ ํฐ์ด ๊ฒฐ์ | ||
user.updateTier(tier); | ||
user.updateConsecutiveCommitDays(consecutiveDays); | ||
user.updateTotalCommitCount(totalCommitCount); | ||
user.updateTodayCommitCount(todayCommitCount); | ||
|
||
commitRepository.saveAll(commits);//๋ณ๊ฒฝ๋ ์ปค๋ฐ ์ ๋ณด ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ | ||
userRepository.save(user);//๋ณ๊ฒฝ๋ ์ฌ์ฉ์ ์ ๋ณด ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ | ||
} | ||
private Tier determineTier(Integer exp){ | ||
return tierRepository.findAll() | ||
.stream() | ||
.filter(tier->tier.getRequiredExp()!=null&&tier.getRequiredExp()<=exp) | ||
.max(Comparator.comparing(Tier::getRequiredExp)) | ||
.orElseThrow(()->new RuntimeException("ํด๋น ๊ฒฝํ์น์ ํฐ์ด๊ฐ ์์"+exp)); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/leets/commitatobe/domain/tier/domain/repository/TierRepository.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.leets.commitatobe.domain.tier.domain.repository; | ||
|
||
import com.leets.commitatobe.domain.tier.domain.Tier; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
public interface TierRepository extends JpaRepository<Tier, UUID> { | ||
Optional<Tier> findByTierName(String tierName); | ||
} |
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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/leets/commitatobe/domain/user/domain/repository/UserRepository.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 |
---|---|---|
@@ -1,12 +1,20 @@ | ||
package com.leets.commitatobe.domain.user.domain.repository; | ||
|
||
import com.leets.commitatobe.domain.user.domain.User; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.UUID; | ||
|
||
|
||
public interface UserRepository extends JpaRepository<User, UUID> { | ||
Optional<User> findByGithubId(String githubId); | ||
|
||
//๊ฒฝํ์น์์ผ๋ก ์ ์ ๋ฅผ ํ์ด์งํ์ฌ ์กฐํํ๋ ๋ฉ์๋ | ||
Page<User> findAllByOrderByExpDesc(Pageable pageable); | ||
} |
27 changes: 26 additions & 1 deletion
27
src/main/java/com/leets/commitatobe/domain/user/presentation/UserController.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 |
---|---|---|
@@ -1,12 +1,37 @@ | ||
package com.leets.commitatobe.domain.user.presentation; | ||
|
||
import com.leets.commitatobe.domain.user.presentation.dto.response.UserRankResponse; | ||
import com.leets.commitatobe.domain.user.presentation.dto.response.UserSearchResponse; | ||
import com.leets.commitatobe.domain.user.usecase.UserQueryService; | ||
import com.leets.commitatobe.global.response.ApiResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.web.PageableDefault; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/user") | ||
public class UserController { | ||
|
||
private final UserQueryService userQueryService; | ||
@Operation( | ||
summary = "์ ์ ์ ๋ณด ๊ฒ์", | ||
description = "๊นํ๋ธ ์์ด๋๋ก ๊ฒ์ํฉ๋๋ค." | ||
) | ||
@GetMapping("/search") | ||
public ApiResponse<UserSearchResponse> searchUsers(@RequestParam("githubId")String githubId){ | ||
return ApiResponse.onSuccess(userQueryService.searchUsersByGithubId(githubId)); | ||
} | ||
@GetMapping("/ranking")//๊ฒฝํ์น ์์ผ๋ก ์ ์ ์ ๋ณด ์กฐํ ์๋ํฌ์ธํธ | ||
public ApiResponse<Page<UserRankResponse>> getUsersByExp(@PageableDefault(size = 50,sort = "exp",direction = Sort.Direction.DESC) | ||
Pageable pageable){//ํ์ด์ง๋ค์ด์ ์ค์ (ํ์ด์ง:50, exp ๋ด๋ฆผ์ฐจ์) | ||
return ApiResponse.onSuccess(userQueryService.getUsersByExp(pageable)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...in/java/com/leets/commitatobe/domain/user/presentation/dto/response/UserRankResponse.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.leets.commitatobe.domain.user.presentation.dto.response; | ||
|
||
import java.util.UUID; | ||
|
||
public record UserRankResponse( | ||
String username, | ||
Integer exp, | ||
Integer consecutiveCommitDays, | ||
String tierName | ||
) { | ||
} |
13 changes: 13 additions & 0 deletions
13
.../java/com/leets/commitatobe/domain/user/presentation/dto/response/UserSearchResponse.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.leets.commitatobe.domain.user.presentation.dto.response; | ||
|
||
public record UserSearchResponse( | ||
String username, | ||
Integer exp, | ||
String tierName, | ||
String characterUrl, | ||
String badgeUrl, | ||
Integer consecutiveCommitDays, | ||
Integer totalCommitCount, | ||
Integer todayCommitCount | ||
) { | ||
} |
7 changes: 6 additions & 1 deletion
7
src/main/java/com/leets/commitatobe/domain/user/usecase/UserQueryService.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 |
---|---|---|
@@ -1,8 +1,13 @@ | ||
package com.leets.commitatobe.domain.user.usecase; | ||
|
||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import com.leets.commitatobe.domain.user.presentation.dto.response.UserRankResponse; | ||
import com.leets.commitatobe.domain.user.presentation.dto.response.UserSearchResponse; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface UserQueryService { | ||
UserSearchResponse searchUsersByGithubId(String GithubId);//์ ์ ์ด๋ฆ์ผ๋ก ์ ์ ์ ๋ณด๋ฅผ ๊ฒ์ํ๋ ๋ฉ์๋ | ||
Page<UserRankResponse> getUsersByExp(Pageable pageable);//๊ฒฝํ์น ์์ผ๋ก ํ์ด์ง๋ ์ ์ ์ ๋ณด๋ฅผ ์กฐํ | ||
String getUserGitHubAccessToken(String githubId); | ||
} |
Oops, something went wrong.