Skip to content

Commit

Permalink
[#26]Refactor: Boj, GitHub Controller를 User에서 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobhboy committed May 11, 2023
1 parent 2d6ed1c commit 379ead3
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package bssm.db.bssmgit.domain.boj.domain.repository;

import bssm.db.bssmgit.domain.boj.web.dto.response.BojResponseDto;

import java.util.List;

public interface CustomBojRepository {

List<BojResponseDto> findAllUserBojDesc();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package bssm.db.bssmgit.domain.boj.domain.repository;

import bssm.db.bssmgit.domain.boj.web.dto.response.BojResponseDto;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

import static bssm.db.bssmgit.domain.boj.domain.QBoj.boj;
import static bssm.db.bssmgit.domain.user.domain.QUser.user;

@Repository
@RequiredArgsConstructor
public class CustomBojRepositoryImpl implements CustomBojRepository {

private final JPAQueryFactory jpaQueryFactory;

@Override
public List<BojResponseDto> findAllUserBojDesc() {
return jpaQueryFactory
.select(Projections.constructor(BojResponseDto.class, user, boj))
.from(boj)
.innerJoin(user)
.fetchJoin()
.where(boj.bojId.isNotNull())
.distinct()
.orderBy(boj.rating.desc())
.fetch();
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
package bssm.db.bssmgit.domain.boj.service;

import bssm.db.bssmgit.domain.github.domain.repository.GitHubRepository;
import bssm.db.bssmgit.domain.github.web.dto.response.GithubResponseDto;
import lombok.AccessLevel;
import bssm.db.bssmgit.domain.boj.domain.repository.CustomBojRepository;
import bssm.db.bssmgit.domain.boj.web.dto.response.BojResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class BojInformationService {

private final GitHubRepository gitHubRepository;
private final CustomBojRepository customBojRepository;

public List<GithubResponseDto> findAllUserGitDesc() {
return gitHubRepository.findAll().stream()
.filter(g -> g.getGithubId() != null)
.filter(g -> g.getCommits() != null)
.map(GithubResponseDto::new)
.collect(Collectors.toList());
public List<BojResponseDto> findAllUserBojDesc() {
return customBojRepository.findAllUserBojDesc();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package bssm.db.bssmgit.domain.boj.web.api;

import bssm.db.bssmgit.domain.boj.service.BojInformationService;
import bssm.db.bssmgit.domain.boj.web.dto.response.BojResponseDto;
import bssm.db.bssmgit.global.generic.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/boj")
@RequiredArgsConstructor
public class BojInformationController {
private final BojInformationService bojInformationService;

@GetMapping("/ranking")
public Result<List<BojResponseDto>> findByGithubCommitDesc() {
List<BojResponseDto> allUserBojDesc = bojInformationService.findAllUserBojDesc();
return new Result<>(allUserBojDesc.size(), allUserBojDesc);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bssm.db.bssmgit.domain.boj.web.dto.response;

import bssm.db.bssmgit.domain.boj.domain.Boj;
import bssm.db.bssmgit.domain.user.domain.User;
import bssm.db.bssmgit.domain.user.web.dto.response.UserResponseDto;
import lombok.Data;
Expand All @@ -16,14 +17,14 @@ public class BojResponseDto {
private final String bojBio;
private final UserResponseDto user;

public BojResponseDto(User user) {
this.bojId = user.getBojId();
this.solvedCount = user.getSolvedCount();
this.rating = user.getRating();
this.tier = user.getTier();
this.maxStreak = user.getMaxStreak();
this.bojImg = user.getBojImg();
this.bojBio = user.getBojBio();
public BojResponseDto(User user, Boj boj) {
this.bojId = boj.getBojId();
this.solvedCount = boj.getSolvedCount();
this.rating = boj.getRating();
this.tier = boj.getTier();
this.maxStreak = boj.getMaxStreak();
this.bojImg = boj.getBojImg();
this.bojBio = boj.getBojBio();
this.user = new UserResponseDto(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public List<GithubResponseDto> getGitHubAndUser() {
return jpaQueryFactory
.select(Projections.constructor(GithubResponseDto.class, user, gitHub))
.from(gitHub)
.where(gitHub.githubId.isNotNull())
.innerJoin(user.gitHub)
.fetchJoin()
.distinct()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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.github.web.dto.GitHubIdDto;
import bssm.db.bssmgit.domain.github.web.dto.request.OauthAttributes;
import bssm.db.bssmgit.domain.user.web.dto.response.CookieResponseDto;
import bssm.db.bssmgit.domain.github.web.dto.response.GitLoginResponseDto;
Expand Down Expand Up @@ -126,16 +126,16 @@ public GitLoginResponseDto gitLogin(String code) throws IOException {
.bodyToMono(OauthTokenResponse.class)
.block();

UserProfile userProfile = getUserProfile(tokenResponse);
GitHubIdDto gitHubIdDto = getUserProfile(tokenResponse);

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

userGitHub.updateGitId(userProfile.getGitId());
userGitHub.updateGitId(gitHubIdDto.getGitHubId());

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

userGitHub.updateGitInfo(commits, bio, img);
gitHubRepository.save(userGitHub);
Expand Down Expand Up @@ -195,7 +195,7 @@ private MultiValueMap<String, String> tokenRequest(String code) {
return formData;
}

private UserProfile getUserProfile(OauthTokenResponse tokenResponse) {
private GitHubIdDto getUserProfile(OauthTokenResponse tokenResponse) {
Map<String, Object> userAttributes = getUserAttributes(tokenResponse);
return OauthAttributes.extract("github", userAttributes);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package bssm.db.bssmgit.domain.github.service;

import bssm.db.bssmgit.domain.github.domain.repository.CustomGithubRepository;
import bssm.db.bssmgit.domain.github.web.dto.response.GithubResponseDto;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@Transactional(readOnly = true)
public class GitHubInformationService {

private final CustomGithubRepository customGithubRepository;

public List<GithubResponseDto> findAllUserGitDesc() {
return customGithubRepository.getGitHubAndUser();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package bssm.db.bssmgit.domain.github.web.api;

import bssm.db.bssmgit.domain.boj.web.dto.response.BojResponseDto;
import bssm.db.bssmgit.domain.github.service.GitHubInformationService;
import bssm.db.bssmgit.domain.github.web.dto.response.GithubResponseDto;
import bssm.db.bssmgit.global.generic.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/github")
@RequiredArgsConstructor
public class GitHubInformationController {

private final GitHubInformationService gitHubInformationService;

@GetMapping("/ranking")
public Result<List<GithubResponseDto>> findByGithubCommitDesc() {
List<GithubResponseDto> allUserGitDesc = gitHubInformationService.findAllUserGitDesc();
return new Result<>(allUserGitDesc.size(), allUserGitDesc);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package bssm.db.bssmgit.domain.github.web.dto;

import bssm.db.bssmgit.domain.github.domain.GitHub;
import bssm.db.bssmgit.domain.user.domain.User;
import lombok.Builder;
import lombok.Getter;

@Getter
public class GitHubIdDto {

private final String gitHubId;

@Builder
public GitHubIdDto(String gitHubId) {
this.gitHubId = gitHubId;
}

public GitHub toGitHubHasThisId() {
return GitHub.builder()
.githubId(this.gitHubId)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package bssm.db.bssmgit.domain.github.web.dto.request;

import bssm.db.bssmgit.domain.user.web.dto.UserProfile;
import bssm.db.bssmgit.domain.github.web.dto.GitHubIdDto;

import java.util.Arrays;
import java.util.Map;

public enum OauthAttributes {
GITHUB("github") {
@Override
public UserProfile of(Map<String, Object> attributes) {
return UserProfile.builder()
.gitId(String.valueOf(attributes.get("login")))
public GitHubIdDto of(Map<String, Object> attributes) {
return GitHubIdDto.builder()
.gitHubId(String.valueOf(attributes.get("login")))
.build();
}
};
Expand All @@ -21,13 +21,13 @@ public UserProfile of(Map<String, Object> attributes) {
this.providerName = name;
}

public static UserProfile extract(String providerName, Map<String, Object> attributes) {
public static GitHubIdDto extract(String providerName, Map<String, Object> attributes) {
return Arrays.stream(values())
.filter(provider -> providerName.equals(provider.providerName))
.findFirst()
.orElseThrow(IllegalArgumentException::new)
.of(attributes);
}

public abstract UserProfile of(Map<String, Object> attributes);
public abstract GitHubIdDto of(Map<String, Object> attributes);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bssm.db.bssmgit.domain.github.web.dto.response;

import bssm.db.bssmgit.domain.github.domain.GitHub;
import bssm.db.bssmgit.domain.user.domain.User;
import lombok.Getter;

Expand All @@ -8,8 +9,8 @@ public class GithubOauthResultResDto {

private boolean result;

public GithubOauthResultResDto(User user) {
if (user.getGithubId() != null) result = true;
public GithubOauthResultResDto(GitHub gitHub) {
if (gitHub.getGithubId() != null) result = true;
}

}
17 changes: 1 addition & 16 deletions src/main/java/bssm/db/bssmgit/domain/user/facade/UserFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ private User bsmTeacherSignup(BsmResourceResponse dto, String bsmToken) {
return user;
}

@Transactional
private User bsmUserUpdate(User user, BsmResourceResponse dto) {
BsmStudentResponse student = dto.getStudent();
user.updateName(student.getName());
Expand All @@ -79,6 +78,7 @@ private User bsmUserUpdate(User user, BsmResourceResponse dto) {
return userRepository.save(user);
}

@Transactional
public User getAndUpdateOrElseSignUp(BsmResourceResponse resource, String token) {
Optional<User> userOptional = findByEmail(resource.getEmail());

Expand All @@ -100,21 +100,6 @@ private Optional<User> findByEmail(String email) {
return userRepository.findByEmail(email);
}

public List<GithubResponseDto> findAllUserGitDesc() {
return userRepository.findGitAll().stream()
.filter(u -> u.getGithubId() != null)
.filter(u -> u.getCommits() != null)
.map(GithubResponseDto::new)
.collect(Collectors.toList());
}

public List<BojResponseDto> findAllUserBojDesc() {
return userRepository.findBojAll().stream()
.filter(u -> u.getBoj.getBojId() != null)
.map(BojResponseDto::new)
.collect(Collectors.toList());
}

public User findById(Long id) {
return userRepository.findById(id)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,4 @@ public User bsmOauth(String authCode) throws IOException {
public UserResponseDto getUser() {
return new UserResponseDto(userFacade.getCurrentUser());
}

public List<GithubResponseDto> findAllUserGitDesc() {
return userFacade.findAllUserGitDesc();
}

public List<BojResponseDto> findAllUserBojDesc() {
return userFacade.findAllUserBojDesc();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,6 @@ public UserResponseDto getUserInfo() {
return userService.getUser();
}

@GetMapping("/git")
public Result<List<GithubResponseDto>> findByGithubCommitDesc() {
List<GithubResponseDto> allUserBojDesc = userService.findAllUserGitDesc();
return new Result<>(allUserBojDesc.size(), allUserBojDesc);
}

@GetMapping("/boj")
public Result<List<BojResponseDto>> findByBojTierDesc() {
List<BojResponseDto> allUserBojDesc = userService.findAllUserBojDesc();
return new Result<>(allUserBojDesc.size(), allUserBojDesc);
}

@PostMapping("/test")
public void test() throws IOException {
githubService.updateUserGithubInfo();
Expand Down
Loading

0 comments on commit 379ead3

Please sign in to comment.