Skip to content

Commit

Permalink
[feat] BaseTimeEntity, 성공 응답 객체 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
hysong4u committed Aug 24, 2024
1 parent 7bcba4b commit 6b6f021
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/relife/huranit/global/common/BaseTimeEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package relife.huranit.global.common;

import jakarta.persistence.Column;
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
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public abstract class BaseTimeEntity {
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package relife.huranit.global.common;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HealthCheckApiController {
@RequestMapping("/")
public String HuranitServer() {
return "Hello Huranit Server!";
}


@RequestMapping("/example")
public ResponseEntity<SuccessResponse<?>> example() {
return SuccessResponse.ok("example");
}
}
23 changes: 23 additions & 0 deletions src/main/java/relife/huranit/global/common/SuccessCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package relife.huranit.global.common;

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

@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum SuccessCode {
/**
* 200 Ok
*/
OK(HttpStatus.OK, "요청이 성공했습니다."),

/**
* 201 Created
*/
CREATED(HttpStatus.CREATED, "요청이 성공했습니다.");

private final HttpStatus httpStatus;
private final String message;
}
37 changes: 37 additions & 0 deletions src/main/java/relife/huranit/global/common/SuccessResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package relife.huranit.global.common;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@Getter
public class SuccessResponse<T> {
private int status;
private String message;
private T data;

public static <T> ResponseEntity<SuccessResponse<?>> ok(T data) {
return ResponseEntity.status(HttpStatus.OK)
.body(SuccessResponse.of(SuccessCode.OK, data));
}

public static <T> ResponseEntity<SuccessResponse<?>> created(T data) {
return ResponseEntity.status(HttpStatus.CREATED)
.body(SuccessResponse.of(SuccessCode.CREATED, data));
}


public static <T> SuccessResponse<?> of(SuccessCode successCode, T data) {
return SuccessResponse.builder()
.status(successCode.getHttpStatus().value())
.message(successCode.getMessage())
.data(data)
.build();
}

}

0 comments on commit 6b6f021

Please sign in to comment.