Skip to content

Commit

Permalink
clean repo on delete (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndc-dxc authored Mar 26, 2024
1 parent ff9a2cd commit 4683a34
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.gov.innovazione.ndc.controller;

import it.gov.innovazione.ndc.harvester.HarvesterService;
import it.gov.innovazione.ndc.harvester.service.RepositoryService;
import it.gov.innovazione.ndc.model.harvester.Repository;
import lombok.Data;
Expand All @@ -22,6 +23,7 @@
import java.net.URL;
import java.security.Principal;
import java.util.List;
import java.util.Optional;

import static org.springframework.http.HttpStatus.CREATED;

Expand All @@ -32,6 +34,7 @@
public class RepositoryController {

private final RepositoryService repositoryService;
private final HarvesterService harvesterService;

@GetMapping
public List<Repository> getAllRepositories() {
Expand Down Expand Up @@ -86,11 +89,27 @@ public ResponseEntity<Void> updateRepository(
public ResponseEntity<?> deleteRepository(
@PathVariable String id,
Principal principal) {
Optional<Repository> optionalRepository = repositoryService.getActiveRepos().stream()
.filter(repo -> repo.getId().equals(id))
.findFirst();

if (optionalRepository.isEmpty()) {
log.warn("Repository {} not found or not active", id);
return ResponseEntity.notFound().build();
}

Repository repository = optionalRepository.get();

harvesterService.clear(repository.getUrl());

int deleted = repositoryService.delete(id, principal);

if (deleted == 0) {
log.warn("Repository {} not found", id);
return ResponseEntity.notFound().build();
}

log.info("Repository {} deleted", repository);
return ResponseEntity.noContent().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ public Optional<Repository> findRepoById(String id) {

@SneakyThrows
public void createRepo(String url, String name, String description, Long maxFileSizeBytes, Principal principal) {
if (repoAlreadyExists(url)) {
reactivate(url, name, description, maxFileSizeBytes, principal);
return;
}

// does not exist but repo to create is a substring of an existing repo,
// or existing repo is a substring of the repo to create
boolean isDuplicate = getAllRepos().stream()
.anyMatch(repo ->
startsWithIgnoreCase(
Expand Down Expand Up @@ -189,6 +196,11 @@ public void createRepo(String url, String name, String description, Long maxFile
maxFileSizeBytes);
}

private boolean repoAlreadyExists(String url) {
return getAllRepos().stream()
.anyMatch(repo -> repo.getUrl().equals(url));
}

public int updateRepo(String id, RepositoryController.CreateRepository loadedRepo, Principal principal) {
String query = "UPDATE REPOSITORY SET "
+ "URL = ?, "
Expand Down Expand Up @@ -221,6 +233,25 @@ public int delete(String id, Principal principal) {
id);
}

public int reactivate(String url, String name, String description, Long maxFileSizeBytes, Principal principal) {
String query = "UPDATE REPOSITORY SET "
+ "ACTIVE = ?, "
+ "NAME = ?, "
+ "DESCRIPTION = ?, "
+ "MAX_FILE_SIZE_BYTES = ?, "
+ "UPDATED = ?, "
+ "UPDATED_BY = ? "
+ "WHERE URL = ?";
return jdbcTemplate.update(query,
true,
name,
description,
maxFileSizeBytes,
java.sql.Timestamp.from(java.time.Instant.now()),
principal.getName(),
url);
}

@SneakyThrows
public void storeRightsHolders(Repository repository, Map<String, Map<String, String>> rightsHolders) {
log.info("Storing rights holders for repository {}", repository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.With;

import java.time.Instant;
Expand All @@ -12,6 +13,7 @@
@Data
@Builder(toBuilder = true)
@EqualsAndHashCode(exclude = {"id", "createdAt", "updatedAt"})
@ToString(exclude = {"rightsHolders"})
public class Repository {
private String id;
@With
Expand Down

0 comments on commit 4683a34

Please sign in to comment.