From 938094901df2842f1761458ebda96fc0f03645f3 Mon Sep 17 00:00:00 2001 From: Peter Siegmund Date: Tue, 27 Aug 2024 22:26:43 +0200 Subject: [PATCH] handling for nullable abbreviation Signed-off-by: Peter Siegmund --- .../timezone/web/controllers/TimeZoneController.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/dev/mars3142/fhq/timezone_service/timezone/web/controllers/TimeZoneController.java b/src/main/java/dev/mars3142/fhq/timezone_service/timezone/web/controllers/TimeZoneController.java index 9ff2fa8..21a6b6d 100644 --- a/src/main/java/dev/mars3142/fhq/timezone_service/timezone/web/controllers/TimeZoneController.java +++ b/src/main/java/dev/mars3142/fhq/timezone_service/timezone/web/controllers/TimeZoneController.java @@ -1,8 +1,10 @@ package dev.mars3142.fhq.timezone_service.timezone.web.controllers; +import dev.mars3142.fhq.timezone_service.timezone.domain.entities.response.TimeApiTimezoneZoneResponse; import dev.mars3142.fhq.timezone_service.timezone.domain.model.response.LocationResponse; import dev.mars3142.fhq.timezone_service.timezone.domain.model.response.TimeZoneResponse; import dev.mars3142.fhq.timezone_service.timezone.service.TimeZoneService; +import java.util.Objects; import lombok.RequiredArgsConstructor; import lombok.val; import org.springframework.web.bind.annotation.GetMapping; @@ -19,8 +21,8 @@ public class TimeZoneController { private final TimeZoneService timeZoneService; @GetMapping - public TimeZoneResponse getTimeZone(@RequestHeader(value = "X-Forwarded-For", defaultValue = "127.0.0.1") String data) { - val clientIp = data.split(",")[0]; + public TimeZoneResponse getTimeZone(@RequestHeader(value = "X-Forwarded-For", defaultValue = "127.0.0.1") String header) { + val clientIp = header.split(",")[0]; val ip = timeZoneService.getExternalIp(clientIp); val timezoneInfo = timeZoneService.getTimeZoneInfoByIp(ip); val posix = timeZoneService.getPosixTimeZone(timezoneInfo.timezone()); @@ -37,7 +39,8 @@ public class TimeZoneController { public TimeZoneResponse getTimeZone(@PathVariable String area, @PathVariable String location) { val timezone = area + "/" + location; val timezoneInfo = timeZoneService.getTimeZoneInfo(timezone); + val abbreviation = Objects.requireNonNullElse(timezoneInfo.dstInterval(), new TimeApiTimezoneZoneResponse.Interval(null)).dstName(); val posix = timeZoneService.getPosixTimeZone(timezone); - return new TimeZoneResponse(timezone, timezoneInfo.dstInterval().dstName(), posix); + return new TimeZoneResponse(timezone, abbreviation, posix); } }