Part on Refactoring Section3 finished
This commit is contained in:
parent
594b8a64fa
commit
672ef6a949
5 changed files with 41 additions and 24 deletions
22
Database.php
22
Database.php
|
|
@ -2,6 +2,7 @@
|
||||||
// connect to MySQL database.
|
// connect to MySQL database.
|
||||||
class Database {
|
class Database {
|
||||||
public $connection;
|
public $connection;
|
||||||
|
public $statement;
|
||||||
|
|
||||||
public function __construct($config, $username, $password)
|
public function __construct($config, $username, $password)
|
||||||
{
|
{
|
||||||
|
|
@ -9,8 +10,23 @@ class Database {
|
||||||
$this->connection = new PDO($dsn, $username, $password, [PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC]);
|
$this->connection = new PDO($dsn, $username, $password, [PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC]);
|
||||||
}
|
}
|
||||||
public function query($query, $params = []) {
|
public function query($query, $params = []) {
|
||||||
$statement = $this->connection->prepare($query);
|
$this->statement = $this->connection->prepare($query);
|
||||||
$statement->execute($params);
|
$this->statement->execute($params);
|
||||||
return $statement;
|
return $this;
|
||||||
|
}
|
||||||
|
public function find() {
|
||||||
|
return $this->statement->fetch();
|
||||||
|
}
|
||||||
|
public function findOrFail() {
|
||||||
|
$result = $this->find();
|
||||||
|
if (! $result) {
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get() {
|
||||||
|
return $this->statement->fetchAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -5,17 +5,14 @@ $config = require('config.php');
|
||||||
$db = new Database($config['database'], $username, $password);
|
$db = new Database($config['database'], $username, $password);
|
||||||
|
|
||||||
$heading = "Note";
|
$heading = "Note";
|
||||||
|
$currentUserId = 1;
|
||||||
|
|
||||||
$note = $db->query('select * from notes where id = :id', [
|
$note = $db->query('select * from notes where id = :id', [
|
||||||
'id' => $_GET['id']
|
'id' => $_GET['id']
|
||||||
])->fetch();
|
])->findOrFail();
|
||||||
|
|
||||||
|
authorize(($note['user_id'] === $currentUserId));
|
||||||
|
|
||||||
|
|
||||||
if (!$note) {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
$currentUserId = 1;
|
|
||||||
if ($note['user_id'] != $currentUserId) {
|
|
||||||
abort(Response::FORBIDDEN);
|
|
||||||
}
|
|
||||||
//dd($notes);
|
//dd($notes);
|
||||||
require "views/note.view.php";
|
require "views/note.view.php";
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,6 @@
|
||||||
|
|
||||||
$heading = "My Notes";
|
$heading = "My Notes";
|
||||||
|
|
||||||
$notes = $db->query('select * from notes where user_id = 1')->fetchAll();
|
$notes = $db->query('select * from notes where user_id = 1')->get();
|
||||||
//dd($notes);
|
//dd($notes);
|
||||||
require "views/notes.view.php";
|
require "views/notes.view.php";
|
||||||
|
|
@ -8,4 +8,10 @@
|
||||||
}
|
}
|
||||||
function urlIs($value) {
|
function urlIs($value) {
|
||||||
return $_SERVER['REQUEST_URI'] === $value;
|
return $_SERVER['REQUEST_URI'] === $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
function authorize($condition) {
|
||||||
|
if (! $condition) {
|
||||||
|
abort(Response::FORBIDDEN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,12 +1,10 @@
|
||||||
<?php require ('partials/head.php') ?>
|
<?php require('partials/head.php') ?>
|
||||||
<?php require ('partials/nav.php') ?>
|
<?php require('partials/nav.php') ?>
|
||||||
<?php require ('partials/banner.php') ?>
|
<?php require('partials/banner.php') ?>
|
||||||
<main>
|
<main>
|
||||||
<div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
|
<div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
|
||||||
<!-- Replace with your content -->
|
<p>Now you are on the about page.</p>
|
||||||
<p>Now you are on the about page.</p>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<!-- /End replace -->
|
</main>
|
||||||
</div>
|
<?php require('partials/footer.php') ?>
|
||||||
</main>
|
|
||||||
<?php require ('partials/footer.php') ?>
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue