Added a time parameter to the GET request #4

Merged
redphoenix9123 merged 1 commit from time-parameter into master 2022-08-04 00:50:55 +00:00
2 changed files with 24 additions and 10 deletions

View file

@ -10,7 +10,7 @@
</parent> </parent>
<groupId>com.synergyhelix</groupId> <groupId>com.synergyhelix</groupId>
<artifactId>countdown</artifactId> <artifactId>countdown</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.1-SNAPSHOT</version>
<name>countdown</name> <name>countdown</name>
<description>Countdown Timer API</description> <description>Countdown Timer API</description>
<properties> <properties>

View file

@ -1,9 +1,12 @@
package com.synergyhelix.countdown; package com.synergyhelix.countdown;
import org.apache.commons.lang3.time.DurationFormatUtils; import org.apache.commons.lang3.time.DurationFormatUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping; 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.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
@ -11,19 +14,28 @@ import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.time.*; import java.time.*;
import java.time.format.DateTimeParseException;
import java.util.Optional;
@RestController @RestController
public class CountdownController { public class CountdownController {
@GetMapping(value = "/", produces = MediaType.IMAGE_JPEG_VALUE) @GetMapping(value = "/", produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] countdown() throws IOException { public byte[] countdown(@RequestParam Optional<String> time) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 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(); return outputStream.toByteArray();
} }
private BufferedImage createImage() throws IOException { private BufferedImage createImage(ZonedDateTime time) throws IOException {
final BufferedImage image = ImageIO.read(getClass().getResourceAsStream("/chicago.jpg")); final BufferedImage image = ImageIO.read(getClass().getResourceAsStream("/chicago.jpg"));
Graphics g = image.getGraphics(); Graphics g = image.getGraphics();
@ -34,7 +46,7 @@ public class CountdownController {
g.setColor(Color.WHITE); g.setColor(Color.WHITE);
g.setFont(new Font("Courier", Font.BOLD, 57)); 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.setColor(new Color(0xE4002B));
g.setFont(g.getFont().deriveFont(46f)); g.setFont(g.getFont().deriveFont(46f));
@ -45,11 +57,13 @@ public class CountdownController {
return image; return image;
} }
private String calculateTimeUntilThing() { private String calculateTimeUntil(ZonedDateTime time) {
ZonedDateTime trip = ZonedDateTime.of(LocalDateTime.parse("2022-08-13T13:00:00"), ZoneId.of("UTC")); Duration duration = Duration.between(Instant.now(), time);
Duration duration = Duration.between(Instant.now(), trip);
if (duration.isNegative()) {
return "00D 00H 00M 00S";
} else {
return DurationFormatUtils.formatDuration(duration.toMillis(), "dd'D' HH'H' mm'M' ss'S'", true); return DurationFormatUtils.formatDuration(duration.toMillis(), "dd'D' HH'H' mm'M' ss'S'", true);
} }
}
} }