zend-blog-3-backend

Форк
0
134 строки · 3.5 Кб
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: morontt
5
 * Date: 15.02.15
6
 * Time: 19:30
7
 */
8

9
namespace App\Controller\API;
10

11
use App\Controller\BaseController;
12
use App\Entity\Post;
13
use App\Form\ArticleFormType;
14
use App\Repository\PostRepository;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\Routing\Annotation\Route;
19

20
/**
21
 * @Route("/api/posts")
22
 *
23
 * Class PostController
24
 */
25
class PostController extends BaseController
26
{
27
    /**
28
     * @Route("", methods={"GET"})
29
     *
30
     * @param Request $request
31
     * @param PostRepository $repository
32
     *
33
     * @return JsonResponse
34
     */
35
    public function findAllAction(Request $request, PostRepository $repository): JsonResponse
36
    {
37
        $pagination = $this->paginate(
38
            $repository->getListQuery(),
39
            $request->query->get('page', 1)
40
        );
41

42
        $result = $this->getDataConverter()
43
            ->getPostArray($pagination, 'category');
44

45
        return new JsonResponse($result);
46
    }
47

48
    /**
49
     * @Route("/{id}", requirements={"id": "\d+"}, methods={"GET"})
50
     *
51
     * @param Post $entity
52
     *
53
     * @return JsonResponse
54
     */
55
    public function findAction(Post $entity): JsonResponse
56
    {
57
        $result = $this->getDataConverter()
58
            ->getPost($entity);
59

60
        return new JsonResponse($result);
61
    }
62

63
    /**
64
     * @Route("", methods={"POST"})
65
     *
66
     * @param Request $request
67
     *
68
     * @throws \Doctrine\ORM\Exception\NotSupported
69
     * @throws \Doctrine\ORM\Exception\ORMException
70
     * @throws \Psr\Container\ContainerExceptionInterface
71
     * @throws \Psr\Container\NotFoundExceptionInterface
72
     *
73
     * @return JsonResponse
74
     */
75
    public function createAction(Request $request): JsonResponse
76
    {
77
        $form = $this->createObjectForm('post', ArticleFormType::class);
78
        $form->handleRequest($request);
79

80
        [$formData, $errors] = $this->handleForm($form);
81
        if ($errors) {
82
            return new JsonResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
83
        }
84

85
        $result = $this->getDataConverter()->savePost(new Post(), $formData['post']);
86

87
        return new JsonResponse($result, Response::HTTP_CREATED);
88
    }
89

90
    /**
91
     * @Route("/{id}", requirements={"id": "\d+"}, methods={"PUT"})
92
     *
93
     * @param Request $request
94
     * @param Post $entity
95
     *
96
     * @throws \Doctrine\ORM\Exception\NotSupported
97
     * @throws \Doctrine\ORM\Exception\ORMException
98
     * @throws \Psr\Container\ContainerExceptionInterface
99
     * @throws \Psr\Container\NotFoundExceptionInterface
100
     *
101
     * @return JsonResponse
102
     */
103
    public function updateAction(Request $request, Post $entity): JsonResponse
104
    {
105
        $form = $this->createObjectForm('post', ArticleFormType::class, true);
106
        $form->handleRequest($request);
107

108
        [$formData, $errors] = $this->handleForm($form);
109
        if ($errors) {
110
            return new JsonResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
111
        }
112

113
        $result = $this->getDataConverter()->savePost($entity, $formData['post']);
114

115
        return new JsonResponse($result);
116
    }
117

118
    /**
119
     * @Route("/{id}", requirements={"id": "\d+"}, methods={"DELETE"})
120
     *
121
     * @param Post $entity
122
     *
123
     * @throws \Doctrine\ORM\Exception\ORMException
124
     *
125
     * @return JsonResponse
126
     */
127
    public function deleteAction(Post $entity): JsonResponse
128
    {
129
        $this->getEm()->remove($entity);
130
        $this->getEm()->flush();
131

132
        return new JsonResponse(true);
133
    }
134
}
135

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.