From 45906de973260496673149a62bcb0d7e57a8f374 Mon Sep 17 00:00:00 2001 From: hyeonbin Date: Wed, 24 Jan 2024 21:29:25 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20Optional=EB=A1=9C=20?= =?UTF-8?q?=EA=B0=90=EC=8B=B8=EC=84=9C=20=EB=B0=98=ED=99=98=20(#28)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CompilerController.java | 8 +-- .../compiler/converter/ProblemConverter.java | 54 +++++++++---------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/api/TaveShot/domain/compiler/controller/CompilerController.java b/src/main/java/com/api/TaveShot/domain/compiler/controller/CompilerController.java index 8187546..a9e52ac 100644 --- a/src/main/java/com/api/TaveShot/domain/compiler/controller/CompilerController.java +++ b/src/main/java/com/api/TaveShot/domain/compiler/controller/CompilerController.java @@ -10,6 +10,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses; import lombok.RequiredArgsConstructor; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @@ -27,9 +28,10 @@ public class CompilerController { schema = @Schema(implementation = ProblemDto.class))) }) @GetMapping("/problems/{id}") - public SuccessResponse getProblem(@PathVariable String id) { - ProblemDto problem = problemService.getProblemById(id); - return new SuccessResponse<>(problem); + public ResponseEntity> getProblem(@PathVariable String id) { + return problemService.getProblemById(id) + .map(problemDto -> ResponseEntity.ok(new SuccessResponse<>(problemDto))) + .orElseGet(() -> ResponseEntity.notFound().build()); } /*@Operation(summary = "코드 컴파일링 및 채점", description = "해당 문제 풀이에 대한 채점 결과를 보여줍니다.") diff --git a/src/main/java/com/api/TaveShot/domain/compiler/converter/ProblemConverter.java b/src/main/java/com/api/TaveShot/domain/compiler/converter/ProblemConverter.java index 73f0084..9332995 100644 --- a/src/main/java/com/api/TaveShot/domain/compiler/converter/ProblemConverter.java +++ b/src/main/java/com/api/TaveShot/domain/compiler/converter/ProblemConverter.java @@ -4,38 +4,34 @@ import com.api.TaveShot.domain.compiler.dto.ProblemDto; import org.springframework.stereotype.Component; +import java.util.Optional; + @Component public class ProblemConverter { - public static ProblemDto convertToDto(BojProblem bojProblem) { - if (bojProblem == null) { - return null; - } - - return ProblemDto.builder() - .id(bojProblem.getId()) - .title(bojProblem.getTitle()) - .description(bojProblem.getDescription()) - .inputDescription(bojProblem.getInputDescription()) - .outputDescription(bojProblem.getOutputDescription()) - .sampleInput(bojProblem.getSampleInput()) - .sampleOutput(bojProblem.getSampleOutput()) - .build(); + public static Optional convertToDto(BojProblem bojProblem) { + return Optional.ofNullable(bojProblem) + .map(bp -> ProblemDto.builder() + .id(bp.getId()) + .title(bp.getTitle()) + .description(bp.getDescription()) + .inputDescription(bp.getInputDescription()) + .outputDescription(bp.getOutputDescription()) + .sampleInput(bp.getSampleInput()) + .sampleOutput(bp.getSampleOutput()) + .build()); } - public static BojProblem convertToEntity(ProblemDto problemDto) { - if (problemDto == null) { - return null; - } - - return BojProblem.builder() - .id(problemDto.getId()) - .title(problemDto.getTitle()) - .description(problemDto.getDescription()) - .inputDescription(problemDto.getInputDescription()) - .outputDescription(problemDto.getOutputDescription()) - .sampleInput(problemDto.getSampleInput()) - .sampleOutput(problemDto.getSampleOutput()) - .build(); + public static Optional convertToEntity(ProblemDto problemDto) { + return Optional.ofNullable(problemDto) + .map(pd -> BojProblem.builder() + .id(pd.getId()) + .title(pd.getTitle()) + .description(pd.getDescription()) + .inputDescription(pd.getInputDescription()) + .outputDescription(pd.getOutputDescription()) + .sampleInput(pd.getSampleInput()) + .sampleOutput(pd.getSampleOutput()) + .build()); } -} +} \ No newline at end of file