From b15f91f9c1b05f482fbdc9d36a5759f3672b718f Mon Sep 17 00:00:00 2001
From: melvinkj <melvinkentj@gmail.com>
Date: Mon, 9 Oct 2023 10:51:02 +0700
Subject: [PATCH] feat: delete dorm

---
 src/Controllers/DormController.php     |  2 +-
 src/Controllers/ReviewController.php   |  2 +-
 src/Controllers/WishlistController.php | 11 ++++-------
 src/Core/Application.php               |  2 +-
 src/Models/Dorm.php                    |  5 +++++
 src/Views/dorm/view.php                |  6 +++---
 src/public/static/styles/dorm-view.css |  3 ++-
 7 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/src/Controllers/DormController.php b/src/Controllers/DormController.php
index 86dcbd8..f6ee0d2 100644
--- a/src/Controllers/DormController.php
+++ b/src/Controllers/DormController.php
@@ -175,7 +175,7 @@ class DormController extends Controller
   public function delete($params)
   {
     $dormId = $params["dormId"];
-    $dorm = Dorm::deleteById($dormId);
+    Dorm::deleteById($dormId);
     Response::redirect("/");
   }
 
diff --git a/src/Controllers/ReviewController.php b/src/Controllers/ReviewController.php
index dde38bb..0233f99 100644
--- a/src/Controllers/ReviewController.php
+++ b/src/Controllers/ReviewController.php
@@ -108,7 +108,7 @@ class ReviewController extends Controller
     ]);
   }
 
-  public function delete($params)
+  public function deleteById($params)
   {
     $dormId = $params["dormId"];
 
diff --git a/src/Controllers/WishlistController.php b/src/Controllers/WishlistController.php
index fda919c..b057f9b 100644
--- a/src/Controllers/WishlistController.php
+++ b/src/Controllers/WishlistController.php
@@ -67,16 +67,13 @@ class WishlistController extends Controller
   public function add($params)
   {
     $userId = Request::getUser()->user_id;
-    // $form = new Validation([
-    //   [new Field('dorm_id'), [new Number()]],
-    // ]);
     $dormId = $params["dormId"];
 
     if (Request::getMethod() === "GET") {
-      $wishlist = Wishlists::toModel(["dorm_id" => $dormId, "user_id" => $userId]);
-      // $wishlist->user_id = $userId;
-      // var_dump($wishlist);
-      $wishlist->save();
+      if (empty(Wishlists::where(["dorm_id"=>$dormId, "user_id"=>$userId]))) {
+        $wishlist = Wishlists::toModel(["dorm_id" => $dormId, "user_id" => $userId]);
+        $wishlist->save();
+      }
       Toast::success("Kos berhasil ditambahkan ke wishlist", true);
       Response::redirect("/dorms/{$dormId}");
     }
diff --git a/src/Core/Application.php b/src/Core/Application.php
index e643dc4..463ff2d 100644
--- a/src/Core/Application.php
+++ b/src/Core/Application.php
@@ -59,7 +59,7 @@ class Application
     $this->router->methods(["GET", "POST"], "/account/editPassword", [AuthRequired::class],  UserController::class, 'editPassword');
 
     $this->router->methods(["GET", "POST"], "/reviews/{reviewId}", [AuthRequired::class], ReviewController::class, 'edit');
-    $this->router->delete("/dorms/{dormId}", [AuthRequired::class], ReviewController::class, 'delete');
+    $this->router->delete("/dorms/{dormId}/delete-review", [AuthRequired::class], ReviewController::class, 'deleteById');
 
     $this->router->methods(["GET"], "/wishlist", [AuthRequired::class], WishlistController::class, 'index');
     $this->router->methods(["GET"], "/dorms/{dormId}/add-to-wishlist", [AuthRequired::class], WishlistController::class, 'add');
diff --git a/src/Models/Dorm.php b/src/Models/Dorm.php
index 6e846df..509ebfc 100644
--- a/src/Models/Dorm.php
+++ b/src/Models/Dorm.php
@@ -78,6 +78,11 @@ class Dorm extends BaseModel
 
   public static function getInIds(array $ids)
   {
+    if (count($ids) == 0) {
+      $dorms = array();
+      $medias = array();
+      return ["dorms" => $dorms, "medias" => $medias];
+    }
     $in  = str_repeat('?,', count($ids) - 1) . '?';
     $stmt = self::$db->prepare("SELECT * FROM dorms WHERE dorm_id IN ($in)");
     $stmt->execute($ids);
diff --git a/src/Views/dorm/view.php b/src/Views/dorm/view.php
index a5f60b6..6d12e5d 100644
--- a/src/Views/dorm/view.php
+++ b/src/Views/dorm/view.php
@@ -79,7 +79,7 @@ use app\Models\User;
     <a href="/dorms/<?= $dorm->dorm_id ?>/edit" class="btn btn-primary btn-edit-kos">Edit Kos</a>
   </div>
   <form method="POST">
-      <button type="button" class="btn btn-danger dialog-btn action-btn"
+      <button type="button" class="btn btn-danger dialog-btn action-btn delete-dorm"
       data-dialog="delete-<?= $dorm->dorm_id ?>"><svg xmlns="http://www.w3.org/2000/svg" fill="none"
       viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" width="14" height="14">
       <path stroke-linecap="round" stroke-linejoin="round"
@@ -91,7 +91,7 @@ use app\Models\User;
       <input type="hidden" name="_method" value="DELETE" />
       <div class="dialog-wrapper delete-<?= $dorm->dorm_id ?>">
         <div class="dialog-content">
-          <h4 class="confirm-title">Apakah Anda yakin ingin menghapus dorm?</h4>
+          <h4 class="confirm-title">Apakah Anda yakin ingin menghapus kos?</h4>
           <div class="confirm-action">
             <button type="button" class="btn btn-outlined dialog-btn"
             data-dialog="delete-<?= $dorm->dorm_id ?>">Batal</button>
@@ -142,7 +142,7 @@ use app\Models\User;
                   </svg>
                 </a>
               <? endif ?>
-              <form method="POST">
+              <form method="POST" action="/dorms/<?= $dorm->dorm_id ?>/delete-review">
                 <button type="button" class="btn btn-danger dialog-btn action-btn"
                 data-dialog="delete-<?= $review->review_id ?>"><svg xmlns="http://www.w3.org/2000/svg" fill="none"
                 viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" width="20" height="20">
diff --git a/src/public/static/styles/dorm-view.css b/src/public/static/styles/dorm-view.css
index 170a54d..744bec5 100644
--- a/src/public/static/styles/dorm-view.css
+++ b/src/public/static/styles/dorm-view.css
@@ -79,7 +79,8 @@
 }
 
 .add-review,
-.edit-admin {
+.edit-admin,
+.delete-dorm {
   margin-top: 20px;
   margin-bottom: 20px;
 }
-- 
GitLab