-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
137 lines (124 loc) · 3.19 KB
/
index.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
<?php
$loader = new Phalcon\Loader();
$loader->registerDirs(array(
__DIR__ . '/app/models'
))->register();
$di = new Phalcon\DI\FactoryDefault();
$di->set('db', function() {
return new Phalcon\Db\Adapter\Pdo\Mysql(array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'robotics'
));
});
$app = new Phalcon\Mvc\Micro($di);
$app->get('/api/robots', function() use ($app) {
$robots = Robots::find();
$data = array();
foreach($robots as $robot) {
$data[] = array(
'id' => $robot->id,
'name' => $robot->name,
);
}
echo json_encode($data);
});
$app->get('/api/robots/search/{name}', function($name) {
$robots = Robots::find(array("name = ?0",
"bind" => array($name)
));
$data = array();
foreach ($robots as $robot) {
$data[] = array(
'id' => $robot->id,
'name' => $robot->name,
);
}
echo json_encode($data);
});
$app->get('/api/robots/{id:[0-9]+}', function($id) {
$robot = Robots::findFirst(array(
"id = ?0",
"bind" => array($id)
));
$response = new Phalcon\Http\Response;
if (!$robot) {
$response->setJsonContent(array('status' => 'NOT-FOUND'));
} else {
$response->setJsonContent(array(
'status' => 'FOUND',
'data' => array(
'id' => $robot->id,
'name' => $robot->name
)
));
}
return $response;
});
$app->post('/api/robots', function() use($app) {
$robot = new Robots;
$robot->name = $app->request->getPost('name');
$robot->type = $app->request->getPost('type');
$robot->year = $app->request->getPost('year');
$response = new Phalcon\Http\Response();
if ($robot->save() == true) {
$response->setStatusCode(201, 'Created');
$robot->id = $robot->id;
$response->setJsonContent(array('status' => 'OK', 'data' => $robot->toArray()));
} else {
getErrors($response, $robot);
}
return $response;
});
$app->put('/api/robots/{id:[0-9]+}', function($id) use($app) {
$robot = Robots::findFirst(array(
"id = ?0",
"bind" => array($id)
));
$response = new Phalcon\Http\Response();
if (!$robot) {
$response->setJsonContent(array('status' => 'NOT-FOUND'));
} else {
$robot->name = $app->request->getPut('name');
$robot->type = $app->request->getPut('type');
$robot->year = $app->request->getPut('year');
if ($robot->save()) {
$response->setJsonContent(array('status' => 'OK'));
} else {
getErrors($response, $robot);
}
}
return $response;
});
$app->delete('/api/robots/{id:[0-9]+}', function($id) {
$robot = Robots::findFirst(array(
"id = ?0",
"bind" => array($id)
));
$response = new Phalcon\Http\Response();
if (!$robot) {
$response->setJsonContent(array('status' => 'NOT-FOUND'));
} else {
if ($robot->delete()) {
$response->setJsonContent(array('status' => 'OK'));
} else {
getErrors($response, $robot);
}
}
return $response;
});
$app->notFound(function() use ($app) {
$app->response->setStatusCode(404, "Not Found")->sendHeaders();
echo 'This is crazy, but this page was not found!';
});
$app->handle();
function getErrors($response, $model)
{
$response->setStatusCode(409, "Conflict");
$errors = array();
foreach ($model->getMessages() as $message) {
$errors[] = $message->getMessage();
}
$response->setJsonContent(array('status' => 'ERROR', 'message' => $errors));
}