Skip to content
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

Feat [#3] 기본 클래스들 세팅 #4

Merged
merged 12 commits into from
Jun 23, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.sopt.jaksim.global.common;

import org.sopt.jaksim.global.message.ErrorMessage;
import org.sopt.jaksim.global.message.SuccessMessage;
import org.springframework.http.ResponseEntity;

public interface ApiResponseUtil {
static ResponseEntity<BaseResponse<?>> success(SuccessMessage successMessage) {
return ResponseEntity.status(successMessage.getHttpStatus())
.body(BaseResponse.of(successMessage));
}

static <T> ResponseEntity<BaseResponse<?>> success(SuccessMessage successMessage, T data) {
return ResponseEntity.status(successMessage.getHttpStatus())
.body(BaseResponse.of(successMessage, data));
}

static ResponseEntity<BaseResponse<?>> failure(ErrorMessage errorMessage) {
return ResponseEntity.status(errorMessage.getHttpStatus())
.body(BaseResponse.of(errorMessage));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.sopt.jaksim.global.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.sopt.jaksim.global.message.ErrorMessage;
import org.sopt.jaksim.global.message.SuccessMessage;

@Builder(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class BaseResponse<T> {
private final int status;
private final String code;
private final String message;
@JsonInclude(value = JsonInclude.Include.NON_NULL)
private final T data;

public static BaseResponse<?> of(SuccessMessage successMessage) {
return builder()
.status(successMessage.getHttpStatus().value())
.code(successMessage.getCode())
.message(successMessage.getMessage())
.build();
}

public static <T> BaseResponse<?> of(SuccessMessage successMessage, T data) {
return builder()
.status(successMessage.getHttpStatus().value())
.code(successMessage.getCode())
.message(successMessage.getMessage())
.data(data)
.build();
}

public static BaseResponse<?> of(ErrorMessage errorMessage) {
return builder()
.status(errorMessage.getHttpStatus().value())
.code(errorMessage.getCode())
.message(errorMessage.getMessage())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.sopt.jaksim.global.common;

import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTimeEntity {

@CreatedDate
private LocalDateTime createdAt;

@LastModifiedDate
private LocalDateTime updatedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.sopt.jaksim.global.common;

import lombok.extern.slf4j.Slf4j;
import org.sopt.jaksim.global.exception.ForbiddenException;
import org.sopt.jaksim.global.exception.NotFoundException;
import org.sopt.jaksim.global.exception.UnauthorizedException;
import org.sopt.jaksim.global.message.ErrorMessage;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<BaseResponse<?>> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
log.error(">>> handle: MethodArgumentNotValidException ", e);
return ApiResponseUtil.failure(ErrorMessage.BAD_REQUEST);
}

@ExceptionHandler(NotFoundException.class)
protected ResponseEntity<BaseResponse<?>> handleNotFoundException(NotFoundException e) {
log.error(">>> handle: NotFoundException ", e);
return ApiResponseUtil.failure(ErrorMessage.NOT_FOUND);
}

@ExceptionHandler(ForbiddenException.class)
protected ResponseEntity<BaseResponse<?>> handleForbiddenException(ForbiddenException e) {
log.error(">>> handle: ForbiddenException ", e);
return ApiResponseUtil.failure(ErrorMessage.FORBIDDEN);
}

@ExceptionHandler(UnauthorizedException.class)
protected ResponseEntity<BaseResponse<?>> handlerUnauthorizedException(UnauthorizedException e) {
log.error(">>> handle: UnauthorizedException ", e);
return ApiResponseUtil.failure(ErrorMessage.UNAUTHORIZED);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package org.sopt.jaksim;
package org.sopt.jaksim.global.common;

import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
@RequiredArgsConstructor
public class ProfileController {

public class HealthCheckController {
private final Environment env;
private static final String NULL = "";

Expand All @@ -19,6 +17,6 @@ public String getProfile() {
return Arrays.stream(env.getActiveProfiles())
.findFirst()
.orElse(NULL);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.jaksim.global.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@Configuration
@EnableJpaAuditing
public class JpaAuditingConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.sopt.jaksim.global.exception;

import lombok.Getter;
import org.sopt.jaksim.global.message.ErrorMessage;

@Getter
public class BusinessException extends RuntimeException {
public ErrorMessage errorMessage;

public BusinessException(ErrorMessage errorMessage) {
super(errorMessage.getMessage());
this.errorMessage = errorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.jaksim.global.exception;

import org.sopt.jaksim.global.message.ErrorMessage;

public class ForbiddenException extends BusinessException {
public ForbiddenException(ErrorMessage errorMessage) {
super(errorMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.sopt.jaksim.global.exception;

import org.sopt.jaksim.global.message.ErrorMessage;

public class InvalidValueException extends BusinessException {
public InvalidValueException() {
super(ErrorMessage.BAD_REQUEST);
}

public InvalidValueException(ErrorMessage errorMessage) {
super(errorMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.jaksim.global.exception;

import org.sopt.jaksim.global.message.ErrorMessage;

public class NotFoundException extends BusinessException {
public NotFoundException(ErrorMessage errorMessage) {
super(errorMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.jaksim.global.exception;

import org.sopt.jaksim.global.message.ErrorMessage;

public class UnauthorizedException extends BusinessException {
public UnauthorizedException(ErrorMessage errorMessage) {
super(errorMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.sopt.jaksim.global.message;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public enum ErrorMessage {
/**
* 400 Bad Request
*/
BAD_REQUEST(HttpStatus.BAD_REQUEST, "e4000", "잘못된 요청입니다."),
INVALID_PLATFORM_TYPE(HttpStatus.BAD_REQUEST, "e4001", "유효하지 않은 플랫폼 타입입니다."),

/**
* 401 Unauthorized
*/
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "e4010", "리소스 접근 권한이 없습니다."),
JWT_UNAUTHORIZED_EXCEPTION(HttpStatus.UNAUTHORIZED, "e4011", "사용자의 로그인 검증을 실패했습니다."),
PASSWORD_NOT_MATCHED_EXCEPTION(HttpStatus.UNAUTHORIZED, "e4012", "해당 유저의 비밀번호가 일치하지 않습니다."),

/**
* 403 Forbidden
*/
FORBIDDEN(HttpStatus.FORBIDDEN, "e4030", "리소스 접근 권한이 없습니다."),

/**
* 404 Not Found
*/
NOT_FOUND(HttpStatus.NOT_FOUND, "e4040", "대상을 찾을 수 없습니다."),

/**
* 405 Method Not Allowed
*/

/**
* 409 Conflict
*/

/**
* 500 Internal Server Error
*/
;

private final HttpStatus httpStatus;
private final String code;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.sopt.jaksim.global.message;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public enum SuccessMessage {
/**
* 200 Ok
*/
USER_SIGN_UP_SUCCESS(HttpStatus.OK, "s2000", "회원가입이 완료되었습니다."),
USER_SIGN_IN_SUCCESS(HttpStatus.OK, "s2000", "로그인이 완료되었습니다."),
;

/**
* 201 Created
*/

private final HttpStatus httpStatus;
private final String code;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.sopt.jaksim.user.api;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@RequiredArgsConstructor
@RequestMapping("/api/v1/users")
@Controller
public class UserApiController {

}
22 changes: 22 additions & 0 deletions jaksim/src/main/java/org/sopt/jaksim/user/domain/Platform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.sopt.jaksim.user.domain;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.sopt.jaksim.global.exception.InvalidValueException;
import org.sopt.jaksim.global.message.ErrorMessage;

import java.util.Arrays;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum Platform {
GOOGLE("Google");

private final String stringPlatform;

public static Platform getEnumPlatformFromStringPlatform(String stringPlatform) {
return Arrays.stream(values())
.filter(platform -> platform.stringPlatform.equals(stringPlatform))
.findFirst()
.orElseThrow(() -> new InvalidValueException(ErrorMessage.INVALID_PLATFORM_TYPE));
}
}
26 changes: 26 additions & 0 deletions jaksim/src/main/java/org/sopt/jaksim/user/domain/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.sopt.jaksim.user.domain;

import jakarta.persistence.*;
import lombok.*;
import org.sopt.jaksim.global.common.BaseTimeEntity;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder(access = AccessLevel.PRIVATE)
@Getter
@Table(name = "users")
@Entity
public class User extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Long id;
@Column(nullable = false)
private String email;
@Column(nullable = false)
private String name;
@Enumerated(EnumType.STRING)
private Platform platform;
private String refreshToken;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.sopt.jaksim.user.dto.request;

public record UserSignInRequest() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.sopt.jaksim.user.dto.response;

public record UserSignInResponse() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.sopt.jaksim.user.facade;

public class UserFacade {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.sopt.jaksim.user.repository;

import org.sopt.jaksim.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.sopt.jaksim.user.service;

public class UserService {
}
Loading