zend-blog-3-backend
134 строки · 3.5 Кб
1<?php
2/**
3* Created by PhpStorm.
4* User: morontt
5* Date: 15.02.15
6* Time: 19:30
7*/
8
9namespace App\Controller\API;
10
11use App\Controller\BaseController;
12use App\Entity\Post;
13use App\Form\ArticleFormType;
14use App\Repository\PostRepository;
15use Symfony\Component\HttpFoundation\JsonResponse;
16use Symfony\Component\HttpFoundation\Request;
17use Symfony\Component\HttpFoundation\Response;
18use Symfony\Component\Routing\Annotation\Route;
19
20/**
21* @Route("/api/posts")
22*
23* Class PostController
24*/
25class PostController extends BaseController
26{
27/**
28* @Route("", methods={"GET"})
29*
30* @param Request $request
31* @param PostRepository $repository
32*
33* @return JsonResponse
34*/
35public 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
45return new JsonResponse($result);
46}
47
48/**
49* @Route("/{id}", requirements={"id": "\d+"}, methods={"GET"})
50*
51* @param Post $entity
52*
53* @return JsonResponse
54*/
55public function findAction(Post $entity): JsonResponse
56{
57$result = $this->getDataConverter()
58->getPost($entity);
59
60return 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*/
75public function createAction(Request $request): JsonResponse
76{
77$form = $this->createObjectForm('post', ArticleFormType::class);
78$form->handleRequest($request);
79
80[$formData, $errors] = $this->handleForm($form);
81if ($errors) {
82return new JsonResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
83}
84
85$result = $this->getDataConverter()->savePost(new Post(), $formData['post']);
86
87return 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*/
103public 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);
109if ($errors) {
110return new JsonResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
111}
112
113$result = $this->getDataConverter()->savePost($entity, $formData['post']);
114
115return 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*/
127public function deleteAction(Post $entity): JsonResponse
128{
129$this->getEm()->remove($entity);
130$this->getEm()->flush();
131
132return new JsonResponse(true);
133}
134}
135