diff --git a/src/main/java/it/inaf/oats/vospace/CollectionsController.java b/src/main/java/it/inaf/oats/vospace/CollectionsController.java index b28ce23d63a39df82c22590863d1d1457bbda48b..a28ff482848b862c41989b515df6641341c97f0b 100644 --- a/src/main/java/it/inaf/oats/vospace/CollectionsController.java +++ b/src/main/java/it/inaf/oats/vospace/CollectionsController.java @@ -15,9 +15,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** @@ -59,14 +59,16 @@ public class CollectionsController { } // delete collection by id - @DeleteMapping(value = "/collections") + @DeleteMapping(value = "/collections/{collectionId}") public ResponseEntity deleteCollection( - @RequestParam("id") Long id, User principal) { + @PathVariable("collectionId") Long id, User principal) { LOG.debug("delete collection called with id {} for user {}", id, principal.getName()); collectionsService.deleteCollectionById(id, principal.getName()); + // TODO: manage case collection not found or request by someone who isn't + // the owner return ResponseEntity.ok("Collection deleted"); } diff --git a/src/main/java/it/inaf/oats/vospace/CollectionsService.java b/src/main/java/it/inaf/oats/vospace/CollectionsService.java index 79e89056419bff076556ed3a319c480d272c2846..eaba3e033382cf85ea35d6f4d89fbcb1da4a4ebe 100644 --- a/src/main/java/it/inaf/oats/vospace/CollectionsService.java +++ b/src/main/java/it/inaf/oats/vospace/CollectionsService.java @@ -52,8 +52,9 @@ public class CollectionsService { public void deleteCollectionById(Long collectionId, String userId) { if(isUserAuthenticated(userId)) { - // TODO: Implement delete - throw new UnsupportedOperationException("delete collection"); + collectionsDAO.deleteCollection(collectionId, userId); + // TODO: throw exceptions if collection not found or deletion requested + // by someone who is not the owner } else { throw new PermissionDeniedException("Authentication required"); } diff --git a/src/main/java/it/inaf/oats/vospace/persistence/CollectionsDAO.java b/src/main/java/it/inaf/oats/vospace/persistence/CollectionsDAO.java index d520bfc03eeac165da07d951feb757f1ac121a99..66a00838e2eaf031a0a7099506c1ce0da9c97d2f 100644 --- a/src/main/java/it/inaf/oats/vospace/persistence/CollectionsDAO.java +++ b/src/main/java/it/inaf/oats/vospace/persistence/CollectionsDAO.java @@ -83,11 +83,10 @@ public class CollectionsDAO { return nc; } - public void deleteCollection(Long collectionId) { - // TODO: this is just a stub for development. - String sql = "DELETE FROM collections WHERE collection_id = ?"; + public void deleteCollection(Long collectionId, String userId) { + String sql = "DELETE FROM collections WHERE collection_id = ? AND owner_id = ?"; - jdbcTemplate.update(sql, collectionId); + jdbcTemplate.update(sql, collectionId, userId); } private NodeCollection getNodeCollectionFromResultset(ResultSet rs) diff --git a/src/test/java/it/inaf/oats/vospace/persistence/CollectionsDAOTest.java b/src/test/java/it/inaf/oats/vospace/persistence/CollectionsDAOTest.java index 5b2d527fb3e64f3d2b6d4c4efb1de1fffeee4384..7b921292df679815974df6dfa86b73224f7c03de 100644 --- a/src/test/java/it/inaf/oats/vospace/persistence/CollectionsDAOTest.java +++ b/src/test/java/it/inaf/oats/vospace/persistence/CollectionsDAOTest.java @@ -52,7 +52,7 @@ public class CollectionsDAOTest { for(NodeCollection nc : ncl) { collectionsDAO.deleteCollection( - nc.getId() + nc.getId(), "pippo" ); }