-
Notifications
You must be signed in to change notification settings - Fork 3
/
PeopleAPI.php
158 lines (144 loc) · 5.34 KB
/
PeopleAPI.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
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
<?php
require_once('PeopleDB.php');
require_once('DB.php'); //Consultas con PDO
class PeopleAPI
{
public function API()
{
header('Content-Type: application/JSON');
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET'://consulta
$this->getPeoples();
break;
case 'POST'://inserta
$this->savePeople();
break;
case 'PUT'://actualiza
$this->updatePeople();
break;
case 'DELETE'://elimina
$this->deletePeople();
break;
default://metodo NO soportado
echo 'METODO NO SOPORTADO';
break;
}
}
/**
* Respuesta al cliente
* @param int $code Codigo de respuesta HTTP
* @param String $status indica el estado de la respuesta puede ser "success" o "error"
* @param String $message Descripcion de lo ocurrido
*/
function response($code=200, $status="", $message="")
{
http_response_code($code);
if( !empty($status) && !empty($message) )
{
$response = array("status" => $status ,"message"=>$message);
echo json_encode($response,JSON_PRETTY_PRINT);
}
}
/**
* función que segun el valor de "action" e "id":
* - mostrara una array con todos los registros de personas
* - mostrara un solo registro
* - mostrara un array vacio
*/
function getPeoples()
{
if($_GET['action']=='peoples')
{
$db = new PeopleDB();
$dbPDO = new DB(); //Objeto de clase DB
if(isset($_GET['id']))
{
//muestra 1 solo registro si es que existiera ID
// $response = $db->getPeople($_GET['id']); //Consultas con mysqli
$response = $dbPDO->getUsuario($_GET['id']); //Consultas con PDO
echo json_encode($response,JSON_PRETTY_PRINT);
}else
{ //muestra todos los registros
$response = $db->getPeoples(); //consultas con mysqli
$response = $dbPDO->getAllUsuarios(); //Consultas con PDO
echo json_encode($response,JSON_PRETTY_PRINT);
}
}else{
$this->response(400);
}
}
/**
* metodo para guardar un nuevo registro de persona en la base de datos
*/
function savePeople()
{
if($_GET['action']=='peoples')
{
//Decodifica un string de JSON
$obj = json_decode( file_get_contents('php://input') );
$objArr = (array)$obj;
if (empty($objArr))
{
$this->response(422,"error","Nothing to add. Check json");
}
else if(isset($obj->name))
{
$people = new PeopleDB();
$people->insert( $obj->name );
$this->response(200,"success","new record added");
}
else{
$this->response(422,"error","The property is not defined");
}
} else{
$this->response(400);
}
}
/**
* Actualiza un recurso
*/
function updatePeople()
{
if( isset($_GET['action']) && isset($_GET['id']) )
{
if($_GET['action']=='peoples')
{
$obj = json_decode( file_get_contents('php://input') );
$objArr = (array)$obj;
if (empty($objArr))
{
$this->response(422,"error","Nothing to add. Check json");
}else if(isset($obj->name))
{
$db = new PeopleDB();
$db->update($_GET['id'], $obj->name);
$this->response(200,"success","Record updated");
}else
{
$this->response(422,"error","The property is not defined");
}
exit;
}
}
$this->response(400);
}
/**
* elimina persona
*/
function deletePeople()
{
if( isset($_GET['action']) && isset($_GET['id']) )
{
if($_GET['action']=='peoples')
{
$db = new PeopleDB();
$db->delete($_GET['id']);
$this->response(204);
exit;
}
}
$this->response(400);
}
}//end class
?>