spring

 avatar
unknown
java
2 years ago
1.6 kB
7
Indexable
package com.codility.tasks.spring.healthcheck;

import org.springframework.web.bind.annotation.*;

@RestController
public class HealthCheckController {

    @GetMapping(value = "/healthcheck", produces = MediaType.APPLICATION_JSON_VALUE)
    public HealthCheckResponse getHealthCheck(@RequestParam(required = false) String format) {
        if (format == null || format.isEmpty() || format.equalsIgnoreCase("short")) {
            return new HealthCheckResponse("OK");
        } else if (format.equalsIgnoreCase("full")) {
            String currentTime = ZonedDateTime.now().toString();
            return new HealthCheckResponse("OK", currentTime);
        } else {
            throw new IllegalArgumentException("Invalid format. Allowed values: 'short', 'full'");
        }
    }

    private static class HealthCheckResponse {
        private String status;
        private String currentTime;

        public HealthCheckResponse(String status) {
            this.status = status;
        }

        public HealthCheckResponse(String status, String currentTime) {
            this.status = status;
            this.currentTime = currentTime;
        }

        // Getters and setters

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getCurrentTime() {
            return currentTime;
        }

        public void setCurrentTime(String currentTime) {
            this.currentTime = currentTime;
        }
    }
}
Editor is loading...