From 9c5ac747cdd988577fd157cd65a8333585b9120d Mon Sep 17 00:00:00 2001
From: Addin Munawwar <91366027+moonawar@users.noreply.github.com>
Date: Fri, 17 Nov 2023 14:53:41 +0700
Subject: [PATCH] feat: auth for book access

---
 app/pages/premium/CollectionDetailPage.php | 22 +++++++++++++++++++++-
 app/pages/user/MySubscriptionPage.php      |  1 -
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/app/pages/premium/CollectionDetailPage.php b/app/pages/premium/CollectionDetailPage.php
index dd3193b..ef9df0a 100644
--- a/app/pages/premium/CollectionDetailPage.php
+++ b/app/pages/premium/CollectionDetailPage.php
@@ -38,13 +38,33 @@
             $raw_data = file_get_contents("http://host.docker.internal:8040/api/curator-collection/$collectionId");
             $data = json_decode($raw_data, true);
             $books = $data['books'];
+            echo json_encode($data);
 
+            require_once dirname(__DIR__) . '../../clients/SoapConsumer.php';
+            $soap = new SoapConsumer();
+            $username = $_SESSION['username'];
+            $soapData = $soap->getSubscriptionOf($username);
+            $subData = $soapData["Response"]["data"];
+            
+            $subs = $subData["item"];
+
+            // check if user has subscribed to this curator, collectionId is in subs
+            $subscribed = false;
+            foreach ($subs as $sub) {
+                if ($sub["curator"] == $books[0]["curator"]) {
+                    $subscribed = true;
+                    break;
+                }
+            }
+
+            $link = $subscribed ? '<a href="/premium/book/' . $book['book_id'] .'">Details</a>' : "Subscribe to view details";
+            
             foreach (($books) as $index => $book) {
                 echo "<tr>";
                 echo "<td>" . $index+1 . "</td>";
                 echo "<td>" . $book['title'] . "</td>";
                 echo "<td>" . $book['author'] . "</td>";
-                echo '<td><a href="/premium/book/' . $book['book_id'] .'">Details</a></td>';
+                echo '<td>' . $link . '</td>';
                 echo "</tr>";
             }
             ?>
diff --git a/app/pages/user/MySubscriptionPage.php b/app/pages/user/MySubscriptionPage.php
index b5c2a14..2227dd9 100644
--- a/app/pages/user/MySubscriptionPage.php
+++ b/app/pages/user/MySubscriptionPage.php
@@ -46,7 +46,6 @@
 
                     $raw_data = file_get_contents('http://host.docker.internal:8040/api/curator/' . $sub['curator']);
                     $data = json_decode($raw_data, true);
-                    echo var_dump($data);
                     $colId = $data['collectionId'];
 
                     echo "<tr>";
-- 
GitLab