181 lines
5.3 KiB
Java
181 lines
5.3 KiB
Java
/**
|
|
*
|
|
*/
|
|
package de.bstly.board.controller;
|
|
|
|
import java.time.Instant;
|
|
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 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("/c")
|
|
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;
|
|
|
|
|
|
@Value("${bstly.board.ranking.gravity:1.8}")
|
|
private double GRAVITY;
|
|
|
|
/**
|
|
* Ranked comments.
|
|
*
|
|
* @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
|
|
* @return the page
|
|
*/
|
|
@PreAuthorize("isAuthenticated()")
|
|
@GetMapping({ "/e/{target}", "/e/{target}/{parent}" })
|
|
public Page<Comment> rankedComments(@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) {
|
|
|
|
Page<Comment> comments = newComments(target, parent, pageParameter, sizeParameter,
|
|
dateParameter);
|
|
commentManager.applyMetadata(getCurrentUsername(), comments.getContent());
|
|
return comments;
|
|
|
|
// Page<Comment> comments = commentManager.fetchByRanking(target, parent.orElse(null),
|
|
// dateParameter.orElse(Instant.now()), gravityParameter.orElse(GRAVITY),
|
|
// pageParameter.orElse(0), sizeParameter.orElse(SIZE));
|
|
// return comments;
|
|
}
|
|
|
|
/**
|
|
* Count comments.
|
|
*
|
|
* @param target the target
|
|
* @param parent the parent
|
|
* @return the long
|
|
*/
|
|
@PreAuthorize("isAuthenticated()")
|
|
@GetMapping({ "/c/{target}", "/c/{target}/{parent}" })
|
|
public Long countComments(@PathVariable("target") Long target,
|
|
@PathVariable("parent") Optional<Long> parent) {
|
|
return commentManager.count(target, parent.orElse(null));
|
|
}
|
|
|
|
/**
|
|
* New comments.
|
|
*
|
|
* @param target the target
|
|
* @param parent the parent
|
|
* @param pageParameter the page parameter
|
|
* @param sizeParameter the size parameter
|
|
* @param dateParameter the date parameter
|
|
* @return the page
|
|
*/
|
|
@PreAuthorize("isAuthenticated()")
|
|
@GetMapping({ "/e/new/{target}", "/e/new/{target}/{parent}" })
|
|
public Page<Comment> newComments(@PathVariable("target") Long target,
|
|
@PathVariable("parent") Optional<Long> parent,
|
|
@RequestParam("page") Optional<Integer> pageParameter,
|
|
@RequestParam("size") Optional<Integer> sizeParameter,
|
|
@RequestParam("date") Optional<Instant> dateParameter) {
|
|
Page<Comment> comments = commentManager.fetchByDate(target, parent.orElse(null),
|
|
dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
|
|
sizeParameter.orElse(SIZE));
|
|
commentManager.applyMetadata(getCurrentUsername(), comments.getContent());
|
|
return comments;
|
|
}
|
|
|
|
/**
|
|
* Gets the comment.
|
|
*
|
|
* @param id the id
|
|
* @return the comment
|
|
*/
|
|
@PreAuthorize("isAuthenticated()")
|
|
@GetMapping("/{id}")
|
|
public Comment getComment(@PathVariable("id") Long id) {
|
|
Comment comment = commentManager.get(id);
|
|
|
|
if (comment == null) {
|
|
throw new EntityResponseStatusException(HttpStatus.NOT_FOUND);
|
|
}
|
|
|
|
commentManager.applyMetadata(getCurrentUsername(), comment);
|
|
|
|
return comment;
|
|
}
|
|
|
|
/**
|
|
* Creates the comment.
|
|
*
|
|
* @param comment the comment
|
|
* @return the comment
|
|
*/
|
|
@PreAuthorize("isAuthenticated()")
|
|
@PostMapping()
|
|
public Comment createComment(@RequestBody Comment comment) {
|
|
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);
|
|
|
|
return comment;
|
|
}
|
|
|
|
}
|