Skip to content

Commit

Permalink
merged with dev
Browse files Browse the repository at this point in the history
  • Loading branch information
usserwoutV2 committed May 19, 2024
2 parents 9667598 + 40810de commit c88d48b
Show file tree
Hide file tree
Showing 10 changed files with 610 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@
import com.ugent.pidgeon.postgre.models.types.UserRole;
import com.ugent.pidgeon.postgre.repository.*;
import com.ugent.pidgeon.util.*;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -90,6 +101,15 @@ public ResponseEntity<?> getSubmission(@PathVariable("submissionid") long submis
return ResponseEntity.ok(submissionJson);
}

private Map<Long, Optional<SubmissionEntity>> getLatestSubmissionsForProject(long projectId) {
List<Long> groupIds = projectRepository.findGroupIdsByProjectId(projectId);
return groupIds.stream()
.collect(Collectors.toMap(
groupId -> groupId,
groupId -> submissionRepository.findLatestsSubmissionIdsByProjectAndGroupId(projectId, groupId)
));
}

/**
* Function to get all submissions
*
Expand All @@ -110,35 +130,35 @@ public ResponseEntity<?> getSubmissions(@PathVariable("projectid") long projecti
return ResponseEntity.status(checkResult.getStatus()).body(checkResult.getMessage());
}

List<Long> projectGroupIds = projectRepository.findGroupIdsByProjectId(projectid);
List<LastGroupSubmissionJson> res = projectGroupIds.stream().map(groupId -> {
GroupEntity group = groupRepository.findById(groupId).orElse(null);
Map<Long, Optional<SubmissionEntity>> submissions = getLatestSubmissionsForProject(projectid);
List<LastGroupSubmissionJson> res = new ArrayList<>();
for (Map.Entry<Long, Optional<SubmissionEntity>> entry : submissions.entrySet()) {
GroupEntity group = groupRepository.findById(entry.getKey()).orElse(null);
if (group == null) {
throw new RuntimeException("Group not found");
}
GroupJson groupjson = entityToJsonConverter.groupEntityToJson(group, false);
GroupFeedbackEntity groupFeedbackEntity = groupFeedbackRepository.getGroupFeedback(groupId, projectid);
GroupFeedbackEntity groupFeedbackEntity = groupFeedbackRepository.getGroupFeedback(entry.getKey(), projectid);
GroupFeedbackJson groupFeedbackJson;
if (groupFeedbackEntity == null) {
groupFeedbackJson = null;
} else {
groupFeedbackJson = entityToJsonConverter.groupFeedbackEntityToJson(groupFeedbackEntity);
}
SubmissionEntity submission = submissionRepository.findLatestsSubmissionIdsByProjectAndGroupId(projectid, groupId).orElse(null);
SubmissionEntity submission = entry.getValue().orElse(null);
if (submission == null) {
return new LastGroupSubmissionJson(null, groupjson, groupFeedbackJson);
res.add(new LastGroupSubmissionJson(null, groupjson, groupFeedbackJson));
continue;
}
res.add(new LastGroupSubmissionJson(entityToJsonConverter.getSubmissionJson(submission), groupjson, groupFeedbackJson));
}

return new LastGroupSubmissionJson(entityToJsonConverter.getSubmissionJson(submission), groupjson, groupFeedbackJson);

}).toList();
return ResponseEntity.ok(res);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}


/**
* Function to submit a file
*
Expand Down Expand Up @@ -306,6 +326,55 @@ public ResponseEntity<?> getSubmissionFile(@PathVariable("submissionid") long su
return Filehandler.getZipFileAsResponse(Path.of(file.getPath()), file.getName());
}

@GetMapping(ApiRoutes.PROJECT_BASE_PATH + "/{projectid}/submissions/files")
@Roles({UserRole.teacher, UserRole.student})
public ResponseEntity<?> getSubmissionsFiles(@PathVariable("projectid") long projectid, @RequestParam(value = "artifacts", required = false) Boolean artifacts, Auth auth) {
try {
CheckResult<Void> checkResult = projectUtil.isProjectAdmin(projectid, auth.getUserEntity());
if (!checkResult.getStatus().equals(HttpStatus.OK)) {
return ResponseEntity.status(checkResult.getStatus()).body(checkResult.getMessage());
}

Path tempDir = Files.createTempDirectory("SELAB6CANDELETEallsubmissions");
Path mainZipPath = tempDir.resolve("main.zip");
try (ZipOutputStream mainZipOut = new ZipOutputStream(Files.newOutputStream(mainZipPath))) {
Map<Long, Optional<SubmissionEntity>> submissions = getLatestSubmissionsForProject(projectid);
for (Map.Entry<Long, Optional<SubmissionEntity>> entry : submissions.entrySet()) {
SubmissionEntity submission = entry.getValue().orElse(null);
if (submission == null) {
continue;
}
FileEntity file = fileRepository.findById(submission.getFileId()).orElse(null);
if (file == null) {
continue;
}

// Create the group-specific zip file in a temporary location
Path groupZipPath = tempDir.resolve("group-" + submission.getGroupId() + ".zip");
try (ZipOutputStream groupZipOut = new ZipOutputStream(Files.newOutputStream(groupZipPath))) {
File submissionZip = Path.of(file.getPath()).toFile();
Filehandler.addExistingZip(groupZipOut, "files.zip", submissionZip);

if (artifacts != null && artifacts) {
Path artifactPath = Filehandler.getSubmissionArtifactPath(projectid, submission.getGroupId(), submission.getId());
File artifactZip = artifactPath.toFile();
if (artifactZip.exists()) {
Filehandler.addExistingZip(groupZipOut, "artifacts.zip", artifactZip);
}
}

}

Filehandler.addExistingZip(mainZipOut, "group-" + submission.getGroupId() + ".zip", groupZipPath.toFile());
}
}

return Filehandler.getZipFileAsResponse(mainZipPath, "allsubmissions.zip");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}

@GetMapping(ApiRoutes.SUBMISSION_BASE_PATH + "/{submissionid}/artifacts") //Route to get a submission
@Roles({UserRole.teacher, UserRole.student})
public ResponseEntity<?> getSubmissionArtifacts(@PathVariable("submissionid") long submissionid, Auth auth) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.ugent.pidgeon.postgre.models.types.DockerTestState;
import com.ugent.pidgeon.postgre.models.types.DockerTestType;
import com.ugent.pidgeon.postgre.repository.*;
import java.io.File;
import java.nio.file.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -252,6 +254,14 @@ else if (submission.getDockerTestType().equals(DockerTestType.SIMPLE)) {
} else {
feedback = new DockerTestFeedbackJson(DockerTestType.TEMPLATE, submission.getDockerFeedback(), submission.getDockerAccepted());
}

boolean artifactsExist;
if (submission.getGroupId() != null) {
Path artifactPath = Filehandler.getSubmissionArtifactPath(submission.getProjectId(), submission.getGroupId(), submission.getId());
artifactsExist = new File(artifactPath.toString()).exists();
} else {
artifactsExist = false;
}
return new SubmissionJson(
submission.getId(),
ApiRoutes.PROJECT_BASE_PATH + "/" + submission.getProjectId(),
Expand All @@ -264,7 +274,7 @@ else if (submission.getDockerTestType().equals(DockerTestType.SIMPLE)) {
submission.getStructureFeedback(),
feedback,
submission.getDockerTestState().toString(),
ApiRoutes.SUBMISSION_BASE_PATH + "/" + submission.getId() + "/artifacts"
artifactsExist ? ApiRoutes.SUBMISSION_BASE_PATH + "/" + submission.getId() + "/artifacts" : null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static File saveFile(Path directory, MultipartFile file, String filename)

try {
// Create a temporary file and save the uploaded file to it
File tempFile = File.createTempFile("uploaded-zip-", ".zip");
File tempFile = File.createTempFile("SELAB6CANDELETEuploaded-zip-", ".zip");
file.transferTo(tempFile);

// Check if the file is a ZIP file
Expand Down Expand Up @@ -213,4 +213,15 @@ public static ResponseEntity<?> getZipFileAsResponse(Path path, String filename)
.headers(headers)
.body(zipFile);
}


public static void addExistingZip(ZipOutputStream groupZipOut, String zipFileName, File zipFile) throws IOException {
ZipEntry zipEntry = new ZipEntry(zipFileName);
groupZipOut.putNextEntry(zipEntry);

// Read the content of the zip file and write it to the group zip output stream
Files.copy(zipFile.toPath(), groupZipOut);

groupZipOut.closeEntry();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ public CheckResult<Void> checkGroupFeedbackUpdateJson(UpdateGroupScoreRequest re
return new CheckResult<>(projectCheck.getStatus(), projectCheck.getMessage(), null);
}
Integer maxScore = projectCheck.getData().getMaxScore();
if ((request.getScore() == null && maxScore != null) || request.getFeedback() == null) {
return new CheckResult<>(HttpStatus.BAD_REQUEST, "Score and feedback need to be provided", null);
if (request.getFeedback() == null) {
return new CheckResult<>(HttpStatus.BAD_REQUEST, "Feedbacks need to be provided", null);
}

if (request.getScore() != null && request.getScore() < 0) {
return new CheckResult<>(HttpStatus.BAD_REQUEST, "Score can't be lower than 0", null);
}

if (maxScore != null && request.getScore() > maxScore) {
if (maxScore != null && request.getScore() != null && request.getScore() > maxScore) {
return new CheckResult<>(HttpStatus.BAD_REQUEST, "Score can't be higher than the defined max score (" + maxScore + ")", null);
}

Expand Down
Loading

0 comments on commit c88d48b

Please sign in to comment.