backend-01

commit be219e322e11af8305ec6d04d783b4808de0a59b

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

tags: add user_id and fetch user scoped tags

 app/controllers/TagsController.php | 5 +++--
 app/daos/TagDAO.php | 12 +++++++-----
 app/models/Tag.php | 12 +++++++++++-
 schema.sql | 4 +++-


diff --git a/app/controllers/TagsController.php b/app/controllers/TagsController.php
index 6db785be10701fd69b2bca2086fc177d0dcd1b22..c487061078825eaf420a363e41ddf0c23117773f 100644
--- a/app/controllers/TagsController.php
+++ b/app/controllers/TagsController.php
@@ -11,7 +11,7 @@     $this->tagDAO = new TagDAO();
   }
 
   public function index() {
-    $tags = $this->tagDAO->getAllTags();
+    $tags = $this->tagDAO->getAllTagsFromUser($_SESSION['user_id']);
 
     return Template::render('tags', ['tags' => $tags]);
   }
@@ -24,7 +24,8 @@   public function store() {
     $data = $_POST;
     $tag = new Tag(
       null,
-      $data['name']
+      $data['name'],
+      $_SESSION['user_id']
     );
 
     $this->tagDAO->save($tag);




diff --git a/app/daos/TagDAO.php b/app/daos/TagDAO.php
index 440c770722e55cfe4b90f8375e5a554ca330457b..820906c46f74f0dca52182e5181788ea4f445ce0 100644
--- a/app/daos/TagDAO.php
+++ b/app/daos/TagDAO.php
@@ -10,9 +10,10 @@   public function __construct() {
     $this->db = getDatabaseConnection();
   }
 
-  public function getAllTags() {
-    $sql = "SELECT id, name FROM tags";
+  public function getAllTagsFromUser($userId) {
+    $sql = 'SELECT * FROM tags WHERE user_id = :user_id';
     $stmt = $this->db->prepare($sql);
+    $stmt->bindParam(':user_id', $userId);
     $stmt->execute();
 
     $tagsData = $stmt->fetchAll(PDO::FETCH_ASSOC);
@@ -21,7 +22,7 @@     if ($tagsData) {
       $tags = [];
 
       foreach ($tagsData as $tagData) {
-        $tags[] = new Tag($tagData['id'], $tagData['name']);
+        $tags[] = new Tag($tagData['id'], $tagData['name'], $tagData['user_id']);
       }
 
       return $tags;
@@ -39,16 +40,17 @@
     $tagData = $stmt->fetch(PDO::FETCH_OBJ);
 
     if ($tagData) {
-      return new Tag($tagData->id, $tagData->name);
+      return new Tag($tagData->id, $tagData->name, $tagData->user_id);
     } else {
       return null;
     }
   }
 
   public function save(Tag $tag) {
-    $sql = "INSERT INTO tags (name) VALUES (:name)";
+    $sql = "INSERT INTO tags (name, user_id) VALUES (:name, :user_id)";
     $stmt = $this->db->prepare($sql);
     $stmt->bindValue(':name', $tag->getName());
+    $stmt->bindValue(':user_id', $tag->getUserId());
 
     $stmt->execute();
   }




diff --git a/app/models/Tag.php b/app/models/Tag.php
index 1f513ac1d18087879122f9fd244c6516221dd0db..d9fbe604dae4d7221937112ddb310209ab3b6756 100644
--- a/app/models/Tag.php
+++ b/app/models/Tag.php
@@ -2,14 +2,24 @@  
 class Tag extends Base {
   private $name;
+  private $userId;
 
-  public function __construct($id, $name) {
+  public function __construct($id, $name, $userId) {
     $this->id = $id;
     $this->name = $name;
+    $this->userId = $userId;
   }
 
   public function getName() {
     return $this->name;
+  }
+
+  public function setName($name) {
+    $this->name = $name;
+  }
+
+  public function getUserId() {
+    return $this->userId;
   }
 }
 




diff --git a/schema.sql b/schema.sql
index 556b7ed09a5cf6c4760707217f97d37f9697b2f0..ae34c454504978a8bda71fc6807cd48d221da06d 100644
--- a/schema.sql
+++ b/schema.sql
@@ -10,8 +10,10 @@
 CREATE TABLE IF NOT EXISTS tags (
   id INTEGER PRIMARY KEY AUTOINCREMENT,
   name VARCHAR(255) NOT NULL,
+  user_id INTEGER NOT NULL,
   created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
-  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+  FOREIGN KEY (user_id) REFERENCES users(id)
 );
 
 CREATE TABLE IF NOT EXISTS bills (