Search in sources :

Example 1 with Reply

use of com.erudika.scoold.core.Reply in project scoold by Erudika.

the class QuestionController method replyAjax.

@PostMapping({ "/{id}", "/{id}/{title}" })
public String replyAjax(@PathVariable String id, @PathVariable(required = false) String title, @RequestParam(required = false) Boolean emailme, HttpServletRequest req, HttpServletResponse res, Model model) throws IOException {
    Post showPost = pc.read(id);
    Profile authUser = utils.getAuthUser(req);
    if (showPost != null && emailme != null) {
        if (emailme) {
            showPost.addFollower(authUser.getUser());
        } else {
            showPost.removeFollower(authUser.getUser());
        }
        // update without adding revisions
        pc.update(showPost);
    } else 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, Badge.EUREKA, answer.getCreatorid().equals(showPost.getCreatorid()));
            answer.setAuthor(authUser);
            model.addAttribute("showPost", showPost);
            model.addAttribute("answerslist", Collections.singletonList(answer));
            // send email to the question author
            sendReplyNotifications(showPost, answer);
            return "reply";
        }
    }
    if (utils.isAjaxRequest(req)) {
        res.setStatus(200);
        return "base";
    } else {
        return "redirect:" + questionslink + "/" + id;
    }
}
Also used : Post(com.erudika.scoold.core.Post) Reply(com.erudika.scoold.core.Reply) HashMap(java.util.HashMap) Map(java.util.Map) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with Reply

use of com.erudika.scoold.core.Reply in project scoold by Erudika.

the class QuestionController method approve.

@PostMapping("/{id}/approve/{answerid}")
public String approve(@PathVariable String id, @PathVariable String answerid, 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.canEdit(showPost, authUser) && answerid != null && utils.isMine(showPost, authUser)) {
        Reply answer = (Reply) pc.read(answerid);
        if (answer != null && answer.isReply()) {
            Profile author = pc.read(answer.getCreatorid());
            if (author != null && utils.isAuthenticated(req)) {
                boolean same = author.equals(authUser);
                if (answerid.equals(showPost.getAnswerid())) {
                    // Answer approved award - UNDO
                    showPost.setAnswerid("");
                    if (!same) {
                        author.removeRep(ANSWER_APPROVE_REWARD_AUTHOR);
                        authUser.removeRep(ANSWER_APPROVE_REWARD_VOTER);
                        pc.updateAll(Arrays.asList(author, authUser));
                    }
                } else {
                    // Answer approved award - GIVE
                    showPost.setAnswerid(answerid);
                    if (!same) {
                        author.addRep(ANSWER_APPROVE_REWARD_AUTHOR);
                        authUser.addRep(ANSWER_APPROVE_REWARD_VOTER);
                        utils.addBadgeOnce(authUser, Badge.NOOB, true);
                        pc.updateAll(Arrays.asList(author, authUser));
                    }
                }
                showPost.update();
            }
        }
    }
    return "redirect:" + showPost.getPostLink(false, false);
}
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 3 with Reply

use of com.erudika.scoold.core.Reply 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 4 with Reply

use of com.erudika.scoold.core.Reply 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)4 Reply (com.erudika.scoold.core.Reply)4 Profile (com.erudika.scoold.core.Profile)3 PostMapping (org.springframework.web.bind.annotation.PostMapping)3 Pager (com.erudika.para.utils.Pager)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1