Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev -> main #126

Merged
merged 7 commits into from
Mar 15, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ public class CompileExecutionResponse {
@Schema(description = "코드 실행 결과", example = "3")
private final String result;

public static CompileExecutionResponse of(String result) {
return new CompileExecutionResponse(result);
@Schema(description = "컴파일 성공 여부", example = "true")
private final boolean isCompileSuccess;

public static CompileExecutionResponse success(String result) {
return new CompileExecutionResponse(result, true);
}

public static CompileExecutionResponse failure() {
return new CompileExecutionResponse("Compile Fail", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public static JdoodleCompileResponse of(
return new JdoodleCompileResponse(output, statusCode, memory, cpuTime, compilationStatus,
projectKey);
}

public boolean isCompileSuccess() {
return statusCode == 200;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ei.algobaroapi.domain.compile.exception;

import ei.algobaroapi.domain.compile.exception.common.CompileErrorCode;
import lombok.Getter;

@Getter
public class JdoodleException extends RuntimeException {

private final String errorCode;
private final String errorMessage;

private JdoodleException(CompileErrorCode errorCode) {
this.errorCode = errorCode.getErrorCode();
this.errorMessage = errorCode.getErrorMessage();
}

public static JdoodleException of(CompileErrorCode errorCode) {
return new JdoodleException(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ei.algobaroapi.domain.compile.exception.common;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum CompileErrorCode {

JDOODLE_API_EXCEPTION("E07401", "Jdoodle API 호출 중 오류가 발생했습니다.");

private final String errorCode;
private final String errorMessage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ei.algobaroapi.domain.compile.exception.common;

import ei.algobaroapi.domain.compile.exception.JdoodleException;
import ei.algobaroapi.global.response.message.ErrorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class CompileExceptionHandler {

@ExceptionHandler(JdoodleException.class)
public ResponseEntity<ErrorResponse> catchJdoodleException(JdoodleException e) {
log.error(e.getErrorCode());

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ErrorResponse.of(
e.getErrorCode(),
e.getErrorMessage())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import ei.algobaroapi.domain.compile.dto.request.JdoodleCompileRequest;
import ei.algobaroapi.domain.compile.dto.response.CompileExecutionResponse;
import ei.algobaroapi.domain.compile.dto.response.JdoodleCompileResponse;
import ei.algobaroapi.domain.compile.exception.JdoodleException;
import ei.algobaroapi.domain.compile.exception.common.CompileErrorCode;
import ei.algobaroapi.global.util.HttpUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -25,17 +27,26 @@ public class JdoodleCompileServiceImpl implements CompileService {

@Override
public CompileExecutionResponse executeCode(CompileExecutionRequest request) {
JdoodleCompileResponse jdoodleCompileResponse = sendJdoodleCompileRequest(
JdoodleCompileRequest.builder()
.clientId(clientId)
.clientSecret(clientSecret)
.stdin(request.getInput())
.script(request.getCode())
.language(request.getLanguage())
.build()
);
try {
JdoodleCompileResponse jdoodleCompileResponse = sendJdoodleCompileRequest(
JdoodleCompileRequest.builder()
.clientId(clientId)
.clientSecret(clientSecret)
.stdin(request.getInput())
.script(request.getCode())
.language(request.getLanguage())
.build()
);

if (jdoodleCompileResponse.isCompileSuccess()) {
return CompileExecutionResponse.success(jdoodleCompileResponse.getOutput());
} else {
return CompileExecutionResponse.failure();
}

return CompileExecutionResponse.of(jdoodleCompileResponse.getOutput());
} catch (Exception e) {
throw JdoodleException.of(CompileErrorCode.JDOODLE_API_EXCEPTION);
}
}

private JdoodleCompileResponse sendJdoodleCompileRequest(JdoodleCompileRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public Page<Room> findListPage(RoomListRequestDto request, Pageable pageable) {
.and(titleContainsIgnoreCase(request.getSearchTitle()))
.and(roomStatusEq(request.getRoomStatus()))
.and(roomAccessTypeEq(request.getRoomAccessType()))
.and(roomMemberNotZero())
.and(languageContains(request.getLanguages()));

List<Room> content = jpaQueryFactory
Expand Down Expand Up @@ -76,6 +77,10 @@ private BooleanExpression roomAccessTypeEq(RoomAccessType roomAccessType) {
}
}

private BooleanExpression roomMemberNotZero() {
return room.roomMembers.size().gt(0);
}

private BooleanExpression languageContains(List<String> languages) {
if (languages == null || languages.isEmpty()) {
return null;
Expand All @@ -86,10 +91,16 @@ private BooleanExpression languageContains(List<String> languages) {
BooleanExpression languageCondition = null;

for (String language : languages) {
String languageWithQuotation = "\"" + language + "\"";
BooleanExpression languageContainsExpression = template.containsIgnoreCase(
languageWithQuotation
);
if (languageCondition == null) {
languageCondition = template.containsIgnoreCase(language);
languageCondition = languageContainsExpression;
} else {
languageCondition = languageCondition.or(template.containsIgnoreCase(language));
languageCondition = languageCondition.or(
languageContainsExpression
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public interface CompileControllerDoc {

@Operation(summary = "코드 제출", description = "사용자가 제출한 코드를 컴파일합니다.")
@ApiResponse(responseCode = "201", description = "코드 컴파일 성공")
@ApiResponse(responseCode = "E07401", description = "Jdoodle API 호출 중 오류가 발생했습니다.")
CompileExecutionResponse compileCode(Member member, CompileExecutionRequest request);
}
Loading