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

211 lines
7.1 KiB
Java

/**
*
*/
package de.bstly.board.controller;
import java.time.Instant;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
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.CommentManager;
import de.bstly.board.businesslogic.VoteManager;
import de.bstly.board.controller.support.EntityResponseStatusException;
import de.bstly.board.controller.support.RequestBodyErrors;
import de.bstly.board.controller.validation.CommentValidator;
import de.bstly.board.model.Comment;
import de.bstly.board.model.Types;
import de.bstly.board.model.Vote;
import de.bstly.board.model.VoteType;
/**
* The Class CommentController.
*/
@RestController
@RequestMapping("/comments")
public class CommentController extends BaseController {
@Autowired
private CommentManager commentManager;
@Autowired
private CommentValidator commentValidator;
@Autowired
private VoteManager voteManager;
@Value("${bstly.board.size:30}")
private int SIZE;
/**
* Fetch by rank.
*
* @param target the target
* @param parent the parent
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @param gravityParameter the gravity parameter
* @param ignoreParameter the ignore parameter
* @return the page
*/
@PreAuthorize("isAuthenticated()")
@GetMapping({ "/{target}", "/{target}/{parent}" })
public Page<Comment> fetchByRank(@PathVariable("target") Long target,
@PathVariable("parent") Optional<Long> parent,
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("gravity") Optional<Double> gravityParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
Page<Comment> comments = fetchByDate(target, parent, pageParameter, sizeParameter,
dateParameter, Optional.of(false), ignoreParameter);
return comments;
}
/**
* Fetch by date.
*
* @param target the target
* @param parent the parent
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @param descParameter the desc parameter
* @param ignoreParameter the ignore parameter
* @return the page
*/
@PreAuthorize("isAuthenticated()")
@GetMapping({ "/new/{target}", "/new/{target}/{parent}" })
public Page<Comment> fetchByDate(@PathVariable("target") Long target,
@PathVariable("parent") Optional<Long> parent,
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("desc") Optional<Boolean> descParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
Page<Comment> comments = commentManager.fetchByDate(target, parent.orElse(null),
dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE), descParameter.orElse(false));
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList("entry"));
commentManager.applyMetadata(getCurrentUsername(), comments.getContent(), ignore);
return comments;
}
/**
* Fetch by username.
*
* @param username the username
* @param parent the parent
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @param ascParameter the asc parameter
* @param ignoreParameter the ignore parameter
* @return the page
*/
@PreAuthorize("isAuthenticated()")
@GetMapping({ "/byuser/{username}" })
public Page<Comment> fetchByUsername(@PathVariable("username") String username,
@PathVariable("parent") Optional<Long> parent,
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("asc") Optional<Boolean> ascParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
Page<Comment> comments = commentManager.fetchByUsername(username, parent.orElse(null),
dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE), ascParameter.orElse(false));
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
commentManager.applyMetadata(getCurrentUsername(), comments.getContent(), ignore);
return comments;
}
/**
* Count comments.
*
* @param target the target
* @param parent the parent
* @return the long
*/
@PreAuthorize("isAuthenticated()")
@GetMapping({ "/count/{target}", "/count/{target}/{parent}" })
public Long countComments(@PathVariable("target") Long target,
@PathVariable("parent") Optional<Long> parent) {
return commentManager.count(target, parent.orElse(null));
}
/**
* Gets the comment.
*
* @param id the id
* @param ignoreParameter the ignore parameter
* @return the comment
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/comment/{id}")
public Comment getComment(@PathVariable("id") Long id,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
Comment comment = commentManager.get(id);
if (comment == null) {
throw new EntityResponseStatusException(HttpStatus.NOT_FOUND);
}
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
commentManager.applyMetadata(getCurrentUsername(), comment, ignore);
return comment;
}
/**
* Creates the comment.
*
* @param comment the comment
* @param ignoreParameter the ignore parameter
* @return the comment
*/
@PreAuthorize("isAuthenticated()")
@PostMapping
public Comment createComment(@RequestBody Comment comment,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
RequestBodyErrors bindingResult = new RequestBodyErrors(comment);
commentValidator.validate(comment, bindingResult);
if (bindingResult.hasErrors()) {
throw new EntityResponseStatusException(bindingResult.getAllErrors(),
HttpStatus.UNPROCESSABLE_ENTITY);
}
comment.setCreated(Instant.now());
comment.setAuthor(getCurrentUsername());
comment = commentManager.save(comment);
Vote vote = new Vote();
vote.setTarget(comment.getId());
vote.setTargetType(Types.comment);
vote.setType(VoteType.up);
vote.setAuthor(getCurrentUsername());
voteManager.save(vote);
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList("entry"));
commentManager.applyMetadata(getCurrentUsername(), comment, ignore);
return comment;
}
}