-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
executable file
·74 lines (60 loc) · 1.82 KB
/
api.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
<?php
$dsn = 'mysql:host=HOST;dbname=DATABASE;charset=UTF8';
$user = 'USER';
$password = 'PASSWORD';
try {
$db = new PDO($dsn, $user, $password, array(
PDO::ATTR_EMULATE_PREPARES => false
));
$collections = $db->prepare('SELECT * FROM collections WHERE handle = ?');
$blocks = $db->prepare('SELECT * FROM blocks WHERE collectionId = ?');
$tags = $db->prepare('
SELECT t.handle, t.title
FROM blockTags bt
JOIN tags t ON t.id = bt.tagId
WHERE bt.blockId = ?
');
$attributes = $db->prepare('
SELECT a.handle, a.title
FROM blockAttributes ba
JOIN attributes a ON a.id = ba.attributeId
WHERE ba.blockId = ?
');
$collections->execute([$_GET['handle']]);
if ($collection = $collections->fetch(PDO::FETCH_OBJ)) {
$response = [
'status' => 200,
'message' => 'OK'
];
$collection->blocks = [];
$collection->url = "#!/{$collection->handle}";
$blocks->execute([$collection->id]);
while ($block = $blocks->fetch(PDO::FETCH_OBJ)) {
$tags->execute([$block->id]);
$attributes->execute([$block->id]);
$block->time = date('c', strtotime($block->time));
$block->tags = $tags->fetchAll(PDO::FETCH_KEY_PAIR);
$block->attributes = $attributes->fetchAll(PDO::FETCH_KEY_PAIR);
$block->url = "#!/{$collection->handle}/{$block->handle}";
$collection->blocks[] = $block;
}
$response += (array)$collection;
} else {
throw new Exception('Not Found', 404);
}
} catch (PDOException $e) {
$response = [
'status' => 500,
'message' => 'Internal Server Error'
];
} catch (Exception $e) {
$response = [
'status' => $e->getCode(),
'message' => $e->getMessage()
];
}
header("HTTP/1.1 {$response['status']} {$response['message']}");
header('Cache-Control: no-cache');
header('Content-Type: application/json');
print json_encode($response);
?>