Created
January 17, 2018 19:41
-
-
Save rowanl/29a2a11554a9a4991db7031abf707053 to your computer and use it in GitHub Desktop.
playframework-kubernetes - Serving an HTML page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CharityController { | |
private final CharityRepository charityRepository; | |
private final HttpExecutionContext ec; | |
private final LocalDateTime endDateTime; | |
private final Clock clock; | |
@Inject | |
public CharityController(CharityRepository charityRepository, | |
HttpExecutionContext ec, | |
Clock clock, | |
Config config) { | |
this.charityRepository = charityRepository; | |
this.ec = ec; | |
this.formFactory = formFactory; | |
this.clock = clock; | |
long endTimeAsEpochMilli = config.getLong("charity.end-time"); | |
String zonedIdAsString = config.getString("time.zone-id"); | |
this.endDateTime = ofInstant(ofEpochMilli(endTimeAsEpochMilli), ZoneId.of(zonedIdAsString)); | |
} | |
@AddCSRFToken | |
public CompletionStage<Result> index() { | |
return charityRepository.findList().thenApplyAsync(items -> | |
timeSensitiveResult(() -> voteIndex.render(items)), ec.current()); | |
} | |
private Result timeSensitiveResult(Supplier<Html> pageToRender) { | |
if (isAfterEndDateTime()) { | |
return movedPermanently(routes.ClosedController.index()); | |
} else { | |
return ok(pageToRender.get()); | |
} | |
} | |
private boolean isAfterEndDateTime() { | |
return endDateTime.isBefore(now(clock)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment