bstlboard-back/src/main/java/de/bstly/board/controller/BookmarksController.java

111 lines
3.3 KiB
Java

/**
*
*/
package de.bstly.board.controller;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Lists;
import de.bstly.board.businesslogic.BookmarksManager;
import de.bstly.board.businesslogic.EntryManager;
import de.bstly.board.businesslogic.UserManager;
import de.bstly.board.controller.support.EntityResponseStatusException;
import de.bstly.board.model.Entry;
/**
* The Class BookmarksController.
*/
@RestController
@RequestMapping("/bookmarks")
public class BookmarksController extends BaseController {
@Autowired
private BookmarksManager bookmarksManager;
@Autowired
private EntryManager entryManager;
@Autowired
private UserManager userManager;
@Value("${bstly.board.size:30}")
private int SIZE;
/**
* Gets the entries.
*
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param ignoreParameter the ignore parameter
* @return the entries
*/
@PreAuthorize("isAuthenticated()")
@GetMapping()
public Page<Entry> getEntries(@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
if (sizeParameter.isPresent() && sizeParameter.get() > 100) {
sizeParameter = Optional.of(100);
}
Page<Entry> entries = entryManager.fetchByBookmarks(getCurrentUsername(),
pageParameter.orElse(0), sizeParameter.orElse(SIZE));
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()),
entries.getContent(), ignore);
return entries;
}
/**
* Adds the entry.
*
* @param id the id
*/
@PreAuthorize("isAuthenticated()")
@PutMapping("/{id}")
public void addEntry(@PathVariable("id") Long id) {
if (!entryManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
}
if (bookmarksManager.hasEntry(getCurrentUsername(), id)) {
throw new EntityResponseStatusException(HttpStatus.NO_CONTENT);
}
bookmarksManager.addEntry(getCurrentUsername(), id);
}
/**
* Removes the entry.
*
* @param id the id
*/
@PreAuthorize("isAuthenticated()")
@DeleteMapping("/{id}")
public void removeEntry(@PathVariable("id") Long id) {
if (!entryManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
}
if (!bookmarksManager.hasEntry(getCurrentUsername(), id)) {
throw new EntityResponseStatusException(HttpStatus.NO_CONTENT);
}
bookmarksManager.removeEntry(getCurrentUsername(), id);
}
}