-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.php
107 lines (77 loc) · 1.84 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
<?php
require 'Slim/Slim/Slim.php';
require 'mongo/crud.php';
require 'mongo/list.php';
require 'mongo/command.php';
define('MONGO_HOST', 'localhost');
$app = new Slim();
/**
* Routing
*/
$app->get( '/:db/:collection', '_list');
$app->post( '/:db/:collection', '_create');
$app->get( '/:db/:collection/:id', '_read');
$app->put( '/:db/:collection/:id', '_update');
$app->delete( '/:db/:collection/:id', '_delete');
// @todo: add count collection command mongo/commands.php
// List
function _list($db, $collection){
$select = array(
'limit' => (isset($_GET['limit'])) ? $_GET['limit'] : false,
'page' => (isset($_GET['page'])) ? $_GET['page'] : false,
'filter' => (isset($_GET['filter'])) ? $_GET['filter'] : false,
'regex' => (isset($_GET['regex'])) ? $_GET['regex'] : false,
'sort' => (isset($_GET['sort'])) ? $_GET['sort'] : false
);
$data = mongoList(
MONGO_HOST,
$db,
$collection,
$select
);
echo json_encode($data);
}
// Create
function _create($db, $collection){
$document = json_decode(Slim::getInstance()->request()->getBody(), true);
$data = mongoCreate(
MONGO_HOST,
$db,
$collection,
$document
);
echo json_encode($data);
}
// Read
function _read($db, $collection, $id){
$data = mongoRetrieve(
MONGO_HOST,
$db,
$collection,
$id
);
echo json_encode($data);
}
// Update
function _update($db, $collection, $id){
$document = json_decode(Slim::getInstance()->request()->getBody(), true);
$data = mongoUpdate(
MONGO_HOST,
$db,
$collection,
$id,
$document
);
echo json_encode($data);
}
// Delete
function _delete($db, $collection, $id){
$data = mongoDelete(
MONGO_HOST,
$db,
$collection,
$id
);
echo json_encode($data);
}
$app->run();