Search in sources :

Example 16 with Post

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);
}
Also used : Post(com.erudika.scoold.core.Post) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 17 with Post

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);
}
Also used : Post(com.erudika.scoold.core.Post) Pager(com.erudika.para.utils.Pager) Question(com.erudika.scoold.core.Question) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 18 with Post

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;
    }
}
Also used : Post(com.erudika.scoold.core.Post) Reply(com.erudika.scoold.core.Reply) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 19 with Post

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);
}
Also used : Post(com.erudika.scoold.core.Post) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 20 with Post

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";
}
Also used : Post(com.erudika.scoold.core.Post) Pager(com.erudika.para.utils.Pager) Reply(com.erudika.scoold.core.Reply) LinkedList(java.util.LinkedList) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

Post (com.erudika.scoold.core.Post)20 Profile (com.erudika.scoold.core.Profile)12 PostMapping (org.springframework.web.bind.annotation.PostMapping)9 GetMapping (org.springframework.web.bind.annotation.GetMapping)7 Pager (com.erudika.para.utils.Pager)6 Reply (com.erudika.scoold.core.Reply)5 Comment (com.erudika.scoold.core.Comment)4 Feedback (com.erudika.scoold.core.Feedback)4 ArrayList (java.util.ArrayList)4 ParaObject (com.erudika.para.core.ParaObject)3 Question (com.erudika.scoold.core.Question)3 HashMap (java.util.HashMap)3 Revision (com.erudika.scoold.core.Revision)2 LinkedList (java.util.LinkedList)2 Report (com.erudika.scoold.core.Report)1 SyndContent (com.sun.syndication.feed.synd.SyndContent)1 SyndContentImpl (com.sun.syndication.feed.synd.SyndContentImpl)1 SyndEntry (com.sun.syndication.feed.synd.SyndEntry)1 SyndEntryImpl (com.sun.syndication.feed.synd.SyndEntryImpl)1 SyndFeed (com.sun.syndication.feed.synd.SyndFeed)1