From e053873b762659248065c7cc1946106157611e9a Mon Sep 17 00:00:00 2001 From: Rickard Blomkvist Date: Wed, 28 Aug 2019 11:57:30 +0200 Subject: [PATCH] Expose createdAt as ZonedDateTime in order for the ui to be able to show time in user's timezone. --- api/src/main/java/api/Post.java | 15 +++++++++++++++ spec/src/test/java/impl/AnswerResourceTest.java | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/api/src/main/java/api/Post.java b/api/src/main/java/api/Post.java index 3b49bad..779f2af 100644 --- a/api/src/main/java/api/Post.java +++ b/api/src/main/java/api/Post.java @@ -1,6 +1,13 @@ package api; + import com.fasterxml.jackson.annotation.JsonIgnore; + import com.fasterxml.jackson.annotation.JsonProperty; + import java.time.LocalDateTime; + import java.time.ZonedDateTime; + + import static java.time.ZoneId.systemDefault; + import static java.util.Optional.ofNullable; /** * A class that defines the shared attributes between a {@link Answer} @@ -31,6 +38,14 @@ public void setCreatedBy(String createdBy) { this.createdBy = createdBy; } + @JsonProperty("createdAt") + public ZonedDateTime getCreated() { + return ofNullable(createdAt) + .map(time -> time.atZone(systemDefault())) + .orElse(null); + } + + @JsonIgnore public LocalDateTime getCreatedAt() { return createdAt; } diff --git a/spec/src/test/java/impl/AnswerResourceTest.java b/spec/src/test/java/impl/AnswerResourceTest.java index dc2e28b..c64c135 100644 --- a/spec/src/test/java/impl/AnswerResourceTest.java +++ b/spec/src/test/java/impl/AnswerResourceTest.java @@ -25,6 +25,7 @@ import se.fortnox.reactivewizard.jaxrs.WebException; import slack.SlackResource; +import java.time.ZoneId; import java.util.ArrayList; import java.util.List; import java.util.function.BiFunction; @@ -302,6 +303,10 @@ public void shouldAddAnswerToQuestion() { assertEquals("this is the body of the answer", createdAnswer.getAnswer()); assertThat(createdAnswer.getVotes()).isZero(); assertEquals("Test Subject", createdAnswer.getCreatedBy()); + assertThat(createdAnswer.getCreated()) + .isNotNull(); + assertThat(createdAnswer.getCreated().getZone()) + .isEqualTo(ZoneId.systemDefault()); } @Test @@ -528,6 +533,8 @@ private static Answer createAnswer() { private static Answer createAnswer( String answerBody) { Answer answer = new Answer(); answer.setAnswer(answerBody); + assertThat(answer.getCreated()) + .isNull(); return answer; }