Skip to content

Commit

Permalink
Merge pull request #65 from FortnoxAB/serialize-zone-offset
Browse files Browse the repository at this point in the history
Expose createdAt as ZonedDateTime in order for the ui to be able to s…
  • Loading branch information
kindanice authored Sep 2, 2019
2 parents 293e770 + e053873 commit da99346
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions api/src/main/java/api/Post.java
Original file line number Diff line number Diff line change
@@ -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}
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 7 additions & 0 deletions spec/src/test/java/impl/AnswerResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit da99346

Please sign in to comment.