Skip to content
Snippets Groups Projects
Commit bf4eb6eb authored by Nicola Fulvio Calabria's avatar Nicola Fulvio Calabria
Browse files

Added crud to collections controller

parent 800f9f41
No related branches found
No related tags found
No related merge requests found
...@@ -13,14 +13,16 @@ import org.slf4j.Logger; ...@@ -13,14 +13,16 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; 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.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
* *
* @author Nicola Fulvio Calabria <nicola.calabria at inaf.it> * @author Nicola Fulvio Calabria <nicola.calabria at inaf.it>
*/ */
@RestController @RestController
public class CollectionsController { public class CollectionsController {
...@@ -33,7 +35,7 @@ public class CollectionsController { ...@@ -33,7 +35,7 @@ public class CollectionsController {
@GetMapping(value = "/collections") @GetMapping(value = "/collections")
public ResponseEntity<NodeCollectionsList> listCollections( public ResponseEntity<NodeCollectionsList> listCollections(
HttpServletRequest request, User principal) { HttpServletRequest request, User principal) {
LOG.debug("listCollections called for user {}", principal.getName()); LOG.debug("list collections called for user {}", principal.getName());
NodeCollectionsList ncl = new NodeCollectionsList(); NodeCollectionsList ncl = new NodeCollectionsList();
...@@ -43,12 +45,29 @@ public class CollectionsController { ...@@ -43,12 +45,29 @@ public class CollectionsController {
return ResponseEntity.ok(ncl); return ResponseEntity.ok(ncl);
} }
// create a new collection with specified title // create a new collection with specified title
@PutMapping(value = "/collections")
public ResponseEntity<String> createCollection(
@RequestBody String collectionName, User principal) {
LOG.debug("create collection called with name {} called for user {}",
collectionName, principal.getName());
collectionsService.createNewCollection(collectionName, principal.getName());
return ResponseEntity.ok("Collection created");
}
// delete collection by id // delete collection by id
@DeleteMapping(value = "/collections")
public ResponseEntity<String> deleteCollection(
@RequestBody Long collectionId, User principal) {
LOG.debug("delete collection called with id {} for user {}",
collectionId, principal.getName());
collectionsService.deleteCollectionById(collectionId, principal.getName());
return ResponseEntity.ok("Collection deleted");
}
} }
...@@ -7,6 +7,7 @@ package it.inaf.oats.vospace; ...@@ -7,6 +7,7 @@ package it.inaf.oats.vospace;
import it.inaf.oats.vospace.persistence.CollectionsDAO; import it.inaf.oats.vospace.persistence.CollectionsDAO;
import it.inaf.oats.vospace.datamodel.collections.NodeCollection; import it.inaf.oats.vospace.datamodel.collections.NodeCollection;
import it.inaf.oats.vospace.exception.PermissionDeniedException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -32,12 +33,32 @@ public class CollectionsService { ...@@ -32,12 +33,32 @@ public class CollectionsService {
if (isUserAuthenticated(userId)) { if (isUserAuthenticated(userId)) {
result.addAll( result.addAll(
collectionsDAO.getUserNodeCollections(userId)); collectionsDAO.getUserNodeCollections(userId));
} else {
throw new PermissionDeniedException("Authentication required");
} }
return result; return result;
} }
public void createNewCollection(String collectionTitle, String userId) {
if (isUserAuthenticated(userId)) {
collectionsDAO.createNewCollection(collectionTitle, userId);
} else {
throw new PermissionDeniedException("Authentication required");
}
}
public void deleteCollectionById(Long collectionId, String userId) {
if(isUserAuthenticated(userId))
{
// TODO: Implement delete
throw new UnsupportedOperationException("delete collection");
} else {
throw new PermissionDeniedException("Authentication required");
}
}
private boolean isUserAuthenticated(String userId) { private boolean isUserAuthenticated(String userId) {
return userId != null return userId != null
&& !userId.equals("anonymous"); && !userId.equals("anonymous");
......
...@@ -10,7 +10,6 @@ import javax.servlet.http.HttpServletRequest; ...@@ -10,7 +10,6 @@ import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
......
...@@ -30,7 +30,7 @@ public class CollectionsDAO { ...@@ -30,7 +30,7 @@ public class CollectionsDAO {
jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate = new JdbcTemplate(dataSource);
} }
void createNewCollection(String title, String ownerId) { public void createNewCollection(String title, String ownerId) {
String sql = "INSERT INTO collections (title, owner_id) VALUES (?,?)"; String sql = "INSERT INTO collections (title, owner_id) VALUES (?,?)";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment