Commit bf4eb6eb authored by Nicola Fulvio Calabria's avatar Nicola Fulvio Calabria
Browse files

Added crud to collections controller

parent 800f9f41
Loading
Loading
Loading
Loading
+33 −14
Original line number Diff line number Diff line
@@ -13,14 +13,16 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author Nicola Fulvio Calabria <nicola.calabria at inaf.it>
 */

@RestController
public class CollectionsController {

@@ -33,7 +35,7 @@ public class CollectionsController {
    @GetMapping(value = "/collections")
    public ResponseEntity<NodeCollectionsList> listCollections(
            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();

@@ -43,12 +45,29 @@ public class CollectionsController {
        return ResponseEntity.ok(ncl);
    }

    
    // 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
    @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");
                
    }
    
}
+21 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ package it.inaf.oats.vospace;

import it.inaf.oats.vospace.persistence.CollectionsDAO;
import it.inaf.oats.vospace.datamodel.collections.NodeCollection;
import it.inaf.oats.vospace.exception.PermissionDeniedException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
@@ -32,12 +33,32 @@ public class CollectionsService {
        if (isUserAuthenticated(userId)) {
            result.addAll(
                    collectionsDAO.getUserNodeCollections(userId));
        } else {
            throw new PermissionDeniedException("Authentication required");
        }

        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) {
        return userId != null
                && !userId.equals("anonymous");
+0 −1
Original line number Diff line number Diff line
@@ -10,7 +10,6 @@ import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
+1 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ public class CollectionsDAO {
        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 (?,?)";