-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from Tave100Shot/28-feat-컴파일러
feat 컴파일러
- Loading branch information
Showing
10 changed files
with
277 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/com/api/TaveShot/domain/compiler/controller/CompilerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.api.TaveShot.domain.compiler.controller; | ||
|
||
import com.api.TaveShot.domain.compiler.dto.ProblemDto; | ||
import com.api.TaveShot.domain.compiler.service.ProblemService; | ||
import com.api.TaveShot.global.success.SuccessResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
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 | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/compile") | ||
public class CompilerController { | ||
|
||
private final ProblemService problemService; | ||
//private final CompilerService compilerService; | ||
|
||
@Operation(summary = "문제 정보 가져오기", description = "해당 문제 번호의 문제 정보들을 보여줍니다.") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "문제 정보 가져오기 성공", | ||
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, | ||
schema = @Schema(implementation = ProblemDto.class))) | ||
}) | ||
@GetMapping("/problems/{id}") | ||
public SuccessResponse<ProblemDto> getProblem(@PathVariable String id) { | ||
ProblemDto problemDto = problemService.getProblemById(id); | ||
return new SuccessResponse<>(problemDto); | ||
} | ||
|
||
/*@Operation(summary = "코드 컴파일링 및 채점", description = "해당 문제 풀이에 대한 채점 결과를 보여줍니다.") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "채점 성공", | ||
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, | ||
schema = @Schema(implementation = String.class))) | ||
}) | ||
@PostMapping("/submit") | ||
public SuccessResponse<String> submitCode(@RequestBody @Validated SubmissionRequestDto submissionRequestDto) { | ||
String result = compilerService.submitCode(submissionRequestDto); | ||
return new SuccessResponse<>(result); | ||
}*/ | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/api/TaveShot/domain/compiler/converter/ProblemConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.api.TaveShot.domain.compiler.converter; | ||
|
||
import com.api.TaveShot.domain.compiler.domain.BojProblem; | ||
import com.api.TaveShot.domain.compiler.dto.ProblemDto; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Optional; | ||
|
||
public class ProblemConverter { | ||
|
||
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 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()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/api/TaveShot/domain/compiler/domain/BojProblem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.api.TaveShot.domain.compiler.domain; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Entity | ||
@Table(name = "bog_problems") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
@AllArgsConstructor | ||
@Builder | ||
public class BojProblem { | ||
|
||
@Id | ||
@Column(name = "ID", columnDefinition = "TEXT") | ||
private String id; | ||
|
||
@Column(name = "Title", columnDefinition = "TEXT") | ||
private String title; | ||
|
||
@Lob | ||
@Column(name = "Description") | ||
private String description; | ||
|
||
@Lob | ||
@Column(name = "Input Description") | ||
private String inputDescription; | ||
|
||
@Column(name = "Output Description", columnDefinition = "TEXT") | ||
private String outputDescription; | ||
|
||
@Column(name = "Sample Input", columnDefinition = "TEXT") | ||
private String sampleInput; | ||
|
||
@Column(name = "Sample Output", columnDefinition = "TEXT") | ||
private String sampleOutput; | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/api/TaveShot/domain/compiler/dto/ProblemDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.api.TaveShot.domain.compiler.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonRawValue; | ||
import com.opencsv.bean.CsvBindByName; | ||
import lombok.*; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class ProblemDto { | ||
|
||
@JsonProperty("ID") | ||
private String id; | ||
|
||
@JsonProperty("Title") | ||
private String title; | ||
|
||
@JsonProperty("Description") | ||
private String description; | ||
|
||
@JsonProperty("Input Description") | ||
private String inputDescription; | ||
|
||
@JsonProperty("Output Description") | ||
private String outputDescription; | ||
|
||
@JsonProperty("Sample Input") | ||
private String sampleInput; | ||
|
||
@JsonProperty("Sample Output") | ||
private String sampleOutput; | ||
|
||
private String problemUrl; | ||
|
||
public String getProblemUrl(){ | ||
this.problemUrl = "https://www.acmicpc.net/problem/" + this.id; | ||
return this.problemUrl; | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/api/TaveShot/domain/compiler/dto/SubmissionRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/*package com.api.TaveShot.domain.compiler.dto; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class SubmissionRequestDto { | ||
@NotEmpty | ||
private String problemId; | ||
@NotEmpty | ||
private String language; | ||
@NotEmpty | ||
private String sourceCode; | ||
}*/ |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/api/TaveShot/domain/compiler/repository/ProblemRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.api.TaveShot.domain.compiler.repository; | ||
|
||
import com.api.TaveShot.domain.compiler.domain.BojProblem; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
public interface ProblemRepository extends JpaRepository<BojProblem, String> { | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/api/TaveShot/domain/compiler/service/CompilerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/*package com.api.TaveShot.domain.compiler.service; | ||
import com.api.TaveShot.domain.compiler.dto.SubmissionRequestDto; | ||
import com.api.TaveShot.global.exception.ApiException; | ||
import com.api.TaveShot.global.exception.ErrorType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
import java.util.Map; | ||
@Service | ||
public class CompilerService { | ||
private static final String SUBMIT_CODE_URL = "http://43.202.52.177/submitCode"; | ||
private static final String RESULT_URL = "http://43.202.52.177/result/"; | ||
private static final int MAX_ATTEMPTS = 10; | ||
private static final long SLEEP_TIME_MILLIS = 5000; | ||
public String submitCode(SubmissionRequestDto submissionRequestDto) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
ResponseEntity<Map> submitResponse = restTemplate.postForEntity(SUBMIT_CODE_URL, submissionRequestDto, Map.class); | ||
String submissionId = (String) submitResponse.getBody().get("submission_id"); | ||
String result = "결과 처리 중"; | ||
int attempts = 0; | ||
while (result.equals("결과 처리 중") && attempts < MAX_ATTEMPTS) { | ||
ResponseEntity<Map> resultResponse = restTemplate.getForEntity(RESULT_URL + submissionId, Map.class); | ||
result = (String) resultResponse.getBody().get("result"); | ||
if (result.contains("정답이 준비되지 않아, 코드를 제출할 수 없습니다.")) { | ||
throw new ApiException(ErrorType._SUBMIT_PAGE_NOT_FOUND); | ||
} | ||
try { | ||
Thread.sleep(SLEEP_TIME_MILLIS); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
attempts++; | ||
} | ||
return result; | ||
} | ||
}*/ |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/api/TaveShot/domain/compiler/service/ProblemService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.api.TaveShot.domain.compiler.service; | ||
|
||
import com.api.TaveShot.domain.compiler.converter.ProblemConverter; | ||
import com.api.TaveShot.domain.compiler.domain.BojProblem; | ||
import com.api.TaveShot.domain.compiler.dto.ProblemDto; | ||
import com.api.TaveShot.domain.compiler.repository.ProblemRepository; | ||
import com.api.TaveShot.global.exception.ApiException; | ||
import com.api.TaveShot.global.exception.ErrorType; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Optional; | ||
|
||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ProblemService { | ||
|
||
private final ProblemRepository problemRepository; | ||
|
||
public ProblemDto getProblemById(String id) { | ||
BojProblem bojProblem = problemRepository.findById(id) | ||
.orElseThrow(() -> new ApiException(ErrorType._PROBLEM_NOT_FOUND)); | ||
|
||
return ProblemConverter.convertToDto(bojProblem).orElseThrow(() -> new ApiException(ErrorType._PROBLEM_CONVERSION_ERROR)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters