Skip to content

Commit

Permalink
Merge pull request #12 from Leets-Official/2-fearture-searchrank
Browse files Browse the repository at this point in the history
2 fearture searchrank
  • Loading branch information
1winhyun authored Jul 25, 2024
2 parents 51401ee + ad7b367 commit b75e9a7
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class Commit extends BaseTimeEntity {
@JsonBackReference
private User user;

@Column(name = "is_calculated")
private boolean isCalculated;//๊ฒฝํ—˜์น˜ ๊ณ„์‚ฐ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

public static Commit create(LocalDateTime commitDate, Integer cnt, User user) {
return Commit.builder()
.commitDate(commitDate)
Expand All @@ -49,5 +52,7 @@ public Commit(LocalDateTime commitDate, Integer cnt, User user) {

public void updateCnt(Integer cnt) {
this.cnt = this.cnt + cnt;
this.isCalculated=false;
}
public void updateStatusToCalculated(boolean calculated){isCalculated=calculated;}//isCalculated ํ•„๋“œ ์„ค์ •
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface CommitRepository extends JpaRepository<Commit, UUID> {
List<Commit> findAllByUser(User user);

Optional<Commit> findByCommitDateAndUser(LocalDateTime commitDate, User user);

List<Commit> findAllByUserOrderByCommitDateAsc(User user);
}
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class FetchCommits {
private final GitHubService gitHubService; // GitHub API ํ†ต์‹ 
private final LoginCommandServiceImpl loginCommandService;
private final LoginQueryService loginQueryService;
private final ExpService expService;
private final UserQueryService userQueryService;

public CommitResponse execute(HttpServletRequest request) {
Expand Down Expand Up @@ -79,6 +80,8 @@ public CommitResponse execute(HttpServletRequest request) {

saveCommits(user);

expService.calculateAndSaveExp(gitHubId);//์ปค๋ฐ‹ ๊ฐ€์ ธ์˜จ ํ›„ ๊ฒฝํ—˜์น˜ ๊ณ„์‚ฐ ๋ฐ ์ €์žฅ

} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/leets/commitatobe/domain/tier/domain/Tier.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Tier extends BaseTimeEntity {

@Id @GeneratedValue(strategy = GenerationType.UUID)
Expand All @@ -25,9 +24,20 @@ public class Tier extends BaseTimeEntity {
private String tierName;
private String characterUrl;
private String badgeUrl;

@Column(nullable = false)
private Integer requiredExp;

@OneToMany(mappedBy = "tier")
private List<User> userList;

public Tier(UUID id,String tierName, String characterUrl, String badgeUrl, Integer requiredExp, List<User>userList){
this.id=id;
this.tierName=tierName;
this.characterUrl=characterUrl;
this.badgeUrl=badgeUrl;
this.requiredExp=requiredExp;
this.userList=userList;
}

}
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);
}
45 changes: 36 additions & 9 deletions src/main/java/com/leets/commitatobe/domain/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
import com.leets.commitatobe.domain.tier.domain.Tier;
import com.leets.commitatobe.global.shared.entity.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

import java.util.List;
import java.util.UUID;

@Entity(name = "users")
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class User extends BaseTimeEntity {

Expand All @@ -28,9 +24,6 @@ public class User extends BaseTimeEntity {
@Column
private String gitHubAccessToken;

@Column
private String refreshToken;

@Column(nullable = true)
private String username;

Expand All @@ -40,17 +33,51 @@ public class User extends BaseTimeEntity {
@Column
private String profileImage;

@Column
@Column(nullable = false)
private Integer exp;

@Column
private Integer commitDays;

@Column
private Integer consecutiveCommitDays;

@Column
private Integer totalCommitCount;

@Column
private Integer todayCommitCount;

@OneToMany(mappedBy = "user")
@JsonManagedReference
private List<Commit> commitList;

@ManyToOne
@JoinColumn(name="tier_id")
private Tier tier;

public void updateExp(Integer exp){
this.exp=exp;
}

public void updateTier(Tier tier){
this.tier=tier;
}

public void updateConsecutiveCommitDays(Integer consecutiveCommitDays){
this.consecutiveCommitDays=consecutiveCommitDays;
}

public void updateTotalCommitCount(Integer totalCommitCount){
this.totalCommitCount=totalCommitCount;
}

public void updateTodayCommitCount(Integer todayCommitCount){
this.todayCommitCount=todayCommitCount;
}

public User(){
this.exp=0;
}

}
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);
}
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));
}
}
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
) {
}
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
) {
}
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);
}
Loading

0 comments on commit b75e9a7

Please sign in to comment.