-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
183 lines (142 loc) · 5.85 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
require 'vendor/autoload.php';
require 'config.php';
$app = new \Slim\App(['settings' => $config]);
$container = $app->getContainer();
// monolog
$container['logger'] = function($c) {
$logger = new \Monolog\Logger('my_logger');
$file_handler = new \Monolog\Handler\StreamHandler('../logs/app.log');
$logger->pushHandler($file_handler);
return $logger;
};
$app->get('/routine/familly/{name}', function ($request, $response, $args) {
session_start();
//$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$this->logger->addInfo($this->get('settings')['mongo']['connectionString']);
$manager = new MongoDB\Driver\Manager($this->get('settings')['mongo']['connectionString']);
$query = new MongoDB\Driver\Query(["name" => $args['name']], []);
$cursor = $manager->executeQuery('test.familly', $query);
$found = false;
foreach ($cursor as $document) {
$this->logger->addInfo('Boucle');
$found = true;
$_SESSION['idFamilly'] = 1;
break;
}
//$a = $cursor->toArray();
//$this->logger->addInfo(var_dump($a));
if ($found) {
return $response->withJson($document, 200, JSON_UNESCAPED_SLASHES);
}
return $response->withStatus(200);
});
$app->get('/routine/childs', function ($request, $response, $args) {
session_start();
$manager = new MongoDB\Driver\Manager($this->get('settings')['mongo']['connectionString']);
//$idFamilly = $_SESSION['idFamilly'];
$idFamilly = 1;
$query = new MongoDB\Driver\Query(["idfamilly" => $idFamilly], []);
$cursor = $manager->executeQuery('test.child', $query);
$found = false;
$childs = array();
foreach ($cursor as $document) {
$this->logger->addInfo('Boucle child - ' . $document->surname);
$found = true;
$queryLog = new MongoDB\Driver\Query(["idFamilly" => $idFamilly, "idChild" => $document->id], []);
$cursorLog = $manager->executeQuery('test.log', $queryLog);
$document->stars = 0;
$document->medals = 0;
foreach($cursorLog as $log) {
$this->logger->addInfo('Boucle log');
$this->logger->addInfo($log->event);
$this->logger->addInfo($log->star);
$document->stars += $log->star;
$document->medals += $log->medal;
}
$this->logger->addInfo('-------');
array_push($childs, $document);
}
//$a = $cursor->toArray();
//$this->logger->addInfo(var_dump($a));
if ($found) {
return $response->withJson($childs, 200, JSON_UNESCAPED_SLASHES);
}
return $response->withStatus(200);
});
$app->post('/routine/log/stepComplete', function ($request, $response, $args) {
$parsedBody = $request->getParsedBody();
$idFamilly = 1;
$idChild = $parsedBody["idChild"];
$stars = $parsedBody["stars"];
$medal = $parsedBody["medal"];
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert([
"id" => time(),
"idFamilly" => $idFamilly,
"idChild" => (int) $idChild,
"event" => "StepComplete",
"star" => (int) $stars,
"medal" => (int) $medal
]);
$manager = new MongoDB\Driver\Manager($this->get('settings')['mongo']['connectionString']);
$result = $manager->executeBulkWrite('test.log', $bulk);
return $response->withStatus(201);
});
$app->post('/routine/log/routineComplete', function ($request, $response, $args) {
$parsedBody = $request->getParsedBody();
$idFamilly = 1;
$idChild = $parsedBody["idChild"];
$stars = $parsedBody["stars"];
$medal = $parsedBody["medal"];
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert([
"id" => time(),
"idFamilly" => $idFamilly,
"idChild" => (int) $idChild,
"event" => "routineComplete",
"star" => (int) $stars,
"medal" => (int) $medal
]);
$manager = new MongoDB\Driver\Manager($this->get('settings')['mongo']['connectionString']);
$result = $manager->executeBulkWrite('test.log', $bulk);
return $response->withStatus(201);
});
$app->get('/routine/routines-list', function ($request, $response, $args) {
session_start();
$manager = new MongoDB\Driver\Manager($this->get('settings')['mongo']['connectionString']);
//$idFamilly = $_SESSION['idFamilly'];
$idFamilly = 1;
$query = new MongoDB\Driver\Query(["idfamilly" => $idFamilly], []);
$cursor = $manager->executeQuery('test.routine', $query);
$found = false;
$routines = array();
foreach ($cursor as $document) {
$this->logger->addInfo('Boucle');
$found = true;
array_push($routines, $document);
}
//$a = $cursor->toArray();
//$this->logger->addInfo(var_dump($a));
if ($found) {
return $response->withJson($routines, 200, JSON_UNESCAPED_SLASHES);
}
return $response->withStatus(200);
});
$app->options('/{routes:.+}', function ($request, $response, $args) {
return $response;
});
$app->add(function ($req, $res, $next) {
$response = $next($req, $res);
return $response
->withHeader('Access-Control-Allow-Origin', 'http://localhost:4200')
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
});
// Catch-all route to serve a 404 Not Found page if none of the routes match
// NOTE: make sure this route is defined last
$app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function($req, $res) {
$handler = $this->notFoundHandler; // handle using the default Slim page not found handler
return $handler($req, $res);
});
$app->run();