aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CommentBundle/Controller/WallabagCommentController.php
blob: be11f22601f93c5c6b0c396ebe40c3d8c73dc023 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

namespace Wallabag\CommentBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Wallabag\CommentBundle\Entity\Comment;
use Wallabag\CoreBundle\Entity\Entry;

class WallabagCommentController extends FOSRestController
{
    /**
     * Retrieve comments for an entry.
     *
     * @ApiDoc(
     *      requirements={
     *          {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
     *      }
     * )
     *
     * @return Response
     */
    public function getAnnotationsAction(Entry $entry)
    {
        $commentRows = $this
                ->getDoctrine()
                ->getRepository('WallabagCommentBundle:Comment')
                ->findCommentsByPageId($entry->getId(), $this->getUser()->getId());
        $total = count($commentRows);
        $comments = array('total' => $total, 'rows' => $commentRows);

        $json = $this->get('serializer')->serialize($comments, 'json');

        return $this->renderJsonResponse($json);
    }

    /**
     * Creates a new comment.
     *
     * @param Entry $entry
     *
     * @ApiDoc(
     *      requirements={
     *          {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"},
     *          {"name"="quote", "dataType"="string", "required"=false, "description"="Optional, quote for the comment"},
     *          {"name"="text", "dataType"="string", "required"=true, "description"=""},
     *      }
     * )
     *
     * @return Response
     */
    public function postAnnotationAction(Request $request, Entry $entry)
    {
        $data = json_decode($request->getContent(), true);

        $em = $this->getDoctrine()->getManager();

        $comment = new Comment($this->getUser());

        $comment->setText($data['text']);
        if (array_key_exists('quote', $data)) {
            $comment->setQuote($data['quote']);
        }
        if (array_key_exists('ranges', $data)) {
            $comment->setRanges($data['ranges']);
        }

        $comment->setEntry($entry);

        $em->persist($comment);
        $em->flush();

        $json = $this->get('serializer')->serialize($comment, 'json');

        return $this->renderJsonResponse($json);
    }

    /**
     * Updates a comment.
     *
     * @ApiDoc(
     *      requirements={
     *          {"name"="comment", "dataType"="string", "requirement"="\w+", "description"="The comment ID"}
     *      }
     * )
     *
     * @ParamConverter("comment", class="WallabagCommentBundle:Comment")
     *
     * @return Response
     */
    public function putAnnotationAction(Comment $comment, Request $request)
    {
        $data = json_decode($request->getContent(), true);

        if (!is_null($data['text'])) {
            $comment->setText($data['text']);
        }

        $em = $this->getDoctrine()->getManager();
        $em->flush();

        $json = $this->get('serializer')->serialize($comment, 'json');

        return $this->renderJsonResponse($json);
    }

    /**
     * Removes a comment.
     *
     * @ApiDoc(
     *      requirements={
     *          {"name"="comment", "dataType"="string", "requirement"="\w+", "description"="The comment ID"}
     *      }
     * )
     *
     * @ParamConverter("comment", class="WallabagCommentBundle:Comment")
     *
     * @return Response
     */
    public function deleteAnnotationAction(Comment $comment)
    {
        $em = $this->getDoctrine()->getManager();
        $em->remove($comment);
        $em->flush();

        $json = $this->get('serializer')->serialize($comment, 'json');

        return $this->renderJsonResponse($json);
    }

    /**
     * Send a JSON Response.
     * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string.
     *
     * @param string $json
     *
     * @return Response
     */
    private function renderJsonResponse($json, $code = 200)
    {
        return new Response($json, $code, array('application/json'));
    }
}