Skip to content

Commit

Permalink
small changes to checks and deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
Aqua-sc committed May 6, 2024
1 parent 25c4a1a commit 6021861
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProjectEntity> projectCheck = projectUtil.getProjectIfAdmin(projectId, auth.getUserEntity());
if (projectCheck.getStatus() != HttpStatus.OK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,17 @@ private ResponseEntity<?> alterTests(
HttpMethod httpMethod
) {

CheckResult<Pair<TestEntity, ProjectEntity>> 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<Pair<TestEntity, ProjectEntity>> updateCheckResult = testUtil.checkForTestUpdate(projectId, user, dockerImage, null, null, httpMethod);


if (!updateCheckResult.getStatus().equals(HttpStatus.OK)) {
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -267,7 +288,7 @@ public ResponseEntity<?> getTestProperty(long projectId, Auth auth, Function<Tes
@DeleteMapping(ApiRoutes.PROJECT_BASE_PATH + "/{projectid}/tests")
@Roles({UserRole.teacher, UserRole.student})
public ResponseEntity<?> deleteTestById(@PathVariable("projectid") long projectId, Auth auth) {
CheckResult<Pair<TestEntity, ProjectEntity>> updateCheckResult = testUtil.checkForTestUpdate(projectId, auth.getUserEntity(), null, null, null, null, HttpMethod.DELETE);
CheckResult<Pair<TestEntity, ProjectEntity>> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
import java.util.Optional;

public interface TestRepository extends JpaRepository<TestEntity, Long> {
@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<TestEntity> findByProjectId(long projectId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ public CheckResult<Void> deleteProject(long projectId) {
}
}

projectRepository.delete(projectEntity);

if (projectEntity.getTestId() != null) {
TestEntity testEntity = testRepository.findById(projectEntity.getTestId()).orElse(null);
if (testEntity == null) {
Expand All @@ -144,7 +142,7 @@ public CheckResult<Void> deleteProject(long projectId) {
return delRes;
}


projectRepository.delete(projectEntity);

return new CheckResult<>(HttpStatus.OK, "", null);
} catch (Exception e) {
Expand Down Expand Up @@ -179,16 +177,13 @@ public CheckResult<Void> deleteSubmissionById(long submissionId) {
* @return CheckResult with the status of the deletion
*/
public CheckResult<Void> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,9 +49,10 @@ public CheckResult<Pair<TestEntity, ProjectEntity>> checkForTestUpdate(
String dockerImage,
String dockerScript,
String dockerTemplate,
String structureTemplate,
HttpMethod httpMethod
) {


CheckResult<ProjectEntity> projectCheck = projectUtil.getProjectIfAdmin(projectId, user);
if (!projectCheck.getStatus().equals(HttpStatus.OK)) {
return new CheckResult<>(projectCheck.getStatus(), projectCheck.getMessage(), null);
Expand Down

0 comments on commit 6021861

Please sign in to comment.