From 6021861d3961a1050df38d2fafcd6b6e33f03d27 Mon Sep 17 00:00:00 2001 From: Aqua-sc <108478185+Aqua-sc@users.noreply.github.com> Date: Mon, 6 May 2024 12:04:07 +0200 Subject: [PATCH] small changes to checks and deleting --- .../controllers/ProjectController.java | 2 +- .../pidgeon/controllers/TestController.java | 31 ++++++++++++++++--- .../postgre/repository/TestRepository.java | 9 +++++- .../pidgeon/util/CommonDatabaseActions.java | 19 +++++------- .../java/com/ugent/pidgeon/util/TestUtil.java | 4 ++- 5 files changed, 45 insertions(+), 20 deletions(-) diff --git a/backend/app/src/main/java/com/ugent/pidgeon/controllers/ProjectController.java b/backend/app/src/main/java/com/ugent/pidgeon/controllers/ProjectController.java index 53058ab6..7beebc7d 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/controllers/ProjectController.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/controllers/ProjectController.java @@ -311,7 +311,7 @@ public ResponseEntity getGroupsOfProject(@PathVariable Long projectId, Auth a * @return ResponseEntity with the status, no content */ @DeleteMapping(ApiRoutes.PROJECT_BASE_PATH + "/{projectId}") - @Roles({UserRole.teacher}) + @Roles({UserRole.teacher, UserRole.student}) public ResponseEntity deleteProjectById(@PathVariable long projectId, Auth auth) { CheckResult projectCheck = projectUtil.getProjectIfAdmin(projectId, auth.getUserEntity()); if (projectCheck.getStatus() != HttpStatus.OK) { diff --git a/backend/app/src/main/java/com/ugent/pidgeon/controllers/TestController.java b/backend/app/src/main/java/com/ugent/pidgeon/controllers/TestController.java index 31bc1aca..aee370dc 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/controllers/TestController.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/controllers/TestController.java @@ -97,7 +97,17 @@ private ResponseEntity alterTests( HttpMethod httpMethod ) { - CheckResult> updateCheckResult = testUtil.checkForTestUpdate(projectId, user, dockerImage, null, null, structureTemplate, httpMethod); + if (dockerImage != null && dockerImage.isBlank()) { + dockerImage = null; + } + if (dockerScript != null && dockerScript.isBlank()) { + dockerScript = null; + } + if (dockerTemplate != null && dockerTemplate.isBlank()) { + dockerTemplate = null; + } + + CheckResult> updateCheckResult = testUtil.checkForTestUpdate(projectId, user, dockerImage, null, null, httpMethod); if (!updateCheckResult.getStatus().equals(HttpStatus.OK)) { @@ -107,6 +117,11 @@ private ResponseEntity alterTests( TestEntity testEntity = updateCheckResult.getData().getFirst(); ProjectEntity projectEntity = updateCheckResult.getData().getSecond(); + // Creating a test entry + if(httpMethod.equals(HttpMethod.POST)){ + testEntity = new TestEntity(); + } + // delete test entry if(httpMethod.equals(HttpMethod.DELETE)){ // first check if docker image is not used anywhere else @@ -123,8 +138,7 @@ private ResponseEntity alterTests( } // Docker test - if(!(dockerImage == null && dockerScript == null && dockerTemplate == null)){ - + if(dockerImage != null){ // update/install image if possible. DockerSubmissionTestModel.installImage(dockerImage); testEntity.setDockerImage(dockerImage); @@ -134,11 +148,18 @@ private ResponseEntity alterTests( } // save structure template + if (!httpMethod.equals(HttpMethod.PATCH) || structureTemplate != null) { + if (structureTemplate != null && structureTemplate.isBlank()) { + structureTemplate = null; + } + } else { + structureTemplate = testEntity.getStructureTemplate(); + } testEntity.setStructureTemplate(structureTemplate); - projectEntity.setTestId(testEntity.getId()); // save test entity testEntity = testRepository.save(testEntity); + projectEntity.setTestId(testEntity.getId()); projectRepository.save(projectEntity); // make sure to update test id in project return ResponseEntity.ok(entityToJsonConverter.testEntityToTestJson(testEntity, projectId)); @@ -267,7 +288,7 @@ public ResponseEntity getTestProperty(long projectId, Auth auth, Function deleteTestById(@PathVariable("projectid") long projectId, Auth auth) { - CheckResult> updateCheckResult = testUtil.checkForTestUpdate(projectId, auth.getUserEntity(), null, null, null, null, HttpMethod.DELETE); + CheckResult> updateCheckResult = testUtil.checkForTestUpdate(projectId, auth.getUserEntity(), null, null, null, HttpMethod.DELETE); if (!updateCheckResult.getStatus().equals(HttpStatus.OK)) { return ResponseEntity.status(updateCheckResult.getStatus()).body(updateCheckResult.getMessage()); } diff --git a/backend/app/src/main/java/com/ugent/pidgeon/postgre/repository/TestRepository.java b/backend/app/src/main/java/com/ugent/pidgeon/postgre/repository/TestRepository.java index cb81a67c..744d016c 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/postgre/repository/TestRepository.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/postgre/repository/TestRepository.java @@ -7,8 +7,15 @@ import java.util.Optional; public interface TestRepository extends JpaRepository { - @Query(value = "SELECT * FROM tests WHERE docker_image = ?1", nativeQuery = true) + @Query(value = """ + SELECT CASE WHEN EXISTS (SELECT t FROM TestEntity t WHERE t.dockerImage = ?1) + THEN true + ELSE false + END + """) boolean imageIsUsed(String image); + + @Query(value ="SELECT t FROM ProjectEntity p JOIN TestEntity t ON p.testId = t.id WHERE p.id = ?1") Optional findByProjectId(long projectId); } diff --git a/backend/app/src/main/java/com/ugent/pidgeon/util/CommonDatabaseActions.java b/backend/app/src/main/java/com/ugent/pidgeon/util/CommonDatabaseActions.java index 8d880a1e..a4e64a58 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/util/CommonDatabaseActions.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/util/CommonDatabaseActions.java @@ -133,8 +133,6 @@ public CheckResult deleteProject(long projectId) { } } - projectRepository.delete(projectEntity); - if (projectEntity.getTestId() != null) { TestEntity testEntity = testRepository.findById(projectEntity.getTestId()).orElse(null); if (testEntity == null) { @@ -144,7 +142,7 @@ public CheckResult deleteProject(long projectId) { return delRes; } - + projectRepository.delete(projectEntity); return new CheckResult<>(HttpStatus.OK, "", null); } catch (Exception e) { @@ -179,16 +177,13 @@ public CheckResult deleteSubmissionById(long submissionId) { * @return CheckResult with the status of the deletion */ public CheckResult deleteTestById(ProjectEntity projectEntity, TestEntity testEntity) { - try { - projectRepository.save(projectEntity); - testRepository.deleteById(testEntity.getId()); - if(!testRepository.imageIsUsed(testEntity.getDockerImage())){ - DockerSubmissionTestModel.removeDockerImage(testEntity.getDockerImage()); - } - return new CheckResult<>(HttpStatus.OK, "", null); - } catch (Exception e) { - return new CheckResult<>(HttpStatus.INTERNAL_SERVER_ERROR, "Error while deleting test", null); + projectEntity.setTestId(null); + projectRepository.save(projectEntity); + testRepository.deleteById(testEntity.getId()); + if(!testRepository.imageIsUsed(testEntity.getDockerImage())){ + DockerSubmissionTestModel.removeDockerImage(testEntity.getDockerImage()); } + return new CheckResult<>(HttpStatus.OK, "", null); } /** diff --git a/backend/app/src/main/java/com/ugent/pidgeon/util/TestUtil.java b/backend/app/src/main/java/com/ugent/pidgeon/util/TestUtil.java index 0a1281e6..6307098d 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/util/TestUtil.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/util/TestUtil.java @@ -7,6 +7,7 @@ import com.ugent.pidgeon.postgre.models.TestEntity; import com.ugent.pidgeon.postgre.models.UserEntity; import com.ugent.pidgeon.postgre.repository.TestRepository; +import java.util.logging.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; @@ -48,9 +49,10 @@ public CheckResult> checkForTestUpdate( String dockerImage, String dockerScript, String dockerTemplate, - String structureTemplate, HttpMethod httpMethod ) { + + CheckResult projectCheck = projectUtil.getProjectIfAdmin(projectId, user); if (!projectCheck.getStatus().equals(HttpStatus.OK)) { return new CheckResult<>(projectCheck.getStatus(), projectCheck.getMessage(), null);