optimize controller responses
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -1,19 +1,16 @@
|
||||
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.TimezoneResponse;
|
||||
import dev.mars3142.fhq.timezone_service.timezone.service.TimezoneService;
|
||||
import java.util.Objects;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.val;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpStatus;
|
||||
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.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@@ -24,32 +21,29 @@ public class TimezoneController {
|
||||
private final TimezoneService timeZoneService;
|
||||
|
||||
@GetMapping
|
||||
public HttpEntity<TimezoneResponse> getTimeZone(
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
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());
|
||||
return new ResponseEntity<>(
|
||||
TimezoneResponse.builder().timezone(timezoneInfo.timezone())
|
||||
.posix_tz(posix).build(), HttpStatus.OK);
|
||||
return TimezoneResponse.builder().timezone(timezoneInfo.timezone())
|
||||
.posix_tz(posix).build();
|
||||
}
|
||||
|
||||
@GetMapping("{area}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public LocationResponse getLocations(@PathVariable String area) {
|
||||
val locations = timeZoneService.getLocations(area);
|
||||
return new LocationResponse(locations.size(), locations);
|
||||
}
|
||||
|
||||
@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 timezoneInfo = timeZoneService.getTimeZoneInfo(timezone);
|
||||
val abbreviation = Objects.requireNonNullElse(timezoneInfo.dstInterval(),
|
||||
new TimeApiTimezoneZoneResponse.Interval(null)).dstName();
|
||||
val posix = timeZoneService.getPosixTimeZone(timezone);
|
||||
return new ResponseEntity<>(
|
||||
TimezoneResponse.builder().timezone(timezone).posix_tz(posix).build(),
|
||||
HttpStatus.OK);
|
||||
return TimezoneResponse.builder().timezone(timezone).posix_tz(posix).build();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user