package kr.itn.itnhub.system; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.ExceptionHandler; import java.util.List; import java.util.Map; @RestController public class SystemResetController { /** 실행 확인 문구. 사용자가 그대로 입력해야만 초기화가 돈다. */ static final String CONFIRM_PHRASE = "채널 초기화"; private final SystemResetService resetService; public SystemResetController(SystemResetService resetService) { this.resetService = resetService; } @GetMapping("/api/system/reset/preview") public List preview() { return resetService.preview(); } /** * 되돌리기 번거로운 작업이라 확인 문구를 본문으로 받는다. 버튼 오클릭만으로는 절대 * 실행되지 않게 하기 위한 것이다(프런트의 확인창과 이중 방어). */ @PostMapping("/api/system/reset/channels") public ResetReport reset(@RequestBody ConfirmRequest request) { if (!CONFIRM_PHRASE.equals(request.confirm())) { throw new InvalidConfirmException("확인 문구가 일치하지 않습니다: \"%s\" 를 입력해야 합니다." .formatted(CONFIRM_PHRASE)); } return resetService.reset(); } public record ConfirmRequest(String confirm) { } static class InvalidConfirmException extends RuntimeException { InvalidConfirmException(String message) { super(message); } } @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(InvalidConfirmException.class) public Map handleInvalidConfirm(InvalidConfirmException e) { return Map.of("message", e.getMessage()); } }