-
Notifications
You must be signed in to change notification settings - Fork 1
소환사 닉네임 검색하면 matchId 20개 불러와서 저장하는 코드 #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kanginkug
wants to merge
17
commits into
main
Choose a base branch
from
Feature/search_user_info
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
22b8467
fix: riot api key 입력
kanginkug 6acc76f
fix: api로 데이터 불러오기 위한 dependency 추가
kanginkug b1ce542
feat: summoner v4를 받아오기 위한 엔티티 생성
kanginkug ac08ce7
feat: summoner v4를 받아오기 위한 클래스 생성
kanginkug 4b38dfe
feat: summoner v4를 받아오기 위한 서비스 생성 403에러가 뜨기 때문에 수정해야한다.
kanginkug 627b3a5
fix: riot api key 수정
kanginkug 05d274a
fix: 생성자를 만드는 어노테이션 추가
kanginkug fca6b53
feat: SummonerRepository 생성
kanginkug 6223506
feat: SummonerResponseDto 생성
kanginkug 6371a36
fix: 상태코드가 200이 아니면 아이디가 존재하지 않는다는 에러메세지 출력 & summonerResponseDto를 이용…
kanginkug 53b9625
feat: matchId를 저장하는 엔티티 생성
kanginkug ce2a5c4
feat: MatchIdRepository 새엇ㅇ
kanginkug c6d6922
feat: MatchIdResponseDto 생성
kanginkug 61e0cde
fix: summoner엔티티에 matchIds컬럼을 추가했기 때문에 생성자도 수정해야 했다. 그리고 카멜 표기법도 스네이크…
kanginkug 3dcb425
fix: matchId를 불러와 저장하기 위한 controller를 만들었다. 이것을 SummonerController에 추…
kanginkug 8dd3d18
fix: puuid로 소환사 정보를 불러오는 jpa 생성
kanginkug 0b82081
fix: matchId를 불러와서 puuid로 소환사 정보를 찾아 온 후 matchId 테이블에 summoner의 id와 m…
kanginkug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 lbs.lbs.controller; | ||
|
|
||
| import lbs.lbs.entity.MatchId; | ||
| import lbs.lbs.entity.Summoner; | ||
| import lbs.lbs.service.SummonerService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.net.URLEncoder; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/summoners") | ||
| @RequiredArgsConstructor | ||
| public class SummonerController { | ||
|
|
||
| private final SummonerService summonerService; | ||
|
|
||
| @GetMapping("/{summonerName}") | ||
| public Summoner callSummonerByName(@PathVariable(name = "summonerName") String summonerName) { | ||
| // URL 인코딩 사용 | ||
| summonerName = URLEncoder.encode(summonerName, StandardCharsets.UTF_8); | ||
| Summoner apiResult = summonerService.callRiotAPISummonerByName(summonerName); | ||
| return apiResult; | ||
| } | ||
|
|
||
| @PostMapping("/match-ids/{puuid}") | ||
| public List<MatchId> getMatchIds(@PathVariable(name = "puuid") String puuid) { | ||
| List<MatchId> matchIds = summonerService.getMatchIds(puuid); | ||
|
|
||
| return matchIds; | ||
| } | ||
| } |
This file contains hidden or 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,12 @@ | ||
| package lbs.lbs.dto; | ||
|
|
||
| import lbs.lbs.entity.MatchId; | ||
| import lombok.Getter; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @Getter | ||
| public class MatchIdResponseDto { | ||
| List<MatchId> matchIdList = new ArrayList<>(); | ||
| } |
This file contains hidden or 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 lbs.lbs.dto; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class SummonerResponseDto { | ||
| private String id; | ||
|
|
||
| private String accountId; | ||
| private String revisionDate; | ||
| private String profileIconId; | ||
| private String puuid; | ||
| private String name; | ||
| private int summonerLevel; | ||
| } |
This file contains hidden or 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,27 @@ | ||
| package lbs.lbs.entity; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @NoArgsConstructor | ||
| public class MatchId { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "id") | ||
| private Long id; | ||
|
|
||
| private String match_id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| private Summoner summoner; | ||
|
|
||
|
|
||
| public MatchId(String match_id, Summoner summoner) { | ||
| this.match_id = match_id; | ||
| this.summoner = summoner; | ||
| } | ||
| } |
This file contains hidden or 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,39 @@ | ||
| package lbs.lbs.entity; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.OneToMany; | ||
| import lombok.*; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Setter | ||
| @Builder | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| public class Summoner { | ||
| @Id @Column(name = "summoner_id") | ||
| private String id; | ||
|
|
||
| private String profile_icon_id; | ||
| private String puuid; | ||
|
|
||
| @OneToMany(mappedBy = "summoner") | ||
| private List<MatchId> matchIds; | ||
|
|
||
| private String summoner_name; | ||
| private int summoner_level; | ||
|
|
||
| public Summoner(String id, String profile_icon_id, String puuid, String summoner_name, int summoner_level) { | ||
| this.id = id; | ||
| this.profile_icon_id = profile_icon_id; | ||
| this.puuid = puuid; | ||
| this.summoner_name = summoner_name; | ||
| this.summoner_level = summoner_level; | ||
| } | ||
|
|
||
|
|
||
| } |
This file contains hidden or 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,9 @@ | ||
| package lbs.lbs.repository; | ||
|
|
||
| import lbs.lbs.entity.MatchId; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface MatchIdRepository extends JpaRepository<MatchId, Long> { | ||
| } |
This file contains hidden or 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,14 @@ | ||
| package lbs.lbs.repository; | ||
|
|
||
| import lbs.lbs.entity.Summoner; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| @Repository | ||
| public interface SummonerRepository extends JpaRepository<Summoner, String> { | ||
| boolean existsById(String id); | ||
| Optional<Summoner> findById(String id); | ||
| Optional<Summoner> findByPuuid(String puuid); | ||
| } |
This file contains hidden or 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,104 @@ | ||
| package lbs.lbs.service; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import lbs.lbs.dto.MatchIdResponseDto; | ||
| import lbs.lbs.dto.SummonerResponseDto; | ||
| import lbs.lbs.entity.MatchId; | ||
| import lbs.lbs.entity.Summoner; | ||
| import lbs.lbs.repository.MatchIdRepository; | ||
| import lbs.lbs.repository.SummonerRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.apache.http.HttpEntity; | ||
| import org.apache.http.HttpResponse; | ||
| import org.apache.http.client.methods.CloseableHttpResponse; | ||
| import org.apache.http.client.methods.HttpGet; | ||
| import org.apache.http.impl.client.CloseableHttpClient; | ||
| import org.apache.http.impl.client.HttpClients; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class SummonerService { | ||
| private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| private final SummonerRepository summonerRepository; | ||
|
|
||
| private final MatchIdRepository matchIdRepository; | ||
| private final CloseableHttpClient httpClient = HttpClients.createDefault(); | ||
|
|
||
| @Value("${riot.api.key}") | ||
| private String myKey; | ||
|
|
||
| public Summoner callRiotAPISummonerByName(String summonerName) { | ||
| try { | ||
| HttpGet request = new HttpGet("https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/"+ summonerName + "?api_key=" + myKey); | ||
| try (CloseableHttpResponse response = httpClient.execute(request)) { | ||
| int statusCode = response.getStatusLine().getStatusCode(); | ||
|
|
||
| if (statusCode != 200) { | ||
| throw new NullPointerException("해당 아이디는 존재하지 않습니다."); | ||
| } | ||
| HttpEntity entity = response.getEntity(); | ||
| SummonerResponseDto summonerResponseDto = objectMapper.readValue(entity.getContent(), SummonerResponseDto.class); | ||
|
|
||
| if(summonerRepository.existsById(summonerResponseDto.getId())){ | ||
| return summonerRepository.findById(summonerResponseDto.getId()).orElseThrow(() -> | ||
| new NullPointerException("해당 아이디는 존재하지 않습니다.")); | ||
|
|
||
| } | ||
| Summoner summoner = new Summoner(summonerResponseDto.getId(), summonerResponseDto.getProfileIconId(), | ||
| summonerResponseDto.getPuuid(),summonerResponseDto.getName(), summonerResponseDto.getSummonerLevel()); | ||
|
|
||
| summonerRepository.save(summoner); | ||
| return summoner; | ||
| } | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public List<MatchId> getMatchIds(String puuid) { | ||
| try { | ||
| HttpGet request = new HttpGet("https://asia.api.riotgames.com/lol/match/v5/matches/by-puuid/" + puuid + "/ids?start=0&count=20" + "&api_key=" + myKey); | ||
| try (CloseableHttpResponse response = httpClient.execute(request)) { | ||
|
|
||
| int statusCode = response.getStatusLine().getStatusCode(); | ||
|
|
||
| if (statusCode != 200) { | ||
| throw new NullPointerException("해당 puuid가 존재하지 않습니다.."); | ||
| } | ||
|
|
||
| HttpEntity entity = response.getEntity(); | ||
| List<String> matchIdList = objectMapper.readValue(entity.getContent(), List.class); | ||
|
|
||
| Summoner summoner = summonerRepository.findByPuuid(puuid).orElseThrow(() -> | ||
| new NullPointerException("해당 아이디는 존재하지 않습니다.")); | ||
|
|
||
| // matchIds를 설정할 때 builder() 대신에 set 메서드를 사용하여 id를 할당 | ||
| summoner.setMatchIds(matchIdList.stream() | ||
| .map(matchId -> new MatchId(matchId, summoner)) | ||
| .collect(Collectors.toList())); | ||
|
|
||
| summonerRepository.save(summoner); | ||
|
|
||
| // MatchId 저장 부분도 변경 | ||
| List<MatchId> savedMatchIds = matchIdRepository.saveAll( | ||
| matchIdList.stream() | ||
| .map(matchIdStr -> new MatchId(matchIdStr, summoner)) | ||
| .collect(Collectors.toList()) | ||
| ); | ||
|
|
||
| return savedMatchIds; | ||
| } | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| return null; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spring boot 는 버전 정보 안 써주면 알아서 호환되는걸로 맞춰서 지워주는게 좋다고 아는데 어떻게생각하시나요?