From 0bba2280eec1375c357966edd643430cb549c7f4 Mon Sep 17 00:00:00 2001
From: bewe <93899302+bernarduswillson@users.noreply.github.com>
Date: Mon, 13 Nov 2023 17:56:07 +0700
Subject: [PATCH] feat: fetch gems from soap

---
 api/exercise/submit.php         |  2 +-
 app/controllers/Exercise.php    |  8 ++++----
 app/controllers/Merchandise.php | 23 ++++++++++++++++-------
 3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/api/exercise/submit.php b/api/exercise/submit.php
index 55a2ec2..a1d8477 100644
--- a/api/exercise/submit.php
+++ b/api/exercise/submit.php
@@ -16,7 +16,7 @@ function submitQuiz($exerciseId, $selectedOption)
     var_dump($exerciseId);
 
     $ch = curl_init();
-    curl_setopt($ch, CURLOPT_URL, "http://172.20.10.2:5000/exercise/result/" . $exerciseId);
+    curl_setopt($ch, CURLOPT_URL, "http://express:5000/exercise/result/" . $exerciseId);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($submitData));
diff --git a/app/controllers/Exercise.php b/app/controllers/Exercise.php
index 1d37887..60ca414 100644
--- a/app/controllers/Exercise.php
+++ b/app/controllers/Exercise.php
@@ -9,7 +9,7 @@ class Exercise extends Controller
     $data["pageTitle"] = "Test your knowledge!";
     $data["languages"] = $this->model("LanguageModel")->getAllLanguage();
 
-    $baseUrl = 'http://172.20.10.2:5000/exercise';
+    $baseUrl = 'http://express:5000/exercise';
     $ch = curl_init($baseUrl);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $response = curl_exec($ch);
@@ -35,7 +35,7 @@ class Exercise extends Controller
       $data["user_id"] = $_SESSION['user_id'];
       $data["exercise_id"] =  intval($exerciseId);
 
-      $baseUrl = 'http://172.20.10.2:5000/exercise';
+      $baseUrl = 'http://express:5000/exercise';
       $ch = curl_init($baseUrl);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $response = curl_exec($ch);
@@ -50,7 +50,7 @@ class Exercise extends Controller
         }
       }
 
-      $baseUrl = 'http://172.20.10.2:5000/question/' . $data["exercise_id"];
+      $baseUrl = 'http://express:5000/question/' . $data["exercise_id"];
       $ch = curl_init($baseUrl);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $response = curl_exec($ch);
@@ -58,7 +58,7 @@ class Exercise extends Controller
 
       $data["question"] = $question['result'][0]; // ini '0' bisa diganti page
 
-      $baseUrl = 'http://172.20.10.2:5000/option/' . $data["question"]["question_id"];
+      $baseUrl = 'http://express:5000/option/' . $data["question"]["question_id"];
       $ch = curl_init($baseUrl);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $response = curl_exec($ch);
diff --git a/app/controllers/Merchandise.php b/app/controllers/Merchandise.php
index b9867ea..8e499e9 100644
--- a/app/controllers/Merchandise.php
+++ b/app/controllers/Merchandise.php
@@ -9,7 +9,7 @@ class Merchandise extends Controller
         $data["pageTitle"] = "Merch!";
         $data["user_id"] = $_SESSION['user_id'];
 
-        $baseUrl = 'http://10.97.58.62:8080/gems';
+        $baseUrl = 'http://soap:8080/gems';
 
         $soapRequest = '<x:Envelope
                             xmlns:x="http://schemas.xmlsoap.org/soap/envelope/"
@@ -17,7 +17,7 @@ class Merchandise extends Controller
                             <x:Header/>
                             <x:Body>
                                 <ser:getGems>
-                                    <arg0>1</arg0>
+                                    <arg0>' . $data["user_id"] . '</arg0>
                                 </ser:getGems>
                             </x:Body>
                         </x:Envelope>';
@@ -26,10 +26,14 @@ class Merchandise extends Controller
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $soapRequest);
-        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
-            'Content-Type: text/xml',
-            'SOAPAction: getGems'
-        )
+        curl_setopt(
+            $ch,
+            CURLOPT_HTTPHEADER,
+            array(
+                'Content-Type: text/xml',
+                'SOAPAction: getGems',
+                'X-api-key: toco_php'
+            )
         );
 
         $response = curl_exec($ch);
@@ -40,7 +44,12 @@ class Merchandise extends Controller
 
         curl_close($ch);
         $data['gems'] = $response;
-
+        
+        if (preg_match('/<return>(\d+)<\/return>/', $response, $matches)) {
+            $data['gems'] = (int)$matches[1];
+        } else {
+            echo 'Error extracting numeric value from XML response.';
+        }
 
         $this->view('header/index', $data);
         $this->view('navbar/index');
-- 
GitLab