-
Notifications
You must be signed in to change notification settings - Fork 0
/
gironiBet.php
155 lines (126 loc) · 5.55 KB
/
gironiBet.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
<?php
require_once __DIR__ . '/controllers/ErrorHandler.php';
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: access');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Content-Type: application/json; charset=UTF-8');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
require_once __DIR__ . '/database.php';
require_once __DIR__ . '/sendJson.php';
require_once __DIR__ . '/controllers/authHandler.php';
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') :
sendJson(200, 'OK');
endif;
//operazioni admin
$authHandler = new AuthHandler();
if ($_SERVER['REQUEST_METHOD'] == 'GET') :
$girone = null;
$loggedUserId = $authHandler->getLoggedUserId();
if(isset($_GET['girone'])){
//singola scommessa girone
$girone = $_GET['girone'];
$sql = "SELECT * FROM `gironi_bet` WHERE `girone`='$girone' AND `user_id`='$loggedUserId'";
$query = mysqli_query($connection, $sql);
$row = mysqli_fetch_array($query, MYSQLI_ASSOC);
if ($row === null) sendJson(404, 'Girone bet not found!');
sendJson(200, '', $row);
}else{
//lista scommesse partite
$sql = "SELECT * FROM `gironi_bet` WHERE `user_id`='$loggedUserId'";
$query = mysqli_query($connection, $sql);
$teamsDict = $query->fetch_all(MYSQLI_ASSOC);
$response = ["gironi_bet" => $teamsDict];
sendJson(200, '', $response);
}
endif;
if ($_SERVER['REQUEST_METHOD'] == 'POST') :
$data = json_decode(file_get_contents('php://input'));
if (
!isset($data->user_id) ||
!isset($data->girone) ||
!isset($data->pos_1) ||
!isset($data->pos_2) ||
!isset($data->pos_3) ||
!isset($data->pos_4) ||
is_null(trim($data->user_id)) ||
is_null(trim($data->girone)) ||
is_null(trim($data->pos_1)) ||
is_null(trim($data->pos_2)) ||
is_null(trim($data->pos_3)) ||
is_null(trim($data->pos_4))
) :
sendJson(
422,
'Please fill all the required fields & None of the fields should be empty.',
['required_fields' => ['user_id', 'girone', 'pos_1', 'pos_2', 'pos_3', 'pos_4']]
);
endif;
$user_id = mysqli_real_escape_string($connection, htmlspecialchars(trim($data->user_id)));
$girone = mysqli_real_escape_string($connection, trim($data->girone));
$pos_1 = mysqli_real_escape_string($connection, trim($data->pos_1));
$pos_2 = mysqli_real_escape_string($connection, trim($data->pos_2));
$pos_3 = mysqli_real_escape_string($connection, trim($data->pos_3));
$pos_4 = mysqli_real_escape_string($connection, trim($data->pos_4));
$sql = "INSERT INTO `gironi_bet` (`user_id`,`girone`,`pos_1`,`pos_2`,`pos_3`,`pos_4`) VALUES($user_id,'$girone',$pos_1,$pos_2,$pos_3,$pos_4)";
$query = mysqli_query($connection, $sql);
if ($query) sendJson(200, 'You have successfully registered a girone bet.');
sendJson(500, 'Something going wrong.');
endif;
if ($_SERVER['REQUEST_METHOD'] == 'PUT') :
$data = json_decode(file_get_contents('php://input'));
if (
!isset($data->id) ||
!isset($data->user_id) ||
!isset($data->girone) ||
!isset($data->pos_1) ||
!isset($data->pos_2) ||
!isset($data->pos_3) ||
!isset($data->pos_4) ||
is_null(trim($data->user_id)) ||
is_null(trim($data->girone)) ||
is_null(trim($data->pos_1)) ||
is_null(trim($data->pos_2)) ||
is_null(trim($data->pos_3)) ||
is_null(trim($data->pos_4))
) :
sendJson(
422,
'Please fill all the required fields & None of the fields should be empty.',
['required_fields' => ['user_id', 'girone', 'pos_1', 'pos_2', 'pos_3', 'pos_4']]
);
endif;
$id = $data->id;
$user_id = mysqli_real_escape_string($connection, htmlspecialchars(trim($data->user_id)));
$girone = mysqli_real_escape_string($connection, trim($data->girone));
$pos_1 = mysqli_real_escape_string($connection, trim($data->pos_1));
$pos_2 = mysqli_real_escape_string($connection, trim($data->pos_2));
$pos_3 = mysqli_real_escape_string($connection, trim($data->pos_3));
$pos_4 = mysqli_real_escape_string($connection, trim($data->pos_4));
$sql = "SELECT `id` FROM `gironi_bet` WHERE `id`= $id";
$query = mysqli_query($connection, $sql);
$row_num = mysqli_num_rows($query);
if ($row_num == 0) sendJson(404, 'This Girone bet doesn\'t exists!');
$sql = "UPDATE `gironi_bet` SET `user_id`='$user_id',`girone`='$girone',`pos_1`=$pos_1,`pos_2`=$pos_2,`pos_3`='$pos_3',`pos_4`='$pos_4' WHERE `id` = $id";
$query = mysqli_query($connection, $sql);
if ($query) sendJson(200, 'You have successfully updated a girone bet.');
sendJson(500, 'Something going wrong.');
endif;
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') :
$data = json_decode(file_get_contents('php://input'));
if (
!isset($data->id) ||
empty(trim($data->id))
) :
sendJson(
422,
'Please fill all the required fields & None of the fields should be empty.',
['required_fields' => ['id']]
);
endif;
$id = $data->id;
$sql = "DELETE FROM `gironi_bet` WHERE `id` = $id";
$query = mysqli_query($connection, $sql);
if ($query) sendJson(200, 'You have successfully deleted a girone bet.');
sendJson(500, 'Something going wrong.');
endif;
sendJson(405, 'Invalid Request Method. HTTP method should be POST');