forked from pedrofaria09/LTW_Projeto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestaurant_actions.php
176 lines (137 loc) · 4.76 KB
/
restaurant_actions.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
<?php
session_start();
include_once('sql/connection.php');
include_once('sql/security.php');
include_once('sql/restaurant.php');
function generateResponse($msg, $request) {
return array('msg' => $msg, 'request' => $request);
}
function actionInsert($obj) {
$obj->name = strip_tags(trim($obj->name));
$obj->location = strip_tags(trim($obj->location));
$obj->description = strip_tags(trim($obj->description));
$obj->cuisine_type = strip_tags(trim($obj->cuisine_type));
$obj->price_range = strip_tags(trim($obj->price_range));
//for integers
if (empty($obj->user_id)) {
return generateResponse("Invalid user!", -1);
}
if (empty($obj->name)) {
return generateResponse("You didn't enter the restaurant name!", -1);
}
if (empty($obj->location)) {
return generateResponse("You didn't enter the restaurant location!", -1);
}
if (empty($obj->description)) {
return generateResponse("You didn't enter a description!", -1);
}
if (empty($obj->cuisine_type)) {
return generateResponse("You didn't enter a cuisine type!", -1);
}
if (!checkInteger($obj->price_range, true)) {
return generateResponse("You didn't enter a valid price range!", -1);
}
if (empty($obj->opening_time)) {
return generateResponse("You didn't enter a opening time!", -1);
}
if (empty($obj->closing_time)) {
return generateResponse("You didn't enter a closing time!", -1);
}
$restaurantId = insertRestaurant($obj->name, $obj->location, $obj->description, $obj->cuisine_type, $obj->opening_time, $obj->closing_time, $obj->price_range, $obj->user_id, $obj->urlPath);
if ($restaurantId < 0) {
return generateResponse('This restaurant already exists!', -1);
}
// use $restaurantId to redirect to the newly created restaurant page
return generateResponse('Added restaurant with success!', $restaurantId);
}
function actionDelete($obj) {
if (restaurantExists($obj->restaurant_id) === false) {
return generateResponse('This restaurant does not exist!', false);
}
if (deleteRestaurant($obj->restaurant_id, $obj->user_id) === false) {
return generateResponse('Could not delete selected restaurant, database error?', false);
}
return generateResponse('Restaurant successfully deleted!', true);
}
function actionUpdate($obj) {
if (restaurantExists($obj->restaurant_id) === false) {
return generateResponse('This restaurant does not exist!', false);
}
if (empty($obj->user_id)) {
return generateResponse("Invalid user!", -1);
}
$obj->name = strip_tags(trim($obj->name));
$obj->location = strip_tags(trim($obj->location));
$obj->description = strip_tags(trim($obj->description));
$obj->cuisine_type = strip_tags(trim($obj->cuisine_type));
$obj->price_range = strip_tags(trim($obj->price_range));
if (empty($obj->name)) {
return generateResponse("You didn't enter the restaurant name!", -1);
}
if (empty($obj->location)) {
return generateResponse("You didn't enter the restaurant location!", -1);
}
if (empty($obj->description)) {
return generateResponse("You didn't enter a description!", -1);
}
if (empty($obj->cuisine_type)) {
return generateResponse("You didn't enter a cuisine type!", -1);
}
if (!checkInteger($obj->price_range, true)) {
return generateResponse("You didn't enter a valid price range!", -1);
}
if (empty($obj->opening_time)) {
return generateResponse("You didn't enter a opening time!", -1);
}
if (empty($obj->closing_time)) {
return generateResponse("You didn't enter a closing time!", -1);
}
if (updateRestaurant(
$obj->restaurant_id,
$obj->user_id,
$obj->name,
$obj->location,
$obj->description,
$obj->cuisine_type,
$obj->opening_time,
$obj->closing_time,
$obj->price_range,
$obj->urlPath) === false) {
return generateResponse('Could not update selected restaurant, database error?', false);
};
return generateResponse('Restaurant information successfully updated!', true);
}
function allRestaurantsLocation($obj){
$result = [];
$variable = getRestaurantIdByLocation($obj->restaurantLocation);
foreach ($variable as $key) {
array_push($result, $key['restaurant_id']);
}
return $result;
}
$data = file_get_contents('php://input');
if (isset($data)) {
$obj = json_decode($data);
switch($obj->type) {
case 'insert':
$result = actionInsert($obj);
break;
case 'delete':
$result = actionDelete($obj);
break;
case 'update':
$result = actionUpdate($obj);
break;
case 'allRestaurants':
$result = listRestaurants();
break;
case 'search_restaurant':
$result = getRestaurantIdByName($obj->restaurantName)['restaurant_id'];
break;
case 'search_restaurant_location':
$result = allRestaurantsLocation($obj);
break;
}
}
echo json_encode($result);
?>