forked from Elyabe/dev.api.ppcchoice.ufes.br
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDisciplinaController.php
More file actions
executable file
·250 lines (220 loc) · 9.23 KB
/
DisciplinaController.php
File metadata and controls
executable file
·250 lines (220 loc) · 9.23 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php defined('BASEPATH') or exit('No direct script access allowed');
require_once APPPATH . 'libraries/APIController.php';
class DisciplinaController extends APIController
{
public function __construct()
{
parent::__construct();
}
/**
* @api {get} disciplinas Listar dados de todas as disciplinas
* @apiName findAll
* @apiGroup Disciplina
*
* @apiSuccess {Disciplina[]} disciplina Array de objetos do tipo Disciplina.
* @apiSuccess {Number} disciplina[numDisciplina] Identificador único da disciplina.
* @apiSuccess {String} disciplina[nome] Nome da disciplina.
* @apiSuccess {Number} disciplina[ch] Carga horária da disciplina.
* @apiSuccess {Number} disciplina[codDepto] Código do departamento cujo qual a disciplina está vinculada.
* @apiSuccess {String} disciplina[abreviaturaDepto] Abreviatura do departamento cujo qual a disciplina está vinculada.
*
* @apiError {String[]} error Entities\\Disciplina: Instância não encontrada.
*/
public function findAll()
{
header("Access-Control-Allow-Origin: *");
$this->_apiconfig(array(
'methods' => array('GET'),
));
$colecaoDisciplina = $this->entityManager->getRepository('Entities\Disciplina')->findAll();
if (!is_null($colecaoDisciplina)) {
$this->apiReturn(
$colecaoDisciplina,
self::HTTP_OK
);
} else {
$this->apiReturn(array(
'error' => $this->getApiMessage(STD_MSG_NOT_FOUND),
), self::HTTP_NOT_FOUND);
}
}
/**
* @api {get} disciplinas/:codDepto/:numDisciplina Listar dados de uma disciplina
* @apiName findById
* @apiGroup Disciplina
*
* @apiParam {Number} numDisciplina Identificador único da disciplina.
* @apiParam {Number} codDepto Código do departamento cujo qual a disciplina está vinculada.
*
* @apiSuccess {String} nome Nome da disciplina.
* @apiSuccess {Number} ch Carga horária da disciplina.
* @apiSuccess {String} nomeDepto Nome do departamento cujo qual a disciplina está vinculada.
*
* @apiError {String[]} error Entities\\Disciplina: Instância não encontrada.
*/
public function findById($codDepto, $numDisciplina)
{
header("Access-Control-Allow-Origin: *");
$this->_apiconfig(array(
'methods' => array('GET'),
));
$disciplina = $this->entityManager->getRepository('Entities\Disciplina')->findById($numDisciplina, $codDepto);
if (!is_null($disciplina)) {
$this->apiReturn(
$disciplina,
self::HTTP_OK
);
} else {
$this->apiReturn(array(
'error' => $this->getApiMessage(STD_MSG_NOT_FOUND),
), self::HTTP_NOT_FOUND);
}
}
/**
* @api {post} disciplinas Criar uma disciplina
* @apiName create
* @apiGroup Disciplina
*
* @apiParam (Request Body/JSON) {Number} numDisciplina Identificador primário da disciplina.
* @apiParam (Request Body/JSON) {String} nome Nome da disciplina.
* @apiParam (Request Body/JSON) {Number} ch Carga horária da disciplina.
* @apiParam (Request Body/JSON) {Number} codDepto Identificador secundário da disciplina (identificador primário do departamento que ela está vinculada).
*
* @apiSuccess {String[]} message Entities\\Disciplina: Instância criada com sucesso.
*
* @apiError {String[]} error Campo obrigatório não informado ou contém valor inválido.
*/
public function create()
{
header("Access-Control-Allow-Origin: *");
$this->_apiconfig(array(
'methods' => array('POST'),
'requireAuthorization' => TRUE,
));
$payload = $this->getBodyRequest();
$disciplina = new Entities\Disciplina();
if (array_key_exists('numDisciplina', $payload)) $disciplina->setNumDisciplina($payload['numDisciplina']);
if (array_key_exists('ch', $payload)) $disciplina->setCh($payload['ch']);
if (array_key_exists('nome', $payload)) $disciplina->setNome($payload['nome']);
if (isset($payload['codDepto'])) {
$depto = $this->entityManager->find('Entities\Departamento', $payload['codDepto']);
if (!is_null($depto)) {
$disciplina->setDepartamento($depto);
$disciplina->setCodDepto($payload['codDepto']);
}
}
$constraints = $this->validator->validate($disciplina);
if ($constraints->success()) {
try {
$this->entityManager->persist($disciplina);
$this->entityManager->flush();
$this->apiReturn(array(
'message' => $this->getApiMessage(STD_MSG_CREATED),
), self::HTTP_OK);
} catch (\Exception $e) {
$this->apiReturn(array(
'error' => $this->getApiMessage(STD_MSG_EXCEPTION),
), self::HTTP_BAD_REQUEST);
}
} else {
$this->apiReturn(array(
'error' => $constraints->messageArray(),
), self::HTTP_BAD_REQUEST);
}
}
/**
* @api {put} disciplinas/:codDepto/:numDisciplina Atualizar dados de uma disciplina
* @apiName update
* @apiGroup Disciplina
*
* @apiParam {Number} numDisciplina Identificador único de uma disciplina.
* @apiParam {Number} codDepto Código do departamento cujo qual a disciplina está vinculada.
* @apiParam (Request Body/JSON) {String} [nome] Nome da disciplina.
* @apiParam (Request Body/JSON) {Number} [ch] Carga horária da disciplina.
*
* @apiSuccess {String[]} message Entities\\Disciplina: Instância atualizada com sucesso.
*
* @apiError {String[]} error Entities\\Disciplina: Instância não encontrada.
* @apiError {String[]} error Campo obrigatório não informado ou contém valor inválido.
*/
public function update($codDepto, $numDisciplina)
{
header("Access-Control-Allow-Origin: *");
$this->_apiconfig(array(
'methods' => array('PUT'),
'requireAuthorization' => TRUE,
));
$disciplina = $this->entityManager->find(
'Entities\Disciplina',
array('codDepto' => $codDepto, 'numDisciplina' => $numDisciplina)
);
$payload = $this->getBodyRequest();
if (!is_null($disciplina)) {
if (array_key_exists('nome', $payload)) $disciplina->setNome($payload['nome']);
if (array_key_exists('ch', $payload)) $disciplina->setCh($payload['ch']);
$constraints = $this->validator->validate($disciplina);
if ($constraints->success()) {
try {
$this->entityManager->merge($disciplina);
$this->entityManager->flush();
$this->apiReturn(array(
'message' => $this->getApiMessage(STD_MSG_UPDATED),
), self::HTTP_OK);
} catch (\Exception $e) {
$this->apiReturn(array(
'error' => $this->getApiMessage(STD_MSG_EXCEPTION),
), self::HTTP_BAD_REQUEST);
}
} else {
$this->apiReturn(array(
'error' => $constraints->messageArray(),
), self::HTTP_BAD_REQUEST);
}
} else {
$this->apiReturn(array(
'error' => $this->getApiMessage(STD_MSG_NOT_FOUND),
), self::HTTP_NOT_FOUND);
}
}
/**
* @api {delete} disciplinas/:codDepto/:numDisciplina Excluir uma disciplina
* @apiName delete
* @apiGroup Disciplina
*
* @apiParam {Number} numDisciplina Identificador único da disciplina.
* @apiParam {Number} codDepto Código do departamento cujo qual a disciplina está vinculada.
*
* @apiSuccess {String[]} message Entities\\Disciplina: Instância removida com sucesso.
*
* @apiError {String[]} error Entities\\Disciplina: Instância não encontrada.
*/
public function delete($codDepto, $numDisciplina)
{
header("Access-Control-Allow-Origin: *");
$this->_apiconfig(array(
'methods' => array('DELETE'),
'requireAuthorization' => TRUE,
));
$disciplina = $this->entityManager->find(
'Entities\Disciplina',
array('codDepto' => $codDepto, 'numDisciplina' => $numDisciplina)
);
if (!is_null($disciplina)) {
try {
$this->entityManager->remove($disciplina);
$this->entityManager->flush();
$this->apiReturn(array(
'message' => $this->getApiMessage(STD_MSG_DELETED),
), self::HTTP_OK);
} catch (\Exception $e) {
$this->apiReturn(array(
'error' => $this->getApiMessage(STD_MSG_EXCEPTION),
), self::HTTP_BAD_REQUEST);
}
} else {
$this->apiReturn(array(
'error' => $this->getApiMessage(STD_MSG_NOT_FOUND),
), self::HTTP_NOT_FOUND);
}
}
}