backend-01

ref: master

app/controllers/TagsController.php


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

require_once __DIR__ . '/../daos/TagDAO.php';
require_once __DIR__ . '/../models/Tag.php';

class TagsController {
  private $tagDAO;

  public function __construct() {
    $this->tagDAO = new TagDAO();
  }

  public function index() {
    $tags = $this->tagDAO->getAllTagsFromUser($_SESSION['user_id']);

    return Template::render('tags', ['tags' => $tags]);
  }

  public function create() {
    return Template::render('tag_create');
  }

  public function store() {
    $data = $_POST;
    $tag = new Tag(
      null,
      $data['name'],
      $_SESSION['user_id']
    );

    $this->tagDAO->save($tag);

    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);

    header('Location: /tags');
    exit;
  }
}