/
ProassistController.php
115 lines (88 loc) · 3.49 KB
/
ProassistController.php
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
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Proassist;
#[Route('/api', name: 'api_')]
class ProassistController extends AbstractController
{
#[Route('/items', name: 'items_index', methods:['get'] )]
public function index(ManagerRegistry $doctrine): JsonResponse
{
$products = $doctrine
->getRepository(Proassist::class)
->findAll();
$data = [];
foreach ($products as $product) {
$data[] = [
'id' => $product->getId(),
'name' => $product->getName(),
'description' => $product->getDescription(),
];
}
return $this->json($data);
}
#[Route('/items', name: 'items_create', methods:['post'] )]
public function create(ManagerRegistry $doctrine, Request $request): JsonResponse
{
$entityManager = $doctrine->getManager();
$project = new Proassist();
$project->setName($request->request->get('name'));
$project->setDescription($request->request->get('description'));
$entityManager->persist($project);
$entityManager->flush();
$data = [
'id' => $project->getId(),
'name' => $project->getName(),
'description' => $project->getDescription(),
];
return $this->json($data);
}
#[Route('/items/{id}', name: 'items_show', methods:['get'] )]
public function show(ManagerRegistry $doctrine, int $id): JsonResponse
{
$project = $doctrine->getRepository(Proassist::class)->find($id);
if (!$project) {
return $this->json('No project found for id ' . $id, 404);
}
$data = [
'id' => $project->getId(),
'name' => $project->getName(),
'description' => $project->getDescription(),
];
return $this->json($data);
}
#[Route('/items/{id}', name: 'items_update', methods:['put', 'patch'] )]
public function update(ManagerRegistry $doctrine, Request $request, int $id): JsonResponse
{
$entityManager = $doctrine->getManager();
$project = $entityManager->getRepository(Proassist::class)->find($id);
if (!$project) {
return $this->json('No project found for id' . $id, 404);
}
$project->setName($request->request->get('name'));
$project->setDescription($request->request->get('description'));
$entityManager->flush();
$data = [
'id' => $project->getId(),
'name' => $project->getName(),
'description' => $project->getDescription(),
];
return $this->json($data);
}
#[Route('/items/{id}', name: 'items_delete', methods:['delete'] )]
public function delete(ManagerRegistry $doctrine, int $id): JsonResponse
{
$entityManager = $doctrine->getManager();
$project = $entityManager->getRepository(Proassist::class)->find($id);
if (!$project) {
return $this->json('No project found for id' . $id, 404);
}
$entityManager->remove($project);
$entityManager->flush();
return $this->json('Deleted a project successfully with id ' . $id);
}
}