Skip to content

Commit

Permalink
[#26]Refactor: GitHub와 Boj의 Service를 User에서 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobhboy committed May 10, 2023
1 parent d3226f8 commit 46c4c8d
Show file tree
Hide file tree
Showing 86 changed files with 154 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
"groups": [
{
"name": "spring.security.oauth2.user.github",
"type": "bssm.db.bssmgit.domain.user.service.AuthProperties",
"sourceType": "bssm.db.bssmgit.domain.user.service.AuthProperties"
"type": "bssm.db.bssmgit.domain.github.service.AuthProperties",
"sourceType": "bssm.db.bssmgit.domain.github.service.AuthProperties"
}
],
"properties": [
{
"name": "spring.security.oauth2.user.github.client-id",
"type": "java.lang.String",
"sourceType": "bssm.db.bssmgit.domain.user.service.AuthProperties"
"sourceType": "bssm.db.bssmgit.domain.github.service.AuthProperties"
},
{
"name": "spring.security.oauth2.user.github.client-secret",
"type": "java.lang.String",
"sourceType": "bssm.db.bssmgit.domain.user.service.AuthProperties"
"sourceType": "bssm.db.bssmgit.domain.github.service.AuthProperties"
},
{
"name": "spring.security.oauth2.user.github.redirect-url",
"type": "java.lang.String",
"sourceType": "bssm.db.bssmgit.domain.user.service.AuthProperties"
"sourceType": "bssm.db.bssmgit.domain.github.service.AuthProperties"
}
],
"hints": []
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bssm.db.bssmgit.domain.baekjoon.domain;
package bssm.db.bssmgit.domain.boj.domain;

import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -9,7 +9,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BaekJoon {
public class Boj {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package bssm.db.bssmgit.domain.boj.domain.repository;

import bssm.db.bssmgit.domain.boj.domain.Boj;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BojRepository extends JpaRepository<Boj, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bssm.db.bssmgit.domain.user.service;
package bssm.db.bssmgit.domain.boj.service;

import bssm.db.bssmgit.domain.user.domain.User;
import bssm.db.bssmgit.domain.boj.domain.Boj;
import bssm.db.bssmgit.domain.boj.domain.repository.BojRepository;
import bssm.db.bssmgit.domain.user.facade.UserFacade;
import bssm.db.bssmgit.domain.user.web.dto.response.BojAuthenticationResultResDto;
import bssm.db.bssmgit.domain.user.web.dto.response.BojJsonResponseDto;
Expand All @@ -18,7 +19,6 @@
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import static bssm.db.bssmgit.global.exception.ErrorCode.USER_NOT_FOUND;
Expand All @@ -30,14 +30,38 @@
@Service
public class BojService {

private final BojRepository bojRepository;
private final UserFacade userFacade;
private final OkHttpClient okHttpClient;

public static String createKey() {
StringBuilder key = new StringBuilder();
Random rnd = new Random();

for (int i = 0; i < 8; i++) {
int index = rnd.nextInt(3);

switch (index) {
case 0:
key.append((char) (rnd.nextInt(26) + 97));
break;
case 1:
key.append((char) ((rnd.nextInt(26)) + 65));
break;
case 2:
key.append((rnd.nextInt(10)));
break;
}
}

return key.toString();
}

@Transactional
public BojAuthenticationResultResDto matchedCode() throws IOException {
User user = userFacade.getCurrentUser();
Boj boj = userFacade.getCurrentUser().getBoj();
Request tokenRequest = new Request.Builder()
.url(Constants.BOJ_URL + user.getBojAuthId())
.url(Constants.BOJ_URL + boj.getBojAuthId())
.build();
Response bojResponse = okHttpClient.newCall(tokenRequest).execute();
if (bojResponse.code() == 404) {
Expand All @@ -50,67 +74,45 @@ public BojAuthenticationResultResDto matchedCode() throws IOException {
Gson gson = new Gson();
BojJsonResponseDto info = gson.fromJson(result, BojJsonResponseDto.class);

if (user.getRandomCode().equals(info.getBio())) {
user.updateUserBojInfo(
user.getBojAuthId(),
if (boj.getRandomCode().equals(info.getBio())) {
boj.updateUserBojInfo(
boj.getBojAuthId(),
info.getSolvedCount(),
info.getTier(),
info.getRating(),
info.getMaxStreak(),
info.getProfileImageUrl(),
info.getBio()
);
userFacade.save(user);
bojRepository.save(boj);
}

return new BojAuthenticationResultResDto(user);
return new BojAuthenticationResultResDto(boj);
}

@Transactional
public RandomCodeResponseDto getRandomCode(String bojId) {
User user = userFacade.getCurrentUser();
Boj boj = userFacade.getCurrentUser().getBoj();
String key = createKey();
user.updateBojAuthId(bojId);
user.updateRandomCode(key);
userFacade.save(user);
boj.updateBojAuthId(bojId);
boj.updateRandomCode(key);
bojRepository.save(boj);

return new RandomCodeResponseDto(key);
}

public static String createKey() {
StringBuilder key = new StringBuilder();
Random rnd = new Random();

for (int i = 0; i < 8; i++) {
int index = rnd.nextInt(3);

switch (index) {
case 0:
key.append((char) (rnd.nextInt(26) + 97));
break;
case 1:
key.append((char) ((rnd.nextInt(26)) + 65));
break;
case 2:
key.append((rnd.nextInt(10)));
break;
}
}

return key.toString();
}

@Scheduled(cron = EVERY_5MINUTES) // 매일 새벽 4시
public void updateUserBojInfo() {
ArrayList<User> users = new ArrayList<>();

userFacade.findAll().stream().filter(u -> u.getBojId() != null)
.forEach(u -> {
bojRepository.findAll().stream().filter(b -> b.getBojId() != null)
.forEach(b -> {
// Request
Request tokenRequest = new Request.Builder()
.url(BOJ_URL + u.getBojAuthId())
.url(BOJ_URL + b.getBojAuthId())
.build();
Response bojResponse = null;

Response bojResponse;

try {
bojResponse = okHttpClient.newCall(tokenRequest).execute();
} catch (IOException e) {
Expand All @@ -121,7 +123,8 @@ public void updateUserBojInfo() {
}

assert bojResponse.body() != null;
String result = null;
String result;

try {
result = bojResponse.body().string();
} catch (IOException e) {
Expand All @@ -131,22 +134,17 @@ public void updateUserBojInfo() {
Gson gson = new Gson();
BojJsonResponseDto info = gson.fromJson(result, BojJsonResponseDto.class);

u.updateUserBojInfo(
u.getBojAuthId(),
b.updateUserBojInfo(
b.getBojAuthId(),
info.getSolvedCount(),
info.getTier(),
info.getRating(),
info.getMaxStreak(),
info.getProfileImageUrl(),
info.getBio()
);

users.add(u);
}
);

userFacade.saveAll(users);

}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package bssm.db.bssmgit.domain.github.domain;

import bssm.db.bssmgit.domain.user.domain.type.Imaginary;
import bssm.db.bssmgit.domain.github.domain.type.Imaginary;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package bssm.db.bssmgit.domain.github.domain.repository;

import bssm.db.bssmgit.domain.github.domain.GitHub;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface GitHubRepository extends JpaRepository<GitHub, Long> {

@Query("select g from GitHub g " +
"where g.imaginary = " +
"bssm.db.bssmgit.domain.github.domain.type.Imaginary.IMAGINARY_NUMBER")
List<GitHub> findGitHubsByImaginary();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bssm.db.bssmgit.domain.user.domain.type;
package bssm.db.bssmgit.domain.github.domain.type;

public enum Imaginary {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bssm.db.bssmgit.domain.user.service;
package bssm.db.bssmgit.domain.github.service;

import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package bssm.db.bssmgit.domain.user.service;
package bssm.db.bssmgit.domain.github.service;

import bssm.db.bssmgit.domain.github.domain.GitHub;
import bssm.db.bssmgit.domain.github.domain.repository.GitHubRepository;
import bssm.db.bssmgit.domain.user.domain.User;
import bssm.db.bssmgit.domain.user.facade.UserFacade;
import bssm.db.bssmgit.domain.user.service.UserService;
import bssm.db.bssmgit.domain.user.web.dto.UserProfile;
import bssm.db.bssmgit.domain.user.web.dto.request.OauthAttributes;
import bssm.db.bssmgit.domain.user.web.dto.response.CookieResponseDto;
Expand All @@ -16,7 +19,6 @@
import bssm.db.bssmgit.global.util.CookieProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
Expand Down Expand Up @@ -46,6 +48,7 @@
public class AuthService {

private final UserFacade userFacade;
private final GitHubRepository gitHubRepository;
private final JwtProvider jwtProvider;
private final JwtValidateService jwtValidateService;
private final CookieProvider cookieProvider;
Expand All @@ -70,7 +73,7 @@ public class AuthService {
@Value("${spring.oauth.git.url.token}")
String token;

GitHub github;
org.kohsuke.github.GitHub github;

@Transactional
public CookieResponseDto bsmLogin(String authCode) throws IOException {
Expand Down Expand Up @@ -125,21 +128,21 @@ public GitLoginResponseDto gitLogin(String code) throws IOException {

UserProfile userProfile = getUserProfile(tokenResponse);

User user = userFacade.getCurrentUser();
if (user.getGithubId() == null) {
GitHub userGitHub = userFacade.getCurrentUser().getGitHub();
if (userGitHub.getGithubId() == null) {

user.updateGitId(userProfile.getGitId());
userGitHub.updateGitId(userProfile.getGitId());

int commits = getUserCommit(userProfile.getGitId());
String bio = github.getUser(userProfile.getGitId()).getBio();
String img = github.getUser(userProfile.getGitId()).getAvatarUrl();

user.updateGitInfo(commits, bio, img);
userFacade.save(user);
userGitHub.updateGitInfo(commits, bio, img);
gitHubRepository.save(userGitHub);
}


return new GitLoginResponseDto(user.getGithubId());
return new GitLoginResponseDto(userGitHub.getGithubId());
}

public int getUserCommit(String githubId) {
Expand Down
Loading

0 comments on commit 46c4c8d

Please sign in to comment.