Skip to content

Commit

Permalink
refactor: 코딩 컨밴션을 맞추며 전체적으로 리팩터링
Browse files Browse the repository at this point in the history
  • Loading branch information
jujubebat committed Jul 21, 2021
1 parent 0148ad5 commit eba7986
Show file tree
Hide file tree
Showing 51 changed files with 219 additions and 223 deletions.
4 changes: 2 additions & 2 deletions backend/src/main/java/com/darass/darass/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*");
.allowedOrigins("*")
.allowedMethods("*");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import com.darass.darass.auth.oauth.controller.AuthenticationPrincipalArgumentResolver;
import com.darass.darass.auth.oauth.controller.RequiredLoginArgumentResolver;
import com.darass.darass.auth.oauth.service.OAuthService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@RequiredArgsConstructor
public class AuthenticationPrincipalConfig implements WebMvcConfigurer {

private final OAuthService oAuthService;

@Override
public void addArgumentResolvers(List argumentResolvers) {
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(createAuthenticationPrincipalArgumentResolver());
argumentResolvers.add(createRequiredLoginArgumentResolver());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.darass.darass.user.domain.SocialLoginUser;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Objects;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand All @@ -32,11 +33,11 @@ public SocialLoginUser findSocialLoginUser(String accessToken) {
try {
SocialLoginResponse socialLoginResponse
= restTemplate.postForObject(KAKAO_API_SERVER_URI, apiRequest, SocialLoginResponse.class);
return parseUser(socialLoginResponse);
return parseUser(Objects.requireNonNull(socialLoginResponse));

} catch (HttpClientErrorException e) {
throw ExceptionWithMessageAndCode.INVALID_JWT_TOKEN.getException();
}
} // TODO: null 포인터 예외 잡아야한다.
}

private HttpEntity<HttpHeaders> prepareRequest(String accessToken) {
Expand All @@ -53,7 +54,7 @@ private SocialLoginUser parseUser(SocialLoginResponse socialLoginResponse) {
String email = kaKaoAccount.getEmail();
Profile profile = socialLoginResponse.getKaKaoAccount().getProfile();
String nickname = profile.getNickname();
String profileImageUrl = profile.getThumbnail_image_url();
String profileImageUrl = profile.getThumbnailImageUrl();

return SocialLoginUser
.builder()
Expand All @@ -63,6 +64,5 @@ private SocialLoginUser parseUser(SocialLoginResponse socialLoginResponse) {
.email(email)
.profileImageUrl(profileImageUrl)
.build();

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.darass.darass.auth.oauth.api.domain.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -11,5 +12,7 @@ public class Profile {

private String nickname;

private String thumbnail_image_url;
@JsonProperty("thumbnail_image_url")
private String thumbnailImageUrl;

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public boolean supportsParameter(MethodParameter parameter) {
@Override
public User resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
String accessToken = AuthorizationExtractor.extract(Objects.requireNonNull(webRequest.getNativeRequest(HttpServletRequest.class)));
String accessToken = AuthorizationExtractor
.extract(Objects.requireNonNull(webRequest.getNativeRequest(HttpServletRequest.class)));

if (Objects.isNull(accessToken) || accessToken.isEmpty()) {
return new GuestUser();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.darass.darass.auth.oauth.controller;

import com.darass.darass.auth.oauth.controller.dto.TokenResponse;
import com.darass.darass.auth.oauth.dto.TokenResponse;
import com.darass.darass.auth.oauth.service.OAuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
import com.darass.darass.auth.oauth.service.OAuthService;
import com.darass.darass.exception.ExceptionWithMessageAndCode;
import com.darass.darass.user.domain.User;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@RequiredArgsConstructor
public class RequiredLoginArgumentResolver implements HandlerMethodArgumentResolver {

Expand All @@ -27,9 +26,9 @@ public boolean supportsParameter(MethodParameter parameter) {

@Override
public User resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
String accessToken = AuthorizationExtractor.extract(Objects.requireNonNull(webRequest.getNativeRequest(
HttpServletRequest.class)));
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
String accessToken = AuthorizationExtractor
.extract(Objects.requireNonNull(webRequest.getNativeRequest(HttpServletRequest.class)));

if (Objects.isNull(accessToken)) {
throw ExceptionWithMessageAndCode.SHOULD_LOGIN.getException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.darass.darass.auth.oauth.controller.dto;
package com.darass.darass.auth.oauth.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.darass.darass.auth.oauth.infrastructure;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;

public class AuthorizationExtractor {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.darass.darass.auth.oauth.infrastructure;

import com.darass.darass.exception.ExceptionWithMessageAndCode;
import io.jsonwebtoken.*;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class JwtTokenProvider {

@Value("${security.jwt.token.secret-key}")
private String secretKey;

@Value("${security.jwt.token.expire-length}")
private long validityInMilliseconds;

Expand All @@ -21,11 +25,11 @@ public String createAccessToken(String payload) {
Date validity = new Date(now.getTime() + validityInMilliseconds);

return Jwts.builder()
.setClaims(claims)
.setIssuedAt(now)
.setExpiration(validity)
.signWith(SignatureAlgorithm.HS256, secretKey)
.compact();
.setClaims(claims)
.setIssuedAt(now)
.setExpiration(validity)
.signWith(SignatureAlgorithm.HS256, secretKey)
.compact();
}

public String getPayload(String accessToken) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package com.darass.darass.auth.oauth.service;

import com.darass.darass.auth.oauth.api.domain.UserInfoProvider;
import com.darass.darass.auth.oauth.controller.dto.TokenResponse;
import com.darass.darass.auth.oauth.dto.TokenResponse;
import com.darass.darass.auth.oauth.infrastructure.JwtTokenProvider;
import com.darass.darass.exception.ExceptionWithMessageAndCode;
import com.darass.darass.user.domain.SocialLoginUser;
import com.darass.darass.user.repository.SocialLoginUserRepository;
import java.util.Optional;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@AllArgsConstructor
@Transactional
public class OAuthService {

private SocialLoginUserRepository socialLoginUserRepository;

private JwtTokenProvider jwtTokenProvider;
private UserInfoProvider userInfoProvider;

private UserInfoProvider userInfoProvider; //TODO: Mocking시 final 못붙이는 문제가 있다.

public TokenResponse oauthLogin(String oauthAccessToken) {
SocialLoginUser socialLoginUser = userInfoProvider.findSocialLoginUser(oauthAccessToken);
Optional<SocialLoginUser> foundSocialLoginUser = socialLoginUserRepository.findByOauthId(socialLoginUser.getOauthId());
Optional<SocialLoginUser> foundSocialLoginUser = socialLoginUserRepository
.findByOauthId(socialLoginUser.getOauthId());

if (foundSocialLoginUser.isEmpty()) {
if (foundSocialLoginUser.isEmpty()) { //TODO: 옵셔널로 변경 가능?
socialLoginUserRepository.save(socialLoginUser);
return TokenResponse.of(jwtTokenProvider.createAccessToken(socialLoginUser.getId().toString()));
}
Expand All @@ -37,7 +39,7 @@ public SocialLoginUser findSocialLoginUserByAccessToken(String accessToken) {
String userId = jwtTokenProvider.getPayload(accessToken);

return socialLoginUserRepository.findById(Long.parseLong(userId))
.orElseThrow(ExceptionWithMessageAndCode.INVALID_JWT_NOT_FOUND_USER_TOKEN::getException);
.orElseThrow(ExceptionWithMessageAndCode.INVALID_JWT_NOT_FOUND_USER_TOKEN::getException);
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.darass.darass.comment.controller;

import com.darass.darass.auth.oauth.domain.AuthenticationPrincipal;
import com.darass.darass.comment.controller.dto.CommentCreateRequest;
import com.darass.darass.comment.controller.dto.CommentDeleteRequest;
import com.darass.darass.comment.controller.dto.CommentResponse;
import com.darass.darass.comment.controller.dto.CommentUpdateRequest;
import com.darass.darass.comment.dto.CommentCreateRequest;
import com.darass.darass.comment.dto.CommentDeleteRequest;
import com.darass.darass.comment.dto.CommentResponse;
import com.darass.darass.comment.dto.CommentUpdateRequest;
import com.darass.darass.comment.service.CommentService;
import com.darass.darass.user.domain.User;
import java.util.List;
Expand All @@ -23,21 +23,23 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/comments")
@RequiredArgsConstructor
@RequestMapping("/api/v1/comments")
@RestController
public class CommentController {

private final CommentService commentService;

@GetMapping
public ResponseEntity<List<CommentResponse>> read(@RequestParam("url") String url, @RequestParam("projectKey") String projectKey) {
public ResponseEntity<List<CommentResponse>> read(@RequestParam("url") String url,
@RequestParam("projectKey") String projectKey) {
List<CommentResponse> commentResponses = commentService.findAllCommentsByUrlAndProjectKey(url, projectKey);
return ResponseEntity.status(HttpStatus.OK).body(commentResponses);
}

@PostMapping
public ResponseEntity<CommentResponse> save(@AuthenticationPrincipal User user, @Valid @RequestBody CommentCreateRequest commentRequest) {
public ResponseEntity<CommentResponse> save(@AuthenticationPrincipal User user,
@Valid @RequestBody CommentCreateRequest commentRequest) {
CommentResponse commentResponse = commentService.save(user, commentRequest);
return ResponseEntity.status(HttpStatus.CREATED).body(commentResponse);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor
@Getter
@NoArgsConstructor
@Entity
public class Comment extends BaseTimeEntity {

@Id
Expand All @@ -32,6 +32,7 @@ public class Comment extends BaseTimeEntity {
private Project project;

private String url;

private String content;

@Builder
Expand All @@ -55,7 +56,7 @@ public boolean match(String url, String projectKey) {
return this.url.equals(url) && project.isSame(projectKey);
}

public Long getUserId(){
public Long getUserId() {
return user.getId();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

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

@RequiredArgsConstructor
public class Comments {

private final List<Comment> comments;

public Comments(List<Comment> comments) {
this.comments = comments;
}

public List<Comment> match(String url, String projectKey) {
return comments.stream()
.filter(it -> it.match(url, projectKey))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.darass.darass.comment.controller.dto;
package com.darass.darass.comment.dto;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -14,10 +15,12 @@ public class CommentCreateRequest {

private String guestPassword;

@NotBlank
private String projectSecretKey;

@NotNull
private String content;

@NotBlank
private String url;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.darass.darass.comment.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class CommentDeleteRequest {

private final Long guestUserId;

private final String guestUserPassword;
}
Loading

0 comments on commit eba7986

Please sign in to comment.