Skip to content

Commit b1740a4

Browse files
committed
Initial commit
0 parents  commit b1740a4

File tree

9 files changed

+489
-0
lines changed

9 files changed

+489
-0
lines changed

api/category/read.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
// Headers
3+
header('Access-Control-Allow-Origin: *');
4+
header('Content-Type: application/json');
5+
6+
include_once '../../config/Database.php';
7+
include_once '../../models/Category.php';
8+
9+
// Instantiate DB & connect
10+
$database = new Database();
11+
$db = $database->connect();
12+
13+
// Instantiate category object
14+
$category = new Category($db);
15+
16+
// Category read query
17+
$result = $category->read();
18+
// Get row count
19+
$num = $result->rowCount();
20+
21+
// Check if any categories
22+
if($num > 0) {
23+
// Cat array
24+
$cat_arr = array();
25+
$cat_arr['data'] = array();
26+
27+
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
28+
extract($row);
29+
30+
$cat_item = array(
31+
'id' => $id,
32+
'name' => $name
33+
);
34+
35+
// Push to "data"
36+
array_push($cat_arr['data'], $cat_item);
37+
}
38+
39+
// Turn to JSON & output
40+
echo json_encode($cat_arr);
41+
42+
} else {
43+
// No Categories
44+
echo json_encode(
45+
array('message' => 'No Categories Found')
46+
);
47+
}

api/post/create.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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/Post.php';
10+
11+
// Instantiate DB & connect
12+
$database = new Database();
13+
$db = $database->connect();
14+
15+
// Instantiate blog post object
16+
$post = new Post($db);
17+
18+
// Get raw posted data
19+
$data = json_decode(file_get_contents("php://input"));
20+
21+
$post->title = $data->title;
22+
$post->body = $data->body;
23+
$post->author = $data->author;
24+
$post->category_id = $data->category_id;
25+
26+
// Create post
27+
if($post->create()) {
28+
echo json_encode(
29+
array('message' => 'Post Created')
30+
);
31+
} else {
32+
echo json_encode(
33+
array('message' => 'Post Not Created')
34+
);
35+
}
36+

api/post/delete.php

Lines changed: 34 additions & 0 deletions
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: 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/Post.php';
10+
11+
// Instantiate DB & connect
12+
$database = new Database();
13+
$db = $database->connect();
14+
15+
// Instantiate blog post object
16+
$post = new Post($db);
17+
18+
// Get raw posted data
19+
$data = json_decode(file_get_contents("php://input"));
20+
21+
// Set ID to update
22+
$post->id = $data->id;
23+
24+
// Delete post
25+
if($post->delete()) {
26+
echo json_encode(
27+
array('message' => 'Post Deleted')
28+
);
29+
} else {
30+
echo json_encode(
31+
array('message' => 'Post Not Deleted')
32+
);
33+
}
34+

api/post/read.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
// Headers
3+
header('Access-Control-Allow-Origin: *');
4+
header('Content-Type: application/json');
5+
6+
include_once '../../config/Database.php';
7+
include_once '../../models/Post.php';
8+
9+
// Instantiate DB & connect
10+
$database = new Database();
11+
$db = $database->connect();
12+
13+
// Instantiate blog post object
14+
$post = new Post($db);
15+
16+
// Blog post query
17+
$result = $post->read();
18+
// Get row count
19+
$num = $result->rowCount();
20+
21+
// Check if any posts
22+
if($num > 0) {
23+
// Post array
24+
$posts_arr = array();
25+
$posts_arr['data'] = array();
26+
27+
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
28+
extract($row);
29+
30+
$post_item = array(
31+
'id' => $id,
32+
'title' => $title,
33+
'body' => html_entity_decode($body),
34+
'author' => $author,
35+
'category_id' => $category_id,
36+
'category_name' => $category_name
37+
);
38+
39+
// Push to "data"
40+
array_push($posts_arr['data'], $post_item);
41+
}
42+
43+
// Turn to JSON & output
44+
echo json_encode($posts_arr);
45+
46+
} else {
47+
// No Posts
48+
echo json_encode(
49+
array('message' => 'No Posts Found')
50+
);
51+
}

api/post/read_single.php

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

api/post/update.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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/Post.php';
10+
11+
// Instantiate DB & connect
12+
$database = new Database();
13+
$db = $database->connect();
14+
15+
// Instantiate blog post object
16+
$post = new Post($db);
17+
18+
// Get raw posted data
19+
$data = json_decode(file_get_contents("php://input"));
20+
21+
// Set ID to update
22+
$post->id = $data->id;
23+
24+
$post->title = $data->title;
25+
$post->body = $data->body;
26+
$post->author = $data->author;
27+
$post->category_id = $data->category_id;
28+
29+
// Update post
30+
if($post->update()) {
31+
echo json_encode(
32+
array('message' => 'Post Updated')
33+
);
34+
} else {
35+
echo json_encode(
36+
array('message' => 'Post Not Updated')
37+
);
38+
}
39+

config/Database.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
class Database {
3+
// DB Params
4+
private $host = 'localhost';
5+
private $db_name = 'YOURDBNAME';
6+
private $username = 'YOURUSERNAME';
7+
private $password = 'YOURPASSWORD';
8+
private $conn;
9+
10+
// DB Connect
11+
public function connect() {
12+
$this->conn = null;
13+
14+
try {
15+
$this->conn = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->db_name, $this->username, $this->password);
16+
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
17+
} catch(PDOException $e) {
18+
echo 'Connection Error: ' . $e->getMessage();
19+
}
20+
21+
return $this->conn;
22+
}
23+
}

models/Category.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
class Category {
3+
// DB Stuff
4+
private $conn;
5+
private $table = 'categories';
6+
7+
// Properties
8+
public $id;
9+
public $name;
10+
public $created_at;
11+
12+
// Constructor with DB
13+
public function __construct($db) {
14+
$this->conn = $db;
15+
}
16+
17+
// Get categories
18+
public function read() {
19+
// Create query
20+
$query = 'SELECT
21+
id,
22+
name,
23+
created_at
24+
FROM
25+
' . $this->table . '
26+
ORDER BY
27+
created_at DESC';
28+
29+
// Prepare statement
30+
$stmt = $this->conn->prepare($query);
31+
32+
// Execute query
33+
$stmt->execute();
34+
35+
return $stmt;
36+
}
37+
}

0 commit comments

Comments
 (0)