backend-01

ref: master

app/controllers/IncomesController.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php

require_once __DIR__ . '/../daos/IncomeDAO.php';
require_once __DIR__ . '/../models/Income.php';

class IncomesController {
  private $incomeDAO;

  public function __construct() {
    $this->incomeDAO = new IncomeDAO();
  }

  public function index() {
    $incomes = $this->incomeDAO->getIncomesByUser($_SESSION['user_id']);

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

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

  public function store() {
    $data = $_POST;

    $income = new Income(
      null,
      $data['title'],
      $data['amount'],
      $data['income_type'],
      $data['recurrence_period'],
      $data['date'],
      $_SESSION['user_id'],
    );

    $this->incomeDAO->create($income);

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

  public function edit($id) {
    $income = $this->incomeDAO->getIncomeById($id);

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

  public function update($id) {
    $data = $_POST;

    $income = new Income(
      $id,
      $data['title'],
      $data['amount'],
      $data['income_type'],
      $data['recurrence_period'],
      $data['date'],
      $_SESSION['user_id'],
    );

    $this->incomeDAO->update($income);

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

  public function destroy($id) {
    $this->incomeDAO->destroy($id, $_SESSION['user_id']);

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