Skip to content

Commit

Permalink
Some changes to the admin backend.
Browse files Browse the repository at this point in the history
* Adding a route now returns the added route object. Changed some method signatures to get the saved instance.
* Remove now uses path params and id, as angular does not allow for sending data in DELETE requests. angular/angular.js#3207
  • Loading branch information
verath committed Oct 25, 2014
1 parent 6a0c501 commit ad329bd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand Down Expand Up @@ -48,13 +49,13 @@ public class AdminController {
private FileThreadService fileThreadService;

@RequestMapping(value = "/addPath", method = RequestMethod.POST)
public ResponseEntity<String> addPath(@RequestBody ListeningPath path) {
public ResponseEntity<ListeningPath> addPath(@RequestBody ListeningPath path) {
File f = new File(path.getListeningPath());
if (f.exists() && f.isDirectory()) {
try {
databaseHelper.addPath(f.toPath());
ListeningPath savedPath = databaseHelper.addPath(f.toPath());
fileThreadService.addListeningPath(f.toPath());
return new ResponseEntity<>(HttpStatus.OK);
return new ResponseEntity<>(savedPath, HttpStatus.OK);
} catch (DataIntegrityViolationException e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
Expand All @@ -63,16 +64,21 @@ public ResponseEntity<String> addPath(@RequestBody ListeningPath path) {
}
}

@RequestMapping(value = "/removePath", method = RequestMethod.DELETE)
public ResponseEntity<String> removePath(@RequestBody ListeningPath path) {
File f = new File(path.getListeningPath());
try {
databaseHelper.removePath(f.toPath());
fileThreadService.removeListeningPath(f.toPath());
movieDatabaseHelper.removeFile(f.toPath());
return new ResponseEntity<>(HttpStatus.OK);
} catch (NullPointerException e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
@RequestMapping(value = "/removePath/{id}", method = RequestMethod.DELETE)
public ResponseEntity<String> removePath(@PathVariable long id) {
ListeningPath lp = listeningPathRepository.findOne(id);
if(lp != null) {
File f = new File(lp.getListeningPath());
try {
databaseHelper.removePath(f.toPath());
fileThreadService.removeListeningPath(f.toPath());
movieDatabaseHelper.removeFile(f.toPath());
return new ResponseEntity<>(HttpStatus.OK);
} catch (NullPointerException e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* and open the template in the editor.
*/
package edu.chalmers.dat076.moviefinder.service;
import edu.chalmers.dat076.moviefinder.persistence.ListeningPath;
import java.nio.file.Path;
import org.springframework.dao.DataIntegrityViolationException;

Expand All @@ -13,7 +14,7 @@
*/
public interface ListeningPathDatabaseHandler {

public void addPath(Path p) throws DataIntegrityViolationException;
public ListeningPath addPath(Path p) throws DataIntegrityViolationException;

public void removePath(Path p) throws NullPointerException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class ListeningPathDatabaseHandlerImpl implements ListeningPathDatabaseHa
@Autowired
ListeningPathRepository repository;

public void addPath(Path p) throws DataIntegrityViolationException{
repository.save(new ListeningPath(p.toString()));
public ListeningPath addPath(Path p) throws DataIntegrityViolationException{
return repository.save(new ListeningPath(p.toString()));
}

public void removePath(Path p) throws NullPointerException {
Expand Down

0 comments on commit ad329bd

Please sign in to comment.