Skip to content

Commit

Permalink
[#58] feat: LocalDateTime > OffsetDateTime 변환
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyoungchoi95 committed Oct 20, 2021
1 parent a235f1e commit 7bf8b80
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.study.realworld.user.domain.Image;
import com.study.realworld.user.domain.Profile;
import com.study.realworld.user.domain.Username;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.List;

@JsonTypeName(value = "article")
Expand All @@ -37,12 +37,12 @@ public class ArticleResponse {
private List<Tag> tags;

@JsonProperty("createdAt")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS")
private LocalDateTime createdAt;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", timezone = "UTC")
private OffsetDateTime createdAt;

@JsonProperty("updatedAt")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS")
private LocalDateTime updatedAt;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", timezone = "UTC")
private OffsetDateTime updatedAt;

@JsonProperty("author")
private AuthorProfile authorProfile;
Expand All @@ -51,7 +51,7 @@ public class ArticleResponse {
}

public ArticleResponse(Slug slug, Title title, Description description, Body body,
List<Tag> tags, LocalDateTime createdAt, LocalDateTime updatedAt,
List<Tag> tags, OffsetDateTime createdAt, OffsetDateTime updatedAt,
AuthorProfile authorProfile) {
this.slug = slug;
this.title = title;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.study.realworld.global.config;

import java.time.OffsetDateTime;
import java.util.Optional;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.auditing.DateTimeProvider;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@Configuration
@EnableJpaAuditing
@EnableJpaAuditing(dateTimeProviderRef = "auditingDateTimeProvider")
public class JpaConfiguration {

@Bean(name = "auditingDateTimeProvider")
public DateTimeProvider dateTimeProvider() {
return () -> Optional.of(OffsetDateTime.now());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.study.realworld.global.domain;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
Expand All @@ -14,28 +14,28 @@ public abstract class BaseTimeEntity {

@CreatedDate
@Column(name = "created_at", updatable = false, nullable = false)
private LocalDateTime createdAt;
private OffsetDateTime createdAt;

@LastModifiedDate
@Column(name = "updated_at")
private LocalDateTime updatedAt;
private OffsetDateTime updatedAt;

@Column(name = "deleted_at")
private LocalDateTime deletedAt;
private OffsetDateTime deletedAt;

public LocalDateTime createdAt() {
public OffsetDateTime createdAt() {
return createdAt;
}

public LocalDateTime updatedAt() {
public OffsetDateTime updatedAt() {
return updatedAt;
}

public LocalDateTime deletedAt() {
public OffsetDateTime deletedAt() {
return deletedAt;
}

public void saveDeletedTime(LocalDateTime deletedAt) {
public void saveDeletedTime(OffsetDateTime deletedAt) {
this.deletedAt = deletedAt;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ spring:
format_sql: true
use_sql_comments: true
hibernate:
ddl-auto: none
ddl-auto: create
open-in-view: false
datasource:
platform: h2
Expand Down
18 changes: 9 additions & 9 deletions src/main/resources/schema-h2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ CREATE TABLE IF NOT EXISTS user
password varchar(255) NOT NULL,
bio varchar DEFAULT NULL,
image varchar DEFAULT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(),
updated_at DATETIME DEFAULT NULL,
deleted_at DATETIME DEFAULT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
updated_at TIMESTAMP DEFAULT NULL,
deleted_at TIMESTAMP DEFAULT NULL,
PRIMARY KEY (id),
CONSTRAINT unique_email UNIQUE (email),
CONSTRAINT unique_username UNIQUE (username)
Expand All @@ -33,9 +33,9 @@ CREATE TABLE IF NOT EXISTS article
title varchar(50) NOT NULL,
description varchar(255) NOT NULL,
body text NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(),
updated_at DATETIME DEFAULT NULL,
deleted_at DATETIME DEFAULT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
updated_at TIMESTAMP DEFAULT NULL,
deleted_at TIMESTAMP DEFAULT NULL,
PRIMARY KEY (id),
CONSTRAINT fk_article_to_user_id FOREIGN KEY (user_id) REFERENCES user (id),
CONSTRAINT unique_user_id_slug UNIQUE (user_id, slug)
Expand All @@ -47,9 +47,9 @@ CREATE TABLE IF NOT EXISTS comment
user_id bigint NOT NULL,
article_id bigint NOT NULL,
body text NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(),
updated_at DATETIME DEFAULT NULL,
deleted_at DATETIME DEFAULT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
updated_at TIMESTAMP DEFAULT NULL,
deleted_at TIMESTAMP DEFAULT NULL,
PRIMARY KEY (id),
CONSTRAINT fk_comment_to_user_id FOREIGN KEY (user_id) REFERENCES user (id),
CONSTRAINT fk_comment_to_article_id FOREIGN KEY (article_id) REFERENCES article (id) ON DELETE CASCADE ON UPDATE CASCADE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static com.study.realworld.user.controller.ApiDocumentUtils.getDocumentRequest;
import static com.study.realworld.user.controller.ApiDocumentUtils.getDocumentResponse;
import static java.time.ZoneOffset.UTC;
import static java.time.format.DateTimeFormatter.ofPattern;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -29,8 +31,7 @@
import com.study.realworld.user.domain.Password;
import com.study.realworld.user.domain.User;
import com.study.realworld.user.domain.Username;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.OffsetDateTime;
import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -85,7 +86,7 @@ void createArticleTestTest() throws Exception {
.build();
Article article = Article.from(articleContent, author);

LocalDateTime now = LocalDateTime.now();
OffsetDateTime now = OffsetDateTime.now();
ReflectionTestUtils.setField(article, "createdAt", now);
ReflectionTestUtils.setField(article, "updatedAt", now);

Expand Down Expand Up @@ -120,9 +121,9 @@ void createArticleTestTest() throws Exception {
.andExpect(jsonPath("$.article.tagList[0]", is(article.tags().get(0).name())))
.andExpect(jsonPath("$.article.tagList[1]", is(article.tags().get(1).name())))
.andExpect(jsonPath("$.article.createdAt",
is(article.updatedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")))))
is(article.updatedAt().format(ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(UTC)))))
.andExpect(jsonPath("$.article.updatedAt",
is(article.updatedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")))))
is(article.updatedAt().format(ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(UTC)))))
.andExpect(jsonPath("$.article.author.username", is(article.author().username().value())))
.andExpect(jsonPath("$.article.author.bio", is(nullValue())))
.andExpect(jsonPath("$.article.author.image", is(nullValue())))
Expand Down

0 comments on commit 7bf8b80

Please sign in to comment.