-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
134 lines (121 loc) · 3.02 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
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
<?php
/*
* Actions: add and remove symbol
*/
// SQL server connection information
$sql_details = array(
'user' => 'root',
'pass' => '',
'db' => 'my_app',
'host' => 'localhost'
);
// to store errors
$errors = [];
// pass back the data to `api.php`
$form_data = [];
if (!empty($_POST['action']) && $_POST['action'] == 'add')
{
// validate the form on the server side
// `symbol` cannot be empty
if (empty($_POST['symbol']))
{
$form_data['success'] = false;
$form_data['errors'] = 'Symbol name cannot be blank';
echo json_encode($form_data);
return;
}
try {
$db = @new PDO(
"mysql:host={$sql_details['host']};dbname={$sql_details['db']}",
$sql_details['user'],
$sql_details['pass'],
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
}
catch (PDOException $e) {
$form_data['success'] = false;
$form_data['errors'] = 'Could not connect to the DB';
echo json_encode($form_data);
return;
}
// Check if it exists
$stmt = $db->query("SELECT symbol, active FROM quotes WHERE symbol = '" . $_POST['symbol'] . "'");
try {
$stmt->execute();
}
catch (PDOException $e) {
$form_data['success'] = false;
$form_data['errors'] = 'DB error: Invalid query';
echo json_encode($form_data);
return;
}
$result = $stmt->fetchAll();
if (empty($result[0][0]))
{
$form_data['success'] = false;
$form_data['errors'] = 'The given symbol does not exist';
echo json_encode($form_data);
return;
}
else if ($result[0][1] == 1)
{
$form_data['success'] = false;
$form_data['errors'] = 'This symbol has already been added to the watchlist';
echo json_encode($form_data);
return;
}
$stmt = $db->prepare("UPDATE quotes SET active = 1 WHERE symbol = '" . $_POST['symbol'] . "'");
// Execute
try {
$stmt->execute();
}
catch (PDOException $e) {
$form_data['success'] = false;
$form_data['errors'] = 'DB error: Invalid query';
echo json_encode($form_data);
return;
}
$form_data['success'] = true;
$form_data['errors'] = 'The symbol is successfully added';
echo json_encode($form_data);
return;
}
else if (!empty($_POST['action']) && $_POST['action'] == 'remove')
{
try {
$db = @new PDO(
"mysql:host={$sql_details['host']};dbname={$sql_details['db']}",
$sql_details['user'],
$sql_details['pass'],
array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )
);
}
catch (PDOException $e) {
$form_data['success'] = false;
$form_data['errors'] = 'Could not connect to the DB';
echo json_encode($form_data);
return;
}
$stmt = $db->prepare("UPDATE quotes SET active = 0 WHERE symbol = '" . $_POST['symbol'] . "'");
// Execute
try {
$stmt->execute();
}
catch (PDOException $e) {
$form_data['success'] = false;
$form_data['errors'] = 'DB error: Invalid query';
echo json_encode($form_data);
return;
}
$form_data['success'] = true;
$form_data['errors'] = 'The symbol is successfully removed';
echo json_encode($form_data);
return;
}
else
{
$form_data['success'] = false;
$form_data['errors'] = 'Error: there are no such action';
echo json_encode($form_data);
return;
}