backend-01

commit 8bb27ce31d6fda1f15d6fdfc0054b8dd90fbdbef

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

bills: add editing view

 app/controllers/BillsController.php | 39 +++++++++++++++++-
 app/daos/BillDAO.php | 64 +++++++++++++++++++++++++++++++
 app/views/bill_edit.php | 44 +++++++++++++++++++++


diff --git a/app/controllers/BillsController.php b/app/controllers/BillsController.php
index 42f6b2f4c4e30b27b99c37fb0869256353f72541..6a1d1a27cb81055d031f64ac8ac9f94285850f3b 100644
--- a/app/controllers/BillsController.php
+++ b/app/controllers/BillsController.php
@@ -14,7 +14,7 @@     $this->tagDAO = new TagDAO();
   }
 
   public function create() {
-    $tags = $this->tagDAO->getAllTags();
+    $tags = $this->tagDAO->getAllTagsFromUser($_SESSION['user_id']);
 
     return Template::render('bill_create', ['tags' => $tags]);
   }
@@ -38,11 +38,44 @@     exit;
   }
 
   public function edit($id) {
-    // TODO: Implement edit method
+    $bill = $this->billDAO->getBillById($id);
+    $tags = $this->tagDAO->getAllTagsFromUser($_SESSION['user_id']);
+    $billTags = $this->billDAO->getTagsByBillId($id);
+
+    $tagIds = array_map(function($tag) {
+      return $tag->id;
+    }, $billTags);
+
+    if ($bill) {
+      return Template::render('bill_edit', [
+        'bill' => $bill,
+        'tags' => $tags,
+        'tagIds' => $tagIds
+      ]);
+    } else {
+      // TODO: Properly handle this shit
+      echo "Bill not found.";
+    }
   }
 
   public function update($id) {
-    // TODO: Implement update method
+    $data = $_POST;
+    $title = $data['title'];
+    $amount = $data['amount'];
+    $dueDate = $data['due_date'];
+    $tags = $data['tags'] ?? [];
+
+    if (empty($title) || empty($amount) || empty($dueDate)) {
+      // TODO: Flash messages
+      echo "All fields are required.";
+
+      return;
+    }
+
+    $this->billDAO->updateBill($id, $title, $amount, $dueDate, $tags);
+
+    header('Location: /dashboard');
+    exit;
   }
 
   public function destroy($id) {




diff --git a/app/daos/BillDAO.php b/app/daos/BillDAO.php
index 79f81898667d9a18e46a277f2b477b3909b5a49f..14480ac3137645249a28a284bbef4af72126bd08 100644
--- a/app/daos/BillDAO.php
+++ b/app/daos/BillDAO.php
@@ -80,6 +80,70 @@       throw $e;
     }
   }
 
+  public function getBillById($id) {
+    $sql = 'SELECT * FROM bills WHERE id = :id';
+    $stmt = $this->db->prepare($sql);
+    $stmt->bindParam(':id', $id);
+    $stmt->execute();
+
+    $billData = $stmt->fetch(PDO::FETCH_OBJ);
+
+    if ($billData) {
+      return new Bill(
+        $billData->id,
+        $billData->title,
+        $billData->amount,
+        $billData->due_date,
+        $billData->paid,
+        $billData->user_id,
+        []
+      );
+    }
+  }
+
+  public function updateBill($id, $title, $amount, $due_date, $tags) {
+    $sql = 'UPDATE bills SET title = :title, amount = :amount, due_date = :due_date WHERE id = :id';
+    $stmt = $this->db->prepare($sql);
+    $stmt->bindParam(':title', $title);
+    $stmt->bindParam(':amount', $amount);
+    $stmt->bindParam(':due_date', $due_date);
+    $stmt->bindParam(':id', $id);
+    $stmt->execute();
+
+    $this->removeTagsFromBill($id);
+
+    foreach ($tags as $tagId) {
+      $this->addTagToBill($id, $tagId);
+    }
+  }
+
+  private function removeTagsFromBill($billId) {
+    $sql = 'DELETE FROM bill_tags WHERE bill_id = :bill_id';
+    $stmt = $this->db->prepare($sql);
+    $stmt->bindParam(':bill_id', $billId);
+    $stmt->execute();
+  }
+
+  private function addTagToBill($billId, $tagId) {
+    $sql = 'INSERT INTO bill_tags (bill_id, tag_id) VALUES (:bill_id, :tag_id)';
+    $stmt = $this->db->prepare($sql);
+    $stmt->bindParam(':bill_id', $billId);
+    $stmt->bindParam(':tag_id', $tagId);
+    $stmt->execute();
+  }
+
+  public function getTagsByBillId($billId) {
+    $sql = 'SELECT t.id, t.name
+      FROM tags t
+      JOIN bill_tags bt ON t.id = bt.tag_id
+      WHERE bt.bill_id = :bill_id';
+    $stmt = $this->db->prepare($sql);
+    $stmt->bindParam(':bill_id', $billId);
+    $stmt->execute();
+
+    return $stmt->fetchAll(PDO::FETCH_OBJ);
+  }
+
   public function destroy($id) {
     $this->db->beginTransaction();
 




diff --git a/app/views/bill_edit.php b/app/views/bill_edit.php
new file mode 100644
index 0000000000000000000000000000000000000000..6b0f5bf8f49b490a57417952f6c609c2336a6804
--- /dev/null
+++ b/app/views/bill_edit.php
@@ -0,0 +1,44 @@
+<div class="container mx-auto mt-10">
+  <h1 class="text-2xl font-bold text-gray-700 mb-6">Editar Gasto</h1>
+
+  <form action="/bills/edit/<?= $bill->getId() ?>" method="POST">
+    <div class="mb-4">
+      <label for="title" class="block text-sm font-medium text-gray-700">Título</label>
+      <input type="text" id="title" name="title" value="<?= htmlspecialchars($bill->getTitle()) ?>"
+             class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500" required />
+    </div>
+
+    <div class="mb-4">
+      <label for="amount" class="block text-sm font-medium text-gray-700">Valor</label>
+      <input type="text" id="amount" name="amount" value="<?= htmlspecialchars($bill->getAmount()) ?>"
+             class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500" required />
+    </div>
+
+    <div class="mb-4">
+      <label for="due_date" class="block text-sm font-medium text-gray-700">Vencimento</label>
+      <input type="date" id="due_date" name="due_date" value="<?= htmlspecialchars($bill->getDueDate()) ?>"
+             class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500" required />
+    </div>
+
+    <div class="mb-4">
+      <label for="tags" class="block text-sm font-medium text-gray-700">Tags</label>
+      <div class="space-y-2">
+        <?php foreach ($tags as $tag) : ?>
+          <label class="inline-flex items-center">
+            <input type="checkbox" name="tags[]" value="<?= $tag->getId() ?>" <?= in_array($tag->getId(), $tagIds) ? 'checked' : '' ?> class="form-checkbox text-blue-500">
+            <span class="ml-2"><?= htmlspecialchars($tag->getName()) ?></span>
+          </label>
+        <?php endforeach; ?>
+      </div>
+    </div>
+
+    <div class="flex items-center space-x-4">
+      <button type="submit" class="bg-blue-600 text-white py-2 px-4 rounded hover:bg-blue-700">
+        Atualizar Gasto
+      </button>
+
+      <a href="/dashboard" class="bg-gray-600 text-white py-2 px-4 rounded hover:bg-gray-700">Voltar</a>
+    </div>
+  </form>
+</div>
+