1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpDraft\Controllers\Commish; |
4
|
|
|
|
5
|
|
|
use \Silex\Application; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use PhpDraft\Domain\Entities\Manager; |
9
|
|
|
use PhpDraft\Domain\Models\PhpDraftResponse; |
10
|
|
|
|
11
|
|
|
class ManagerController { |
12
|
|
|
public function Get(Application $app, Request $request) { |
13
|
|
|
$draftId = (int)$request->get('draft_id'); |
14
|
|
|
|
15
|
|
|
$managers = $app['phpdraft.ManagerRepository']->GetManagersByDraftOrder($draftId); |
16
|
|
|
|
17
|
|
|
return $app->json($managers, Response::HTTP_OK); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function Create(Application $app, Request $request) { |
21
|
|
|
$draftId = (int)$request->get('draft_id'); |
22
|
|
|
|
23
|
|
|
$manager = new Manager(); |
24
|
|
|
$manager->draft_id = $draftId; |
25
|
|
|
$manager->manager_name = $request->get('manager_name'); |
26
|
|
|
|
27
|
|
|
$validity = $app['phpdraft.ManagerValidator']->IsManagerValidForCreate($draftId, $manager); |
28
|
|
|
|
29
|
|
|
if(!$validity->success) { |
30
|
|
|
return $app->json($validity, Response::HTTP_BAD_REQUEST); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$response = $app['phpdraft.ManagerService']->CreateNewManager($manager); |
34
|
|
|
|
35
|
|
|
return $app->json($response, $response->responseType(Response::HTTP_CREATED)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function CreateMany(Application $app, Request $request) { |
39
|
|
|
$draftId = (int)$request->get('draft_id'); |
40
|
|
|
|
41
|
|
|
$managersJson = $request->get('managers'); |
42
|
|
|
$newManagers = array(); |
43
|
|
|
|
44
|
|
|
foreach($managersJson as $managerRequest) { |
45
|
|
|
$newManager = new Manager(); |
46
|
|
|
$newManager->draft_id = $draftId; |
47
|
|
|
$newManager->manager_name = $managerRequest['manager_name']; |
48
|
|
|
|
49
|
|
|
$newManagers[] = $newManager; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$validity = $app['phpdraft.ManagerValidator']->AreManagersValidForCreate($draftId, $newManagers); |
53
|
|
|
|
54
|
|
|
if(!$validity->success) { |
55
|
|
|
return $app->json($validity, Response::HTTP_BAD_REQUEST); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$response = $app['phpdraft.ManagerService']->CreateManyManagers($draftId, $newManagers); |
59
|
|
|
|
60
|
|
|
return $app->json($response, $response->responseType(Response::HTTP_CREATED)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function Reorder(Application $app, Request $request) { |
64
|
|
|
$draftId = (int)$request->get('draft_id'); |
65
|
|
|
|
66
|
|
|
$managersIdJson = $request->get('ordered_manager_ids'); |
67
|
|
|
$managerIds = array(); |
68
|
|
|
|
69
|
|
|
foreach($managersIdJson as $managerId) { |
70
|
|
|
$managerIds[] = (int)$managerId; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$validity = $app['phpdraft.ManagerValidator']->AreManagerIdsValidForOrdering($draftId, $managerIds); |
74
|
|
|
|
75
|
|
|
if(!$validity->success) { |
76
|
|
|
return $app->json($validity, Response::HTTP_BAD_REQUEST); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$response = $app['phpdraft.ManagerService']->ReorderManagers($managerIds); |
80
|
|
|
|
81
|
|
|
return $app->json($response, $response->responseType()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function Update(Application $app, Request $request) { |
85
|
|
|
$draftId = (int)$request->get('draft_id'); |
86
|
|
|
$managerId = $request->get('manager_id'); |
87
|
|
|
|
88
|
|
|
try { |
89
|
|
|
$draft = $app['phpdraft.DraftRepository']->Load($draftId); |
90
|
|
|
$manager = $app['phpdraft.ManagerRepository']->Load($managerId); |
91
|
|
|
} catch(\Exception $e) { |
92
|
|
|
$response = new PhpDraftResponse(false, array()); |
93
|
|
|
$response->errors[] = "Unable to load manager #$managerId"; |
94
|
|
|
|
95
|
|
|
return $app->json($response, Response::HTTP_BAD_REQUEST); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$manager->manager_name = $request->get('name'); |
99
|
|
|
|
100
|
|
|
$validity = $app['phpdraft.ManagerValidator']->IsManagerValidForUpdate($draft, $manager); |
101
|
|
|
|
102
|
|
|
if(!$validity->success) { |
103
|
|
|
return $app->json($validity, Response::HTTP_BAD_REQUEST); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$response = $app['phpdraft.ManagerService']->UpdateManager($manager); |
107
|
|
|
|
108
|
|
|
return $app->json($response, $response->responseType()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function Delete(Application $app, Request $request) { |
112
|
|
|
$draftId = (int)$request->get('draft_id'); |
113
|
|
|
$managerId = $request->get('manager_id'); |
114
|
|
|
|
115
|
|
|
try { |
116
|
|
|
$manager = $app['phpdraft.ManagerRepository']->Load($managerId); |
117
|
|
|
} catch(\Exception $e) { |
118
|
|
|
$response = new PhpDraftResponse(false, array()); |
119
|
|
|
$response->errors[] = "Unable to delete manager #$managerId"; |
120
|
|
|
|
121
|
|
|
return $app->json($response, Response::HTTP_BAD_REQUEST); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if($manager->draft_id != $draftId) { |
125
|
|
|
$response = new PhpDraftResponse(false, array()); |
126
|
|
|
$response->errors[] = "Unable to delete manager #$managerId"; |
127
|
|
|
|
128
|
|
|
return $app->json($response, Response::HTTP_BAD_REQUEST); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$response = $app['phpdraft.ManagerService']->DeleteManager($manager); |
132
|
|
|
|
133
|
|
|
return $app->json($response, $response->responseType()); |
134
|
|
|
} |
135
|
|
|
} |