|
@@ -0,0 +1,88 @@
|
|
1
|
+<?PHP
|
|
2
|
+ header('Access-Control-Allow-Origin: *');
|
|
3
|
+ header('Access-Control-Allow-Headers: *');
|
|
4
|
+ header('Access-Control-Allow-Methods: *');
|
|
5
|
+ header('Content-Type: application/json; charset=UTF-8');
|
|
6
|
+
|
|
7
|
+ //Database Connection
|
|
8
|
+ $host = 'localhost';
|
|
9
|
+ $dbname = 'censo';
|
|
10
|
+ $user = 'luis.ortiz79';
|
|
11
|
+ $passw = '27gnpXrDmPUPzEPQ';
|
|
12
|
+
|
|
13
|
+ try{
|
|
14
|
+ $connection = mysqli_connect($host, $user, $passw, $dbname) or $error = 1;
|
|
15
|
+ }
|
|
16
|
+ catch(Exception $ex){
|
|
17
|
+ print("Error connecting to database: ". $ex->getMessage()) and die();
|
|
18
|
+ }
|
|
19
|
+
|
|
20
|
+ $method = $_SERVER['REQUEST_METHOD'];
|
|
21
|
+ switch($method){
|
|
22
|
+ case "GET":
|
|
23
|
+ $sql = "SELECT * FROM noticias";
|
|
24
|
+ $path = explode('/', $_SERVER['REQUEST_URI']);
|
|
25
|
+ if(isset($path[3]) && is_numeric($path[3])) {
|
|
26
|
+ $sql .= " WHERE id = :id";
|
|
27
|
+ $stmt = $conn->prepare($sql);
|
|
28
|
+ $stmt->bindParam(':id', $path[3]);
|
|
29
|
+ $stmt->execute();
|
|
30
|
+ $users = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
31
|
+ } else {
|
|
32
|
+ $stmt = $conn->prepare($sql);
|
|
33
|
+ $stmt->execute();
|
|
34
|
+ $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ echo json_encode($users);
|
|
38
|
+ break;
|
|
39
|
+
|
|
40
|
+ case "POST":
|
|
41
|
+ $user = json_decode(file_get_contents('php://input') );
|
|
42
|
+ $sql = "INSERT INTO noticias(id, title, subject, created_at) VALUES(null, :title, :subject, :created_at)";
|
|
43
|
+ $stmt = $conn->prepare($sql);
|
|
44
|
+ $created_at = date('Y-m-d');
|
|
45
|
+ $stmt->bindParam(':title', $user->title);
|
|
46
|
+ $stmt->bindParam(':subject', $user->subject);
|
|
47
|
+ $stmt->bindParam(':created_at', $created_at);
|
|
48
|
+
|
|
49
|
+ if($stmt->execute()) {
|
|
50
|
+ $response = ['status' => 1, 'message' => 'Record updated successfully.'];
|
|
51
|
+ } else {
|
|
52
|
+ $response = ['status' => 0, 'message' => 'Failed to update record.'];
|
|
53
|
+ }
|
|
54
|
+ echo json_encode($response);
|
|
55
|
+ break;
|
|
56
|
+
|
|
57
|
+ case "PUT":
|
|
58
|
+ $user = json_decode( file_get_contents('php://input') );
|
|
59
|
+ $sql = "UPDATE noticias SET title= :title, subject = :subject, updated_at = :updated_at WHERE id = :id";
|
|
60
|
+ $stmt = $conn->prepare($sql);
|
|
61
|
+ $updated_at = date('Y-m-d');
|
|
62
|
+ $stmt->bindParam(':title', $user->title);
|
|
63
|
+ $stmt->bindParam(':subject', $user->subject);
|
|
64
|
+ $stmt->bindParam(':updated_at', $updated_at);
|
|
65
|
+
|
|
66
|
+ if($stmt->execute()) {
|
|
67
|
+ $response = ['status' => 1, 'message' => 'Record updated successfully.'];
|
|
68
|
+ } else {
|
|
69
|
+ $response = ['status' => 0, 'message' => 'Failed to update record.'];
|
|
70
|
+ }
|
|
71
|
+ echo json_encode($response);
|
|
72
|
+ break;
|
|
73
|
+
|
|
74
|
+ case "DELETE":
|
|
75
|
+ $sql = "DELETE FROM noticias WHERE id = :id";
|
|
76
|
+ $path = explode('/', $_SERVER['REQUEST_URI']);
|
|
77
|
+
|
|
78
|
+ $stmt = $conn->prepare($sql);
|
|
79
|
+ $stmt->bindParam(':id', $path[3]);
|
|
80
|
+ if($stmt->execute()) {
|
|
81
|
+ $response = ['status' => 1, 'message' => 'Record deleted successfully.'];
|
|
82
|
+ } else {
|
|
83
|
+ $response = ['status' => 0, 'message' => 'Failed to delete record.'];
|
|
84
|
+ }
|
|
85
|
+ echo json_encode($response);
|
|
86
|
+ break;
|
|
87
|
+ }
|
|
88
|
+?>
|