-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectController.php
More file actions
68 lines (60 loc) · 2.32 KB
/
ProjectController.php
File metadata and controls
68 lines (60 loc) · 2.32 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
<?php
namespace App\Controller\Project;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Manager\User\UserManager;
use App\Manager\Project\ProjectManager;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Security\Voter\Project\ProjectVoter;
class ProjectController extends AbstractController
{
/**
* @Route("/api/projects", name="get_projects", methods={"GET"})
*/
public function getAll(ProjectManager $projectManager)
{
return new JsonResponse($projectManager->getAll());
}
/**
* @Route("/api/projects/{slug}", name="get_project", methods={"GET"})
*/
public function getProject(ProjectManager $projectManager, string $slug)
{
if (($project = $projectManager->get($slug)) === null) {
throw new NotFoundHttpException('projects.not_found');
}
return new JsonResponse($project);
}
/**
* @Route("/api/users/{id}/projects", name="get_user_projects", methods={"GET"})
*/
public function getUserProjects(int $id, UserManager $userManager, ProjectManager $projectManager)
{
if (($user = $userManager->get($id)) === null) {
throw new NotFoundHttpException('users.not_found');
}
return new JsonResponse($projectManager->getUserProjects($user));
}
/**
* @Route("/api/projects", name="create_project", methods={"POST"})
*/
public function createProject(Request $request, ProjectManager $projectManager)
{
$this->denyAccessUnlessGranted('ROLE_USER');
return new JsonResponse($projectManager->create($request->request->all(), $this->getUser()), 201);
}
/**
* @Route("/api/projects/{slug}", name="update_project", methods={"PUT"})
*/
public function updateProject(string $slug, Request $request, ProjectManager $projectManager)
{
if (($project = $projectManager->get($slug)) === null) {
throw new NotFoundHttpException('project.not_found');
}
$this->denyAccessUnlessGranted(ProjectVoter::UPDATE, $project);
$projectManager->update($project, $request->request->all());
return new JsonResponse($project);
}
}