-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewsController.php
More file actions
99 lines (84 loc) · 3.39 KB
/
NewsController.php
File metadata and controls
99 lines (84 loc) · 3.39 KB
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
<?php
namespace App\Controller\Project;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Manager\Project\ProjectManager;
use App\Manager\Project\NewsManager;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use App\Security\Voter\Project\NewsVoter;
class NewsController extends AbstractController
{
/**
* @Route("/api/projects/{slug}/news", name="get_all_project_news", methods={"GET"})
*/
public function getProjectNews(string $slug, ProjectManager $projectManager, NewsManager $newsManager)
{
if (($project = $projectManager->get($slug)) === null) {
throw new NotFoundHttpException('projects.not_found');
}
return new JsonResponse($newsManager->getProjectNews($project, $this->getUser()));
}
/**
* @Route("/api/projects/{slug}/news/{id}", name="get_project_news", methods={"GET"})
*/
public function getNews(int $id, NewsManager $newsManager)
{
return new JsonResponse($newsManager->get($id));
}
/**
* @Route("/api/news/last", name="get_last_news", methods={"GET"})
*/
public function getLastNews(NewsManager $newsManager)
{
return new JsonResponse($newsManager->getLastNews());
}
/**
* @Route("/api/projects/{slug}/news", name="create_project_news", methods={"POST"})
*/
public function create(Request $request, ProjectManager $projectManager, NewsManager $newsManager)
{
if (($project = $projectManager->get($request->attributes->get('slug'))) === null) {
throw new NotFoundHttpException('projects.not_found');
}
$this->denyAccessUnlessGranted(NewsVoter::CREATE, $project);
return new JsonResponse($newsManager->create($request->request->all(), $project, $this->getUser()), 201);
}
/**
* @Route("/api/projects/{slug}/news/{id}", name="update_project_news", methods={"PUT"})
*/
public function update(int $id, Request $request, NewsManager $newsManager)
{
if (($news = $newsManager->get($id)) === null) {
throw new NotFoundHttpException('projects.news.not_found');
}
$this->denyAccessUnlessGranted(NewsVoter::UPDATE, $news);
return new JsonResponse($newsManager->update($news, $request->request->all()));
}
/**
* @Route("/api/projects/{slug}/news/{id}/publish", name="publish_project_news", methods={"PATCH"})
*/
public function publish(int $id, NewsManager $newsManager)
{
if (($news = $newsManager->get($id)) === null) {
throw new NotFoundHttpException('news.not_found');
}
$this->denyAccessUnlessGranted(NewsVoter::PUBLISH, $news);
$newsManager->publish($news);
return new Response('', 204);
}
/**
* @Route("/api/projects/{slug}/news/{id}/unpublish", name="unpublish_project_news", methods={"PATCH"})
*/
public function unpublish(int $id, NewsManager $newsManager)
{
if (($news = $newsManager->get($id)) === null) {
throw new NotFoundHttpException('news.not_found');
}
$this->denyAccessUnlessGranted(NewsVoter::PUBLISH, $news);
$newsManager->unpublish($news);
return new Response('', 204);
}
}