optimize controller responses

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2024-10-28 16:26:46 +01:00
parent fd2d9873e1
commit 7119be77ad

View File

@@ -1,19 +1,16 @@
package dev.mars3142.fhq.timezone_service.timezone.controllers; package dev.mars3142.fhq.timezone_service.timezone.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.LocationResponse;
import dev.mars3142.fhq.timezone_service.timezone.domain.model.response.TimezoneResponse; import dev.mars3142.fhq.timezone_service.timezone.domain.model.response.TimezoneResponse;
import dev.mars3142.fhq.timezone_service.timezone.service.TimezoneService; import dev.mars3142.fhq.timezone_service.timezone.service.TimezoneService;
import java.util.Objects;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.val; import lombok.val;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@@ -24,32 +21,29 @@ public class TimezoneController {
private final TimezoneService timeZoneService; private final TimezoneService timeZoneService;
@GetMapping @GetMapping
public HttpEntity<TimezoneResponse> getTimeZone( @ResponseStatus(HttpStatus.OK)
public TimezoneResponse getTimeZone(
@RequestHeader(value = "X-Forwarded-For", defaultValue = "127.0.0.1") String header) { @RequestHeader(value = "X-Forwarded-For", defaultValue = "127.0.0.1") String header) {
val clientIp = header.split(",")[0]; val clientIp = header.split(",")[0];
val ip = timeZoneService.getExternalIp(clientIp); val ip = timeZoneService.getExternalIp(clientIp);
val timezoneInfo = timeZoneService.getTimeZoneInfoByIp(ip); val timezoneInfo = timeZoneService.getTimeZoneInfoByIp(ip);
val posix = timeZoneService.getPosixTimeZone(timezoneInfo.timezone()); val posix = timeZoneService.getPosixTimeZone(timezoneInfo.timezone());
return new ResponseEntity<>( return TimezoneResponse.builder().timezone(timezoneInfo.timezone())
TimezoneResponse.builder().timezone(timezoneInfo.timezone()) .posix_tz(posix).build();
.posix_tz(posix).build(), HttpStatus.OK);
} }
@GetMapping("{area}") @GetMapping("{area}")
@ResponseStatus(HttpStatus.OK)
public LocationResponse getLocations(@PathVariable String area) { public LocationResponse getLocations(@PathVariable String area) {
val locations = timeZoneService.getLocations(area); val locations = timeZoneService.getLocations(area);
return new LocationResponse(locations.size(), locations); return new LocationResponse(locations.size(), locations);
} }
@GetMapping("{area}/{location}") @GetMapping("{area}/{location}")
public HttpEntity<TimezoneResponse> getTimeZoneForLocation(@PathVariable String area, @PathVariable String location) { @ResponseStatus(HttpStatus.OK)
public TimezoneResponse getTimeZoneForLocation(@PathVariable String area, @PathVariable String location) {
val timezone = area + "/" + 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); val posix = timeZoneService.getPosixTimeZone(timezone);
return new ResponseEntity<>( return TimezoneResponse.builder().timezone(timezone).posix_tz(posix).build();
TimezoneResponse.builder().timezone(timezone).posix_tz(posix).build(),
HttpStatus.OK);
} }
} }