From 2f0a86bfcc312e48de0deaaa4c9e299fa41d6e16 Mon Sep 17 00:00:00 2001 From: redphoenix9123 Date: Wed, 3 Aug 2022 19:48:33 -0500 Subject: [PATCH] Added a time parameter to the GET request --- pom.xml | 2 +- .../countdown/CountdownController.java | 32 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index f4d403e..b44aff1 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.synergyhelix countdown - 1.0.0-SNAPSHOT + 1.0.1-SNAPSHOT countdown Countdown Timer API diff --git a/src/main/java/com/synergyhelix/countdown/CountdownController.java b/src/main/java/com/synergyhelix/countdown/CountdownController.java index f3e4523..46207fb 100644 --- a/src/main/java/com/synergyhelix/countdown/CountdownController.java +++ b/src/main/java/com/synergyhelix/countdown/CountdownController.java @@ -1,9 +1,12 @@ package com.synergyhelix.countdown; import org.apache.commons.lang3.time.DurationFormatUtils; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; import javax.imageio.ImageIO; import java.awt.*; @@ -11,19 +14,28 @@ import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.time.*; +import java.time.format.DateTimeParseException; +import java.util.Optional; @RestController public class CountdownController { @GetMapping(value = "/", produces = MediaType.IMAGE_JPEG_VALUE) - public byte[] countdown() throws IOException { + public byte[] countdown(@RequestParam Optional time) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ImageIO.write(createImage(), "jpg", outputStream); + ZonedDateTime inputTime; + try { + inputTime = ZonedDateTime.of(LocalDateTime.parse(time.orElse(LocalDateTime.now().toString())), ZoneOffset.UTC); + } catch (DateTimeParseException e) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, String.format("Unable to parse the input time: %s\n %s", time, e)); + } + + ImageIO.write(createImage(inputTime), "jpg", outputStream); return outputStream.toByteArray(); } - private BufferedImage createImage() throws IOException { + private BufferedImage createImage(ZonedDateTime time) throws IOException { final BufferedImage image = ImageIO.read(getClass().getResourceAsStream("/chicago.jpg")); Graphics g = image.getGraphics(); @@ -34,7 +46,7 @@ public class CountdownController { g.setColor(Color.WHITE); g.setFont(new Font("Courier", Font.BOLD, 57)); - g.drawString(calculateTimeUntilThing(), 15, 60); + g.drawString(calculateTimeUntil(time), 15, 60); g.setColor(new Color(0xE4002B)); g.setFont(g.getFont().deriveFont(46f)); @@ -45,11 +57,13 @@ public class CountdownController { return image; } - private String calculateTimeUntilThing() { - ZonedDateTime trip = ZonedDateTime.of(LocalDateTime.parse("2022-08-13T13:00:00"), ZoneId.of("UTC")); + private String calculateTimeUntil(ZonedDateTime time) { + Duration duration = Duration.between(Instant.now(), time); - Duration duration = Duration.between(Instant.now(), trip); - - return DurationFormatUtils.formatDuration(duration.toMillis(), "dd'D' HH'H' mm'M' ss'S'", true); + if (duration.isNegative()) { + return "00D 00H 00M 00S"; + } else { + return DurationFormatUtils.formatDuration(duration.toMillis(), "dd'D' HH'H' mm'M' ss'S'", true); + } } }