backend-01

commit 243714e4cc4d1fe2ec4a0458e6f69c1c07979faa

Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>

dashboard: add deleting for bills and format date and number

 app/controllers/BillsController.php | 5 ++++-
 app/daos/BillDAO.php | 21 ++++++++++++++++++++-
 app/views/dashboard.php | 6 +++---


diff --git a/app/controllers/BillsController.php b/app/controllers/BillsController.php
index ba3543b5e49a43011ad72a21d1a19bf9be6950a6..42f6b2f4c4e30b27b99c37fb0869256353f72541 100644
--- a/app/controllers/BillsController.php
+++ b/app/controllers/BillsController.php
@@ -46,6 +46,9 @@     // TODO: Implement update method
   }
 
   public function destroy($id) {
-    // TODO: Implement destroy method
+    $this->billDAO->destroy($id);
+
+    header('Location: /dashboard');
+    exit;
   }
 }




diff --git a/app/daos/BillDAO.php b/app/daos/BillDAO.php
index 50eaa5a839935bcfdaa1b3ea11c236647097f48d..79f81898667d9a18e46a277f2b477b3909b5a49f 100644
--- a/app/daos/BillDAO.php
+++ b/app/daos/BillDAO.php
@@ -11,7 +11,7 @@     $this->db = getDatabaseConnection();
   }
 
   public function findAllByUserId($userId) {
-    $sql = 'SELECT b.*, GROUP_CONCAT(t.name) AS tags
+    $sql = 'SELECT b.*, GROUP_CONCAT(t.name, ", ") AS tags
             FROM bills b
             LEFT JOIN bill_tags bt ON b.id = bt.bill_id
             LEFT JOIN tags t ON bt.tag_id = t.id
@@ -74,6 +74,25 @@       $bill->setId($billId);
       $bill->setTags($tagIds);
 
       return $bill;
+    } catch (Exception $e) {
+      $this->db->rollBack();
+      throw $e;
+    }
+  }
+
+  public function destroy($id) {
+    $this->db->beginTransaction();
+
+    try {
+      $stmt = $this->db->prepare('DELETE FROM bills WHERE id = :id');
+      $stmt->bindParam(':id', $id);
+      $stmt->execute();
+
+      $stmt = $this->db->prepare('DELETE FROM bill_tags WHERE bill_id = :bill_id');
+      $stmt->bindParam(':bill_id', $id);
+      $stmt->execute();
+
+      $this->db->commit();
     } catch (Exception $e) {
       $this->db->rollBack();
       throw $e;




diff --git a/app/views/dashboard.php b/app/views/dashboard.php
index f2cde52f75c17bab74858e30cbe9e16251b2934b..b0b9331113e168a8751be160c3233ff24c2c44d5 100644
--- a/app/views/dashboard.php
+++ b/app/views/dashboard.php
@@ -31,11 +31,11 @@         
           <?php foreach ($bills as $bill) : ?>
             <tr class="border-b">
               <td class="py-3 px-6"><?= htmlspecialchars($bill->getTitle()) ?></td>
-              <td class="py-3 px-6"><?= htmlspecialchars($bill->getAmount()) ?></td>
-              <td class="py-3 px-6"><?= htmlspecialchars($bill->getDueDate()) ?></td>
+              <td class="py-3 px-6">$<?= number_format($bill->getAmount(), 2, '.', ',') ?></td>
+              <td class="py-3 px-6"><?= date('d/m/Y', strtotime($bill->getDueDate())) ?></td>
               <td class="py-3 px-6"><?= htmlspecialchars($bill->getTags()) ?></td>
               <td class="py-3 px-6"><?= $bill->isPaid() ? 'Sim' : 'Não' ?></td>
-              <td class="py-3 px-6 flex space-x-2">
+              <td class="py-3 px-6 flex items-center space-x-2">
                 <a href="/bills/edit/<?= $bill->getId() ?>" class="text-yellow-600 hover:text-yellow-700">Editar</a>
                 <a href="/bills/delete/<?= $bill->getId() ?>" class="text-red-600 hover:text-red-700">Apagar</a>
               </td>