Skip to content

Commit

Permalink
Merge pull request #8 from resume-reading-service/oauth
Browse files Browse the repository at this point in the history
feat : #4 user role, penalty 도메인 작성 및 user 조회 전략 일괄 수정
  • Loading branch information
sehajyang authored Nov 1, 2020
2 parents 31310ce + edc7f01 commit 9e50b8e
Show file tree
Hide file tree
Showing 31 changed files with 591 additions and 32 deletions.
5 changes: 3 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Thu Oct 15 19:57:46 KST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
28 changes: 28 additions & 0 deletions src/main/java/com/component/MessageComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.component;

import lombok.RequiredArgsConstructor;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
* Created by sehajyang
* DateTime : 2020/10/31
*/

@Component
@RequiredArgsConstructor
public class MessageComponent {
private final ResourceBundleMessageSource messageSource;

public String getMessage(String code) {
String message;
try {
message = messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
} catch (Exception e){
message = "UNDEFINED ERROR";
}
return StringUtils.isEmpty(message) ? "UNDEFINED ERROR" : message;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.resumers.server.oauth.controller;

import com.resumers.oauth.service.SocialService;
import com.resumers.oauth.service.UserService;
import com.resumers.server.oauth.service.SocialService;
import com.resumers.server.oauth.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.resumers.server.oauth.controller;

import com.resumers.server.oauth.model.dto.ApiResponse;
import com.resumers.server.oauth.model.dto.UserDto;
import com.resumers.server.oauth.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* Created by sehajyang
* DateTime : 2020/10/19
*/

@Slf4j
@Controller
@RestController
@RequiredArgsConstructor
@RequestMapping("/users")
public class UserController {
private final UserService userService;

@GetMapping
public ApiResponse<List<UserDto>> findAll(UserDto.SearchQuery userDto){
return new ApiResponse<>(userService.findAll());
}

@PostMapping
public void save(UserDto.InsertUser user) {
userService.create(user);
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/resumers/server/oauth/enums/ExceptionType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.resumers.server.oauth.enums;

import lombok.Getter;

/**
* Created by sehajyang
* DateTime : 2020/10/31
*/

@Getter
public enum ExceptionType {
/*
* BadRequest 400
* Unauthorized 401
* Forbidden 403
* Not Found 404
* Server 500
*/

USER_NOT_FOUND(400, 1000, "존재하지 않는 유저입니다");

private Integer httpStatusCode;
private Integer code;
private String value;

ExceptionType(Integer httpStatusCode, Integer code, String value) {
this.httpStatusCode = httpStatusCode;
this.code = code;
this.value = value;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/resumers/server/oauth/enums/RoleType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.resumers.server.oauth.enums;

/**
* Created by sehajyang
* DateTime : 2020/10/09
*/
public enum RoleType {
USER, ADMIN
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.resumers.server.oauth.exception;

import com.resumers.server.oauth.enums.ExceptionType;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
* Created by sehajyang
* DateTime : 2020/10/31
*/

@Getter
@NoArgsConstructor
public class CustomException extends RuntimeException{
private ExceptionType type;
private String message;

public CustomException(ExceptionType type) {
super(type.getValue());
this.type = type;
}

public CustomException(ExceptionType type, String message) {
super(type.getValue());
this.type = type;
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
package com.resumers.server.oauth.exception;

import com.component.MessageComponent;
import com.resumers.server.oauth.model.dto.ApiResponse;
import com.resumers.server.oauth.model.dto.EmptyObject;
import com.resumers.server.oauth.model.dto.ErrorResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

/**
* Created by sehajyang
* DateTime : 2020/10/03
*/
public class ExceptionHandler {

@Slf4j
@RestController
@RestControllerAdvice
@RequiredArgsConstructor
public class ExceptionHandler implements ErrorController {

private final ErrorAttributes errorAttributes;
private final MessageComponent message;

@Override
public String getErrorPath() {
return "/error";
}

private Map<String, Object> getErrorAttribute(WebRequest webRequest) {
return errorAttributes.getErrorAttributes(webRequest, true);
}

@org.springframework.web.bind.annotation.ExceptionHandler(value = CustomException.class)
public ResponseEntity<ApiResponse<EmptyObject>> customException(WebRequest webRequest, CustomException e) {
return ResponseEntity.status(e.getType().getHttpStatusCode()).body(new ApiResponse<>(new ErrorResponse(e.getType().getCode(), getErrorAttribute(webRequest), e.getMessage())));
}
}
71 changes: 71 additions & 0 deletions src/main/java/com/resumers/server/oauth/model/dto/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.resumers.server.oauth.model.dto;

import lombok.Data;
import org.slf4j.MDC;
/**
* Created by sehajyang
* DateTime : 2020/10/31
*/

@Data
public class ApiResponse<T> {
private MetaResponse meta;
private T data;
private ErrorResponse error;
private String traceId;

public ApiResponse() {
this.meta = new MetaResponse();
this.data = (T) EmptyObject.OBJECT;
this.error = new ErrorResponse();
setTraceId();
}

public ApiResponse(MetaResponse meta) {
this.meta = meta;
this.data = (T) EmptyObject.LIST;
this.error = new ErrorResponse();
setTraceId();
}

public ApiResponse(T data) {
if (data == null) {
this.data = (T) EmptyObject.OBJECT;
this.meta = new MetaResponse();
} else {
this.data = data;
this.meta = new MetaResponse(1L);
}
this.error = new ErrorResponse();
setTraceId();
}

public ApiResponse(ErrorResponse error) {
this.meta = new MetaResponse();
this.data = (T) EmptyObject.OBJECT;
this.error = error;
setTraceId();
}

public ApiResponse(Long totalCount, T data) {
this.meta = new MetaResponse(totalCount);

if (data == null) {
this.data = (T) EmptyObject.LIST;
} else {
this.data = data;
}

this.error = new ErrorResponse();
setTraceId();
}

private void setTraceId() {
try {
this.traceId = MDC.get("TRACE_ID");
} catch (NullPointerException e) {
this.traceId = "";
}
}
}

16 changes: 16 additions & 0 deletions src/main/java/com/resumers/server/oauth/model/dto/EmptyObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.resumers.server.oauth.model.dto;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.util.ArrayList;
import java.util.List;
/**
* Created by sehajyang
* DateTime : 2020/10/31
*/

@JsonSerialize
public class EmptyObject {
public static EmptyObject OBJECT = new EmptyObject();
public static List LIST = new ArrayList();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.resumers.server.oauth.model.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

/**
* Created by sehajyang
* DateTime : 2020/10/31
*/

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ErrorResponse {
private Integer status;
private String error;
private String message;
private LocalDateTime timeStamp;

public ErrorResponse(int status, Map<String, Object> errorAttribute, String message) {
this.status = status;
this.message = message;
this.error = (String) errorAttribute.get("error");
this.timeStamp = new Timestamp(((Date) errorAttribute.get("timestamp")).getTime()).toLocalDateTime();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.resumers.server.oauth.model.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Created by sehajyang
* DateTime : 2020/10/31
*/

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MetaResponse {
private Long total;
}
30 changes: 30 additions & 0 deletions src/main/java/com/resumers/server/oauth/model/dto/UserDto.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
package com.resumers.server.oauth.model.dto;

import com.resumers.server.oauth.model.entity.User;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
* Created by sehajyang
* DateTime : 2020/10/03
*/

@Getter
@NoArgsConstructor
public class UserDto {
private Long userId;
private String email;
private String nickname;
private String description;

public UserDto(User user) {
this.userId = user.getUserId();
this.email = user.getEmail();
this.nickname = user.getNickname();
this.description = user.getDescription();
}

@Getter
public class SearchQuery extends UserDto{
public SearchQuery(){
super();
}
}

@Getter
@NoArgsConstructor
public class InsertUser extends UserDto {
}
}
Loading

0 comments on commit 9e50b8e

Please sign in to comment.