feat: add attempt limit validation for submissions

This commit is contained in:
BillieFaiqul 2025-07-04 18:31:15 +07:00
parent bc28180777
commit bf65115b50
2 changed files with 78 additions and 5 deletions

View File

@ -682,9 +682,18 @@ public function restart(Request $request)
if ($request->submission_id == null) return response()->json([ if ($request->submission_id == null) return response()->json([
'message' => 'Submission ID is required', 'message' => 'Submission ID is required',
], 404); ], 404);
$user = Auth::user(); $user = Auth::user();
$submission = Submission::where('id', $request->submission_id)->where('user_id', $user->id)->first(); $submission = Submission::where('id', $request->submission_id)->where('user_id', $user->id)->first();
if ($submission) { if ($submission) {
// Check if attempts count is 3 or more
if ($submission->attempts >= 3) {
return response()->json([
'message' => 'Maximum attempts reached. Cannot restart submission.',
], 403);
}
$submission->createHistory("Submission has been restarted"); $submission->createHistory("Submission has been restarted");
if ($submission->port != null) { if ($submission->port != null) {
@ -697,6 +706,7 @@ public function restart(Request $request)
['rm', '-rf', $this->getTempDir($submission)], ['rm', '-rf', $this->getTempDir($submission)],
]; ];
} }
// Delete temp directory // Delete temp directory
foreach ($commands as $command) { foreach ($commands as $command) {
if (!$this->is_dir_empty($this->getTempDir($submission))) { if (!$this->is_dir_empty($this->getTempDir($submission))) {
@ -716,6 +726,7 @@ public function restart(Request $request)
'message' => 'Submission has been restarted successfully', 'message' => 'Submission has been restarted successfully',
], 200); ], 200);
} }
return response()->json([ return response()->json([
'message' => 'Submission not found', 'message' => 'Submission not found',
], 404); ], 404);
@ -726,9 +737,18 @@ public function changeSourceCode($submission_id)
{ {
$user = Auth::user(); $user = Auth::user();
$submission = Submission::where('id', $submission_id)->where('user_id', $user->id)->first(); $submission = Submission::where('id', $submission_id)->where('user_id', $user->id)->first();
if ($submission) { if ($submission) {
// Check if attempts count is 3 or more
if ($submission->attempts >= 3) {
return response()->json([
'message' => 'Maximum attempts reached. Cannot change source code.',
], 403);
}
return view('nodejs.submissions.change_source_code', compact('submission')); return view('nodejs.submissions.change_source_code', compact('submission'));
} }
return redirect()->route('submissions'); return redirect()->route('submissions');
} }

View File

@ -14,7 +14,7 @@
@section('scripts') @section('scripts')
<script type="text/javascript"> <script type="text/javascript">
function requestServer(element){ function requestServer(element){
let submission_id = element.attr('data-submission-id'); let submission_id = element.attr('data-submission-id');
let request_type = element.attr('data-request-type'); let request_type = element.attr('data-request-type');
@ -89,21 +89,74 @@ function requestServer(element){
window.location = "/nodejs/submissions"; window.location = "/nodejs/submissions";
}); });
}, },
error: function(data) { error: function(xhr, status, error) {
let errorMessage = "Something went wrong!";
console.log("Error status:", xhr.status);
console.log("Error response:", xhr.responseText);
// Check if it's a 403 error (maximum attempts reached)
if (xhr.status === 403) {
try {
let response = JSON.parse(xhr.responseText);
errorMessage = response.message || "Maximum attempts reached. Cannot restart submission.";
} catch (e) {
errorMessage = "Maximum attempts reached. Cannot restart submission.";
}
} else if (xhr.responseJSON && xhr.responseJSON.message) {
errorMessage = xhr.responseJSON.message;
}
swal({ swal({
title: "Error!", title: "Error!",
text: "Something went wrong!", text: errorMessage,
icon: "error", icon: "error",
button: "Ok", button: "Ok",
}); });
console.log(data);
} }
}); });
} }
}); });
break; break;
case "change-source-code": case "change-source-code":
window.location = '/nodejs/submissions/change/' + submission_id; // First check if attempts limit is reached via AJAX
$.ajax({
url: '/nodejs/submissions/change/' + submission_id,
type: 'GET',
headers: {
'X-CSRF-TOKEN': '{{ csrf_token()}}'
},
success: function(data) {
// If successful, redirect to the change source code page
window.location = '/nodejs/submissions/change/' + submission_id;
},
error: function(xhr, status, error) {
// Handle 403 error (maximum attempts reached)
if (xhr.status === 403) {
let errorMessage = "Maximum attempts reached. Cannot change source code.";
try {
let response = JSON.parse(xhr.responseText);
errorMessage = response.message || errorMessage;
} catch (e) {
// Use default message if parsing fails
}
swal({
title: "Error!",
text: errorMessage,
icon: "error",
button: "Ok",
});
} else {
swal({
title: "Error!",
text: "Something went wrong!",
icon: "error",
button: "Ok",
});
}
}
});
break; break;
default: default:
break; break;