From f946ce85d58305c253567098406b10060f6fc4ab Mon Sep 17 00:00:00 2001
From: Ulung32 <13521122@mahasiswa.itb.ac.id>
Date: Fri, 17 Nov 2023 10:44:38 +0700
Subject: [PATCH] add history (call rest)

---
 src/components/history/History.php           | 16 ++++----
 src/controller/history/HistoryController.php | 12 +++++-
 src/rest/Rest.php                            | 43 ++++++++++++++++++++
 3 files changed, 61 insertions(+), 10 deletions(-)
 create mode 100644 src/rest/Rest.php

diff --git a/src/components/history/History.php b/src/components/history/History.php
index 2558a59..cced51f 100644
--- a/src/components/history/History.php
+++ b/src/components/history/History.php
@@ -22,21 +22,19 @@
                     <?php
                     include_once(dirname(__DIR__) . "/template/HistoryCard.php");
 
-                    // TODO : fetch data from REST
-                    $histories = [
-                        ['courierName' => "jamal", 'price' => 100, 'rating' => 4, 'historyId' => 1],
-                        ['courierName' => "rusdi", 'price' => 69, 'rating' => 0, 'historyId' => 2],
-                        ['courierName' => "Ukin", 'price' => 420, 'rating' => 3, 'historyId' => 3],
-                    ];
+                    $histories = $this->data;
                     ?>
                     <section class="articles">
                     <?php
+                    // var_dump($histories);
+                    $index = 1;
                     foreach ($histories as $history) {
                         history_card_template(
-                            $history['courierName'],
-                            $history['price'],
+                            "Pesanan" . $index,
+                            "Penerima: ".$history['nama_penerima'],
                             $history['rating'],
-                            $history['historyId']);
+                            $history['id']);
+                        $index++;
                     }
                     ?>
                     </section>
diff --git a/src/controller/history/HistoryController.php b/src/controller/history/HistoryController.php
index 0b88076..db2d189 100644
--- a/src/controller/history/HistoryController.php
+++ b/src/controller/history/HistoryController.php
@@ -1,7 +1,17 @@
 <?php
 
+require_once __DIR__ . '/../../rest/Rest.php';
 class HistoryController extends Controller{
     public function index() {
-        $this->render();
+        $userId = $_SESSION['user_id'];
+        Rest::setEndpoint('history/penerima/'.$userId);
+        Rest::setOption('GET', null);
+
+        $History = Rest::call();
+
+
+        $data = json_decode($History, true);
+        // var_dump($data);
+        $this->render($data);
     }
 }
\ No newline at end of file
diff --git a/src/rest/Rest.php b/src/rest/Rest.php
new file mode 100644
index 0000000..5a8304d
--- /dev/null
+++ b/src/rest/Rest.php
@@ -0,0 +1,43 @@
+<?php
+
+class Rest {
+    public static $apiEndpoint;
+
+    public static $url = "http://host.docker.internal:5000/";
+    public static $option;
+
+
+    public static function setOption($method, $data){
+        if(!is_null($data)){
+            static::$option = [
+                'http' => [
+                    'method' => $method, 
+                    'header' => 'Content-Type: application/json', 
+                    'content' => json_encode($data),
+                ],
+            ];
+        }else{
+            static::$option = [
+                'http' => [
+                    'method' => $method, 
+                    'header' => 'Content-Type: application/json', 
+                ],
+            ];
+        }
+        
+    }
+
+    public static function setEndpoint($apiEndpoint){
+        static::$apiEndpoint = $apiEndpoint;
+
+    }
+
+    public static function call(){
+        $context = stream_context_create(static::$option);
+        $response = file_get_contents(static::$url . static::$apiEndpoint, false, $context);
+        return $response;
+    }
+
+
+}
+?>
-- 
GitLab