Skip to content

Commit

Permalink
refactor: Optional<ProblemDto>로 감싸서 반환 (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonbinn committed Jan 24, 2024
1 parent 46dabbd commit 45906de
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,9 +28,10 @@ public class CompilerController {
schema = @Schema(implementation = ProblemDto.class)))
})
@GetMapping("/problems/{id}")
public SuccessResponse<ProblemDto> getProblem(@PathVariable String id) {
ProblemDto problem = problemService.getProblemById(id);
return new SuccessResponse<>(problem);
public ResponseEntity<SuccessResponse<ProblemDto>> getProblem(@PathVariable String id) {
return problemService.getProblemById(id)
.map(problemDto -> ResponseEntity.ok(new SuccessResponse<>(problemDto)))
.orElseGet(() -> ResponseEntity.notFound().build());
}

/*@Operation(summary = "코드 컴파일링 및 채점", description = "해당 문제 풀이에 대한 채점 결과를 보여줍니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProblemDto> 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<BojProblem> 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());
}
}
}

0 comments on commit 45906de

Please sign in to comment.