-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.php
107 lines (87 loc) · 2.5 KB
/
services.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
<?php
/**
* Using Slim Framework - a microframework for web services
*/
require 'Slim/Slim.php';
require "NotORM.php";
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->error(function (\Exception $e) use ($app) {
echo $e->getMessage();
$app->stop();
});
try {
$dsn = 'mysql:host=localhost;dbname=snippet';
$username = 'root';
$password = 'root';
$pdo = new PDO($dsn, $username, $password);
$db = new NotORM($pdo);
}
catch(PDOException $e){
$app->error($e);
}
//$app->get("/userid/:userid(/query(/:query(/lastid(/:id))))"
$app->get("/userid/:userid/query(/:query)/lastid(/:lastid)", function ($userid="", $query="", $lastid="") use ($app, $db) {
$snippets = $db->snippets();
$snippets->order("id DESC");
$snippets->limit(5);
if(is_numeric($userid)){
$snippets->where("user_id = ?", "$userid");
}
else{
$app->error();
}
if($query!=""){
$snippets->where("content LIKE ?", "%$query%");
}
if(is_numeric($lastid)){
$snippets->where("id < ?", $lastid);
}
$res = array();
foreach ($snippets as $snipp) {
$res[$snipp["id"]] = array(
"id" => $snipp["id"],
"content" => $snipp["content"],
);
}
$app->response()->header("Content-Type", "application/json");
echo json_encode($res);
});
$app->post("/userid/:userid(/query/lastid)", function ($userid) use($app, $db) {
$req = $app->request();
$data = json_decode($req->getBody(), true);
$conteny = "";
if(!empty($data)){
$content = $data['content'];
}
$result = $db->snippets->insert(array('content'=>$content, 'user_id' =>$userid));
$app->response()->header("Content-Type", "application/json");
if($result){
$res = array();
$res[$result['id']] = array(
"id" => $result['id'],
"content" => $content
);
echo json_encode($res);
}
else{
$app->error();
}
});
$app->get("/snippetById/:id", function ($id) use ($app, $db) {
$app->response()->header("Content-Type", "application/json");
$snippet = $db->snippets()->where("id", $id);
if ($data = $snippet->fetch()) {
echo json_encode(array(
"id" => $data["id"],
));
}
else{
echo json_encode(array(
"status" => false,
"message" => "snippet ID $id does not exist"
));
}
});
// run the Slim app
$app->run();