-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudents_controller.php
48 lines (45 loc) · 1.65 KB
/
students_controller.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
<?php
header("Content-Type: application/json");
/**
* students_controller.php
* Implementa los servicios para realizar altas, bajas, modificaciones y lectura
* de datos de estudiantes
*/
require 'student_dao.php';
$requestMethod = $_SERVER['REQUEST_METHOD'];
$studentDAO = new StudentDAO();
switch ($requestMethod) {
case 'GET':
if (empty($_GET['id'])) {
echo json_encode($studentDAO->findStudents());
} else {
echo json_encode($studentDAO->findStudentById($_GET['id']));
}
break;
case 'POST':
$jsonStudent = json_decode(file_get_contents("php://input"), true);
$student = new Student(
$jsonStudent['firstName'],
$jsonStudent['lastName'],
$jsonStudent['city'],
$jsonStudent['semester']
);
echo json_encode(['result' => $studentDAO->save($student)]);
break;
case 'PUT':
$jsonStudent = json_decode(file_get_contents("php://input"), true);
$student = new Student(
$jsonStudent['firstName'],
$jsonStudent['lastName'],
$jsonStudent['city'],
$jsonStudent['semester'],
$jsonStudent['id']
);
echo json_encode(['result' => $studentDAO->update($student)]);
break;
case 'DELETE':
$query = $_SERVER['QUERY_STRING'];
list($key, $value) = explode('=', $query);
echo json_encode(['result' => $studentDAO->delete($value)]);
break;
}