Skip to content

Commit 424976f

Browse files
authored
Merge pull request #1 from rjmead23/Category/adding-remaining-CRUD
Adding remaining CRUD operations for Category model
2 parents acc9a25 + 20ec356 commit 424976f

File tree

5 files changed

+239
-2
lines changed

5 files changed

+239
-2
lines changed

api/category/create.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
// Headers
3+
header('Access-Control-Allow-Origin: *');
4+
header('Content-Type: application/json');
5+
header('Access-Control-Allow-Methods: POST');
6+
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization,X-Requested-With');
7+
8+
include_once '../../config/Database.php';
9+
include_once '../../models/Category.php';
10+
// Instantiate DB & connect
11+
$database = new Database();
12+
$db = $database->connect();
13+
14+
// Instantiate blog post object
15+
$category = new Category($db);
16+
17+
// Get raw posted data
18+
$data = json_decode(file_get_contents("php://input"));
19+
20+
$category->name = $data->name;
21+
22+
// Create Category
23+
if($category->create()) {
24+
echo json_encode(
25+
array('message' => 'Category Created')
26+
);
27+
} else {
28+
echo json_encode(
29+
array('message' => 'Category Not Created')
30+
);
31+
}

api/category/delete.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
// Headers
3+
header('Access-Control-Allow-Origin: *');
4+
header('Content-Type: application/json');
5+
header('Access-Control-Allow-Methods: DELETE');
6+
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization,X-Requested-With');
7+
8+
include_once '../../config/Database.php';
9+
include_once '../../models/Category.php';
10+
// Instantiate DB & connect
11+
$database = new Database();
12+
$db = $database->connect();
13+
14+
// Instantiate blog post object
15+
$category = new Category($db);
16+
17+
// Get raw posted data
18+
$data = json_decode(file_get_contents("php://input"));
19+
20+
// Set ID to UPDATE
21+
$category->id = $data->id;
22+
23+
// Delete post
24+
if($category->delete()) {
25+
echo json_encode(
26+
array('message' => 'Category deleted')
27+
);
28+
} else {
29+
echo json_encode(
30+
array('message' => 'Category not deleted')
31+
);
32+
}

api/category/read_single.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
// Headers
4+
header('Access-Control-Allow-Origin: *');
5+
header('Content-Type: application/json');
6+
7+
include_once '../../config/Database.php';
8+
include_once '../../models/Category.php';
9+
// Instantiate DB & connect
10+
$database = new Database();
11+
$db = $database->connect();
12+
// Instantiate blog category object
13+
$category = new Category($db);
14+
15+
// Get ID
16+
$category->id = isset($_GET['id']) ? $_GET['id'] : die();
17+
18+
// Get post
19+
$category->read_single();
20+
21+
// Create array
22+
$category_arr = array(
23+
'id' => $category->id,
24+
'name' => $category->name
25+
);
26+
27+
// Make JSON
28+
print_r(json_encode($category_arr));

api/category/update.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
// Headers
3+
header('Access-Control-Allow-Origin: *');
4+
header('Content-Type: application/json');
5+
header('Access-Control-Allow-Methods: PUT');
6+
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization,X-Requested-With');
7+
8+
include_once '../../config/Database.php';
9+
include_once '../../models/Category.php';
10+
// Instantiate DB & connect
11+
$database = new Database();
12+
$db = $database->connect();
13+
14+
// Instantiate blog post object
15+
$category = new Category($db);
16+
17+
// Get raw posted data
18+
$data = json_decode(file_get_contents("php://input"));
19+
20+
// Set ID to UPDATE
21+
$category->id = $data->id;
22+
23+
$category->name = $data->name;
24+
25+
// Update post
26+
if($category->update()) {
27+
echo json_encode(
28+
array('message' => 'Category Updated')
29+
);
30+
} else {
31+
echo json_encode(
32+
array('message' => 'Category not updated')
33+
);
34+
}

models/Category.php

+114-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct($db) {
1717
// Get categories
1818
public function read() {
1919
// Create query
20-
$query = 'SELECT
20+
$query = 'SELECT
2121
id,
2222
name,
2323
created_at
@@ -34,4 +34,116 @@ public function read() {
3434

3535
return $stmt;
3636
}
37-
}
37+
38+
// Get Single Category
39+
public function read_single(){
40+
// Create query
41+
$query = 'SELECT
42+
id,
43+
name
44+
FROM
45+
' . $this->table . '
46+
WHERE id = ?
47+
LIMIT 0,1';
48+
49+
//Prepare statement
50+
$stmt = $this->conn->prepare($query);
51+
52+
// Bind ID
53+
$stmt->bindParam(1, $this->id);
54+
55+
// Execute query
56+
$stmt->execute();
57+
58+
$row = $stmt->fetch(PDO::FETCH_ASSOC);
59+
60+
// set properties
61+
$this->id = $row['id'];
62+
$this->name = $row['name'];
63+
}
64+
65+
// Create Category
66+
public function create() {
67+
// Create Query
68+
$query = 'INSERT INTO ' .
69+
$this->table . '
70+
SET
71+
name = :name';
72+
73+
// Prepare Statement
74+
$stmt = $this->conn->prepare($query);
75+
76+
// Clean data
77+
$this->name = htmlspecialchars(strip_tags($this->name));
78+
79+
// Bind data
80+
$stmt-> bindParam(':name', $this->name);
81+
82+
// Execute query
83+
if($stmt->execute()) {
84+
return true;
85+
}
86+
87+
// Print error if something goes wrong
88+
printf("Error: $s.\n", $stmt->error);
89+
90+
return false;
91+
}
92+
93+
// Update Category
94+
public function update() {
95+
// Create Query
96+
$query = 'UPDATE ' .
97+
$this->table . '
98+
SET
99+
name = :name
100+
WHERE
101+
id = :id';
102+
103+
// Prepare Statement
104+
$stmt = $this->conn->prepare($query);
105+
106+
// Clean data
107+
$this->name = htmlspecialchars(strip_tags($this->name));
108+
$this->id = htmlspecialchars(strip_tags($this->id));
109+
110+
// Bind data
111+
$stmt-> bindParam(':name', $this->name);
112+
$stmt-> bindParam(':id', $this->id);
113+
114+
// Execute query
115+
if($stmt->execute()) {
116+
return true;
117+
}
118+
119+
// Print error if something goes wrong
120+
printf("Error: $s.\n", $stmt->error);
121+
122+
return false;
123+
}
124+
125+
// Delete Category
126+
public function delete() {
127+
// Create query
128+
$query = 'DELETE FROM ' . $this->table . ' WHERE id = :id';
129+
130+
// Prepare Statement
131+
$stmt = $this->conn->prepare($query);
132+
133+
// clean data
134+
$this->id = htmlspecialchars(strip_tags($this->id));
135+
136+
// Bind Data
137+
$stmt-> bindParam(':id', $this->id);
138+
139+
// Execute query
140+
if($stmt->execute()) {
141+
return true;
142+
}
143+
144+
// Print error if something goes wrong
145+
printf("Error: $s.\n", $stmt->error);
146+
147+
return false;
148+
}
149+
}

0 commit comments

Comments
 (0)