-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanageItems.php
116 lines (89 loc) · 3 KB
/
manageItems.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
<?php
require_once 'dbConfig.php';
require_once 'Item.cls.php';
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET");
header("Access-Control-Allow-Headers: Content-Type");
try{
$connection = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$eData = file_get_contents("php://input");
$dData = json_decode($eData, true);
$operation = $dData['operation'];
function createItem(){
global $connection;
global $dData;
$name = $dData['name'];
$quantity = $dData['quantity'];
$price = $dData['price'];
$list_id = $dData['list_id'];
$item_cat_id = $dData['item_cat_id'];
if ($name != "" and $quantity != "" and $price != "" and $list_id != "" and $item_cat_id != "") {
$l1 = new Item(null, $name, $quantity, $price, $list_id, $item_cat_id, 0);
$isInserted = $l1->create($connection);
if ($isInserted){
echo json_encode("The item has been inserted successfully");
} else {
echo json_encode("An error occurred, try again!");
}
} else {
echo json_encode("Please fill in all fields!");
}
}
function deleteItem(){
global $connection;
global $dData;
$itemId = $dData['itemId'];
$li = new Item();
$li->setId($itemId);
$isDeleted = $li->delete($connection);
if ($isDeleted){
echo json_encode("The item has been deleted successfully");
} else {
echo json_encode("An error occurred, try again!");
}
}
function updateItem(){
global $connection;
global $dData;
$itemId = $dData['itemId'];
$field = $dData['field'];
$value = $dData['value'];
$isUpdated = Item::update($connection, $itemId, $field, $value);
if ($isUpdated) {
echo json_encode("Item updated!");
}
else {
echo json_encode("An error occurred, try again!");
}
}
function getItems(){
global $connection;
global $dData;
$list_id = $dData['list_id'];
if ($list_id !== ''){
$l1 = new Item();
$l1->setList_id($list_id);
$listOfItems = $l1->getAllItems($connection);
echo json_encode($listOfItems);
} else {
echo json_encode("An error occurred, try again!");
}
}
switch ($operation){
case 'create':
createItem();
break;
case 'delete':
deleteItem();
break;
case 'update':
updateItem();
break;
case 'select':
getItems();
break;
}
} catch (PDOException $e){
$err = $connection->errorInfo();
echo json_encode($err);
}