Merge branch 'time-parameter' into 'master'

Added a time parameter to the GET request

See merge request c4181/countdown-timer!4
This commit is contained in:
Christopher Knapczyk 2022-08-04 00:50:55 +00:00
commit 046cc716dd
2 changed files with 24 additions and 10 deletions

View file

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

View file

@ -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<String> 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"));
Duration duration = Duration.between(Instant.now(), trip);
private String calculateTimeUntil(ZonedDateTime time) {
Duration duration = Duration.between(Instant.now(), time);
if (duration.isNegative()) {
return "00D 00H 00M 00S";
} else {
return DurationFormatUtils.formatDuration(duration.toMillis(), "dd'D' HH'H' mm'M' ss'S'", true);
}
}
}