use of com.erudika.scoold.core.Post in project scoold by Erudika.
the class QuestionController method delete.
@PostMapping("/{id}/delete")
public String delete(@PathVariable String id, HttpServletRequest req) {
Post showPost = pc.read(id);
Profile authUser = utils.getAuthUser(req);
if (!utils.canEdit(showPost, authUser) || showPost == null) {
return "redirect:" + req.getRequestURI();
}
if (!showPost.isReply()) {
if ((utils.isMine(showPost, authUser) || utils.isMod(authUser))) {
showPost.delete();
return "redirect:" + questionslink + "?success=true&code=16";
}
} else if (showPost.isReply()) {
if (utils.isMine(showPost, authUser) || utils.isMod(authUser)) {
Post parent = pc.read(showPost.getParentid());
parent.setAnswercount(parent.getAnswercount() - 1);
parent.update();
showPost.delete();
}
}
return "redirect:" + showPost.getPostLink(false, false);
}
use of com.erudika.scoold.core.Post in project scoold by Erudika.
the class QuestionsController method getSimilarAjax.
@GetMapping("/questions/similar/{like}")
public void getSimilarAjax(@PathVariable String like, HttpServletRequest req, HttpServletResponse res) throws IOException {
StringBuilder sb = new StringBuilder();
Question q = new Question();
q.setTitle(like);
q.setBody("");
q.setTags(Arrays.asList(""));
for (Post similarPost : utils.getSimilarPosts(q, new Pager(Config.getConfigInt("max_similar_posts", 7)))) {
boolean hasAnswer = !StringUtils.isBlank(similarPost.getAnswerid());
sb.append("<span class=\"lightborder phm").append(hasAnswer ? " light-green white-text" : "").append("\">");
sb.append(similarPost.getVotes());
sb.append("</span> <a href=\"").append(similarPost.getPostLink(false, false)).append("\">");
sb.append(similarPost.getTitle()).append("</a><br>");
}
res.getWriter().print(sb.toString());
res.setStatus(200);
}
use of com.erudika.scoold.core.Post in project scoold by Erudika.
the class FeedbackController method replyAjax.
@PostMapping({ "/{id}", "/{id}/{title}" })
public String replyAjax(@PathVariable String id, @PathVariable(required = false) String title, HttpServletRequest req, HttpServletResponse res, Model model) throws IOException {
Post showPost = pc.read(id);
Profile authUser = utils.getAuthUser(req);
if (showPost != null && !showPost.isClosed() && !showPost.isReply()) {
//create new answer
Reply answer = utils.populate(req, new Reply(), "body");
Map<String, String> error = utils.validate(answer);
if (!error.containsKey("body")) {
answer.setTitle(showPost.getTitle());
answer.setCreatorid(authUser.getId());
answer.setParentid(showPost.getId());
answer.create();
showPost.setAnswercount(showPost.getAnswercount() + 1);
if (showPost.getAnswercount() >= MAX_REPLIES_PER_POST) {
showPost.setCloserid("0");
}
// update without adding revisions
pc.update(showPost);
utils.addBadgeAndUpdate(authUser, Profile.Badge.EUREKA, answer.getCreatorid().equals(showPost.getCreatorid()));
answer.setAuthor(authUser);
model.addAttribute("showPost", showPost);
model.addAttribute("answerslist", Collections.singletonList(answer));
return "reply";
}
}
if (utils.isAjaxRequest(req)) {
res.setStatus(200);
return "base";
} else {
return "redirect:" + feedbacklink + "/" + id;
}
}
use of com.erudika.scoold.core.Post in project scoold by Erudika.
the class QuestionController method close.
@PostMapping("/{id}/close")
public String close(@PathVariable String id, HttpServletRequest req) {
Post showPost = pc.read(id);
Profile authUser = utils.getAuthUser(req);
if (!utils.canEdit(showPost, authUser) || showPost == null) {
return "redirect:" + req.getRequestURI();
}
if (utils.isMod(authUser)) {
if (showPost.isClosed()) {
showPost.setCloserid(null);
} else {
showPost.setCloserid(authUser.getId());
}
showPost.update();
}
return "redirect:" + showPost.getPostLink(false, false);
}
use of com.erudika.scoold.core.Post in project scoold by Erudika.
the class QuestionController method get.
@GetMapping({ "/{id}", "/{id}/{title}" })
public String get(@PathVariable String id, @PathVariable(required = false) String title, @RequestParam(required = false) String sortby, HttpServletRequest req, HttpServletResponse res, Model model) {
Post showPost = pc.read(id);
if (showPost == null || !ParaObjectUtils.typesMatch(showPost)) {
return "redirect:" + questionslink;
}
Pager itemcount = utils.getPager("page", req);
itemcount.setSortby("newest".equals(sortby) ? "timestamp" : "votes");
List<Reply> answerslist = showPost.getAnswers(itemcount);
LinkedList<Post> allPosts = new LinkedList<Post>();
allPosts.add(showPost);
allPosts.addAll(answerslist);
utils.fetchProfiles(allPosts);
getComments(allPosts);
updateViewCount(showPost, req, res);
model.addAttribute("path", "question.vm");
model.addAttribute("title", utils.getLang(req).get("questions.title") + " - " + showPost.getTitle());
model.addAttribute("description", Utils.abbreviate(Utils.stripAndTrim(showPost.getBody(), " "), 195));
model.addAttribute("itemcount", itemcount);
model.addAttribute("showPost", allPosts.removeFirst());
model.addAttribute("answerslist", allPosts);
model.addAttribute("similarquestions", utils.getSimilarPosts(showPost, new Pager(10)));
model.addAttribute("maxCommentLength", Comment.MAX_COMMENT_LENGTH);
model.addAttribute("maxCommentLengthError", Utils.formatMessage(utils.getLang(req).get("maxlength"), Comment.MAX_COMMENT_LENGTH));
return "base";
}
Aggregations