backend-01

commit 3ab62698aaa61a9baf52b5877463e5f634105d57

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

tags: allow to edit a tag

 app/controllers/TagsController.php | 15 +++++++++++++++
 app/daos/TagDAO.php | 25 +++++++++++++++++++++++++
 app/views/tag_edit.php | 20 ++++++++++++++++++++
 config/routes.php | 8 ++++++++


diff --git a/app/controllers/TagsController.php b/app/controllers/TagsController.php
index 82ee8c4de40c24e9fdc85cd6a7a13880693c65f3..6db785be10701fd69b2bca2086fc177d0dcd1b22 100644
--- a/app/controllers/TagsController.php
+++ b/app/controllers/TagsController.php
@@ -33,6 +33,21 @@     header('Location: /tags');
     exit;
   }
 
+  public function edit($id) {
+    $tag = $this->tagDAO->getTagById($id);
+
+    return Template::render('tag_edit', ['tag' => $tag]);
+  }
+
+  public function update($id) {
+    $data = $_POST;
+
+    $this->tagDAO->updateTag($id, $data['name']);
+
+    header('Location: /tags');
+    exit;
+  }
+
   public function destroy($id) {
     $this->tagDAO->delete($id);
 




diff --git a/app/daos/TagDAO.php b/app/daos/TagDAO.php
index b32edbb16d03e72939b1bca18863d3c6dc452123..440c770722e55cfe4b90f8375e5a554ca330457b 100644
--- a/app/daos/TagDAO.php
+++ b/app/daos/TagDAO.php
@@ -30,6 +30,21 @@       return [];
     }
   }
 
+  public function getTagById($id) {
+    $sql = 'SELECT * FROM tags WHERE id = :id';
+    $stmt = $this->db->prepare($sql);
+    $stmt->bindParam(':id', $id);
+    $stmt->execute();
+
+    $tagData = $stmt->fetch(PDO::FETCH_OBJ);
+
+    if ($tagData) {
+      return new Tag($tagData->id, $tagData->name);
+    } else {
+      return null;
+    }
+  }
+
   public function save(Tag $tag) {
     $sql = "INSERT INTO tags (name) VALUES (:name)";
     $stmt = $this->db->prepare($sql);
@@ -37,6 +52,16 @@     $stmt->bindValue(':name', $tag->getName());
 
     $stmt->execute();
   }
+
+  public function updateTag($id, $name) {
+    $sql = 'UPDATE tags SET name = :name WHERE id = :id';
+    $stmt = $this->db->prepare($sql);
+    $stmt->bindParam(':name', $name);
+    $stmt->bindParam(':id', $id);
+
+    return $stmt->execute();
+  }
+
 
   public function delete($id) {
     $sql = "DELETE FROM tags WHERE id = :id";




diff --git a/app/views/tag_edit.php b/app/views/tag_edit.php
new file mode 100644
index 0000000000000000000000000000000000000000..ced5f1538f04c82d8fdfb4127da91f15a20b2d00
--- /dev/null
+++ b/app/views/tag_edit.php
@@ -0,0 +1,20 @@
+<div class="container mx-auto mt-10">
+  <h1 class="text-2xl font-bold text-gray-700 mb-6">Editar Tag</h1>
+
+  <form action="/tags/edit/<?= $tag->getId() ?>" method="POST">
+    <div class="mb-4">
+      <label for="name" class="block text-sm font-medium text-gray-700">Nome</label>
+      <input type="text" id="name" name="name" value="<?= htmlspecialchars($tag->getName()) ?>"
+            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="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 Tag
+      </button>
+
+      <a href="/tags" class="bg-gray-600 text-white py-2 px-4 rounded hover:bg-gray-700">Voltar</a>
+    </div>
+  </form>
+</div>
+




diff --git a/config/routes.php b/config/routes.php
index 732e1588f1d35ecf5c2f11ee5eee0246c08abde8..dfd8f161435c39ae55557cedc818fe1b59fae0c7 100644
--- a/config/routes.php
+++ b/config/routes.php
@@ -67,6 +67,14 @@   // POST /tags/create
   } elseif ($uri === 'tags/create' && $method === 'POST') {
     return (new TagsController())->store();
 
+  // GET /tags/edit/{id}
+  } elseif (preg_match('/^tags\/edit\/(\d+)$/', $uri, $matches) && $method === 'GET') {
+    return (new TagsController())->edit($matches[1]);
+
+  // POST /tags/edit/{id}
+  } elseif (preg_match('/^tags\/edit\/(\d+)$/', $uri, $matches) && $method === 'POST') {
+    return (new TagsController())->update($matches[1]);
+
   // GET /tags/delete/{id}
   } elseif (preg_match('/^tags\/delete\/(\d+)$/', $uri, $matches) && $method === 'GET') {
     return (new TagsController())->destroy($matches[1]);