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