瀏覽代碼

Fixed a bug in listFlowers.php and added user history logic

Víctor Hernández 4 年之前
父節點
當前提交
8fc80285a1
共有 2 個檔案被更改,包括 112 行新增1 行删除
  1. 1
    1
      Backend/listFlowers.php
  2. 111
    0
      Backend/listHistory.php

+ 1
- 1
Backend/listFlowers.php 查看文件

@@ -130,7 +130,7 @@ function listFlowers($userID, $q = '', $edible = '', $vegetable = '', $scientifi
130 130
             echo json_encode(array("error" => "Unknown error occurred with Treffle API (Code: $code)"));
131 131
             return;
132 132
         } elseif (isset($data->error) && $data->error == "true") {
133
-            http_reponse_code(400);
133
+            http_response_code(400);
134 134
             echo json_encode(array("error" => isset($data->messages) ? $data->messages : $data->message));
135 135
             return;
136 136
         }

+ 111
- 0
Backend/listHistory.php 查看文件

@@ -0,0 +1,111 @@
1
+<?php
2
+
3
+require_once('config.php');
4
+
5
+
6
+// Filter unsupported HTTP requests
7
+if ($_SERVER["REQUEST_METHOD"] !== "GET") {
8
+    http_response_code(400); // use appropriate status
9
+    echo json_encode(array("error" => "Endpoint only supports GET requests"));
10
+    exit();
11
+}
12
+
13
+
14
+// Get user input
15
+$userID = isset($_GET['user_id']) ? $_GET['user_id'] : '';
16
+
17
+
18
+// Clean user input
19
+$userID = $connection->real_escape_string($userID);
20
+
21
+
22
+function fetchSingleFlower($flowerID) {
23
+
24
+    global $TREFFLE_BASE_URL, $TREFFLE_LIST_PATH, $TREFFLE_TOKEN;
25
+
26
+    $endpoint = $TREFFLE_BASE_URL . $TREFFLE_LIST_PATH . "/$flowerID";
27
+    $queryParams = array("token" => $TREFFLE_TOKEN);
28
+    
29
+    $curl = curl_init();
30
+    $url = sprintf("%s?%s", $endpoint, http_build_query($queryParams));
31
+    curl_setopt($curl, CURLOPT_URL, $url);
32
+    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
33
+
34
+    $response = curl_exec($curl);
35
+    $http_response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
36
+    
37
+    curl_close($curl);
38
+
39
+    return array("data" => json_decode($response), "code" => $http_response_code);
40
+    
41
+}
42
+
43
+
44
+function listHistory($userID) {
45
+
46
+    if ($userID == '') {
47
+        http_response_code(400); // use appropriate status code
48
+        echo json_encode(array("error" => "Field 'user_id' is required"));
49
+        return;
50
+    }
51
+
52
+    global $connection;
53
+
54
+    $query = "SELECT * 
55
+                FROM UserHistory AS H
56
+                WHERE H.user_id = '$userID';";
57
+
58
+    if ($result = $connection->query($query)) {
59
+
60
+
61
+        // Create flower container
62
+        $flowers = [];
63
+
64
+
65
+        // Loop through user's flowers
66
+        while($row = $result->fetch_assoc()) {
67
+            
68
+            // Query API
69
+            $apiOutput  = fetchSingleFlower($row['flower_id']);
70
+            $data = $apiOutput['data'];
71
+            $code = $apiOutput['code'];
72
+            
73
+            // Handle API errors        
74
+            if ($code >= 300 || $code < 200) {
75
+                http_response_code($code);
76
+                echo json_encode(array("error" => "Unknown error occurred with Treffle API (Code: $code)"));
77
+                return;
78
+            } elseif (isset($data->error) && $data->error == "true") {
79
+                http_response_code(400);
80
+                $msg = isset($data->message) ? $data->message : $data->messages;
81
+                echo json_encode(array("error" => $msg));
82
+                return;
83
+            }
84
+            
85
+            // Extract flower
86
+            $flower = $data->data;
87
+            $flower->isFavorite = $row['in_wishlist'] == "1" ? true : false;
88
+            $flower->hasBeenFound = $row['has_been_found'] == "1" ? true : false;
89
+            
90
+            // Append flower
91
+            $flowers[] = $flower;
92
+            
93
+        }
94
+        
95
+        
96
+        // Return response
97
+        http_response_code(200);
98
+        $response = new stdClass;
99
+        $response->data = $flowers;
100
+        echo json_encode($response);
101
+
102
+
103
+    } else {
104
+        http_response_code(500); // use appropriate status code
105
+        echo json_encode(array("error" => $connection->error));
106
+    }
107
+
108
+}
109
+
110
+
111
+listHistory($userID);