From b34f5b3301db63be8bd5bdb82dab1ba5bd617c3b Mon Sep 17 00:00:00 2001 From: Piotr Dec Date: Tue, 16 Jul 2024 21:57:52 +0200 Subject: [PATCH 1/2] feat: TemperaturesController --- readme.md | 5 +++++ .../ztsh/lfr/web/TemperaturesController.java | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/main/java/eu/ztsh/lfr/web/TemperaturesController.java diff --git a/readme.md b/readme.md index 0d417ee..14ca349 100644 --- a/readme.md +++ b/readme.md @@ -36,3 +36,8 @@ Write an application that, at the endpoint specified by you, returns the yearly } ] ``` + +## Usage + +Temperature statistics endpoint: `/api/temperatures/{city}` +Returns HTTP 200 when city is found, 404 otherwise. diff --git a/src/main/java/eu/ztsh/lfr/web/TemperaturesController.java b/src/main/java/eu/ztsh/lfr/web/TemperaturesController.java new file mode 100644 index 0000000..fa5cd6b --- /dev/null +++ b/src/main/java/eu/ztsh/lfr/web/TemperaturesController.java @@ -0,0 +1,20 @@ +package eu.ztsh.lfr.web; + +import eu.ztsh.lfr.model.Entry; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/api/temperatures") +public class TemperaturesController { + + @GetMapping("/{city}") + public List getTemperatures(@PathVariable String city) { + throw new UnsupportedOperationException("Not supported yet."); + } + +} From 2f161d34814da4d2e08c56bd3435b03ac104972a Mon Sep 17 00:00:00 2001 From: Piotr Dec Date: Tue, 16 Jul 2024 22:22:42 +0200 Subject: [PATCH 2/2] feat: TemperaturesService --- .../eu/ztsh/lfr/core/TemperaturesService.java | 13 +++++++++++++ .../core/impl/TemperaturesServiceImpl.java | 19 +++++++++++++++++++ .../ztsh/lfr/web/TemperaturesController.java | 16 ++++++++++++++-- 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/main/java/eu/ztsh/lfr/core/TemperaturesService.java create mode 100644 src/main/java/eu/ztsh/lfr/core/impl/TemperaturesServiceImpl.java diff --git a/src/main/java/eu/ztsh/lfr/core/TemperaturesService.java b/src/main/java/eu/ztsh/lfr/core/TemperaturesService.java new file mode 100644 index 0000000..1ee66e3 --- /dev/null +++ b/src/main/java/eu/ztsh/lfr/core/TemperaturesService.java @@ -0,0 +1,13 @@ +package eu.ztsh.lfr.core; + +import eu.ztsh.lfr.model.Entry; +import jakarta.annotation.Nonnull; + +import java.util.List; + +public interface TemperaturesService { + + @Nonnull + List getTemperaturesFor(String city); + +} diff --git a/src/main/java/eu/ztsh/lfr/core/impl/TemperaturesServiceImpl.java b/src/main/java/eu/ztsh/lfr/core/impl/TemperaturesServiceImpl.java new file mode 100644 index 0000000..79b2f27 --- /dev/null +++ b/src/main/java/eu/ztsh/lfr/core/impl/TemperaturesServiceImpl.java @@ -0,0 +1,19 @@ +package eu.ztsh.lfr.core.impl; + +import eu.ztsh.lfr.core.TemperaturesService; +import eu.ztsh.lfr.model.Entry; +import jakarta.annotation.Nonnull; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class TemperaturesServiceImpl implements TemperaturesService { + + @Nonnull + @Override + public List getTemperaturesFor(String city) { + throw new UnsupportedOperationException("Not supported yet."); + } + +} diff --git a/src/main/java/eu/ztsh/lfr/web/TemperaturesController.java b/src/main/java/eu/ztsh/lfr/web/TemperaturesController.java index fa5cd6b..52a2685 100644 --- a/src/main/java/eu/ztsh/lfr/web/TemperaturesController.java +++ b/src/main/java/eu/ztsh/lfr/web/TemperaturesController.java @@ -1,6 +1,9 @@ package eu.ztsh.lfr.web; +import eu.ztsh.lfr.core.TemperaturesService; import eu.ztsh.lfr.model.Entry; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -12,9 +15,18 @@ import java.util.List; @RequestMapping("/api/temperatures") public class TemperaturesController { + private final TemperaturesService temperaturesService; + + @Autowired + public TemperaturesController(TemperaturesService temperaturesService) { + this.temperaturesService = temperaturesService; + } + @GetMapping("/{city}") - public List getTemperatures(@PathVariable String city) { - throw new UnsupportedOperationException("Not supported yet."); + public ResponseEntity> getTemperatures(@PathVariable String city) { + var data = temperaturesService.getTemperaturesFor(city); + + return data.isEmpty() ? ResponseEntity.notFound().build() : ResponseEntity.ok(data); } }