Skip to content

Commit 3d60c26

Browse files
committed
ugly delete added
1 parent 2e41df9 commit 3d60c26

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

Core/router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function abort($code = 404)
2222
{
2323
http_response_code($code);
2424
// TODO: check for corresponding view file if it exists or not
25-
require "views/{$code}.php";
25+
require base_path("views/{$code}.php");
2626
die();
2727
}
2828

controllers/notes/show.php

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,46 @@
55
use Core\Database;
66

77
$db = new Database($config['database']);
8+
$currentUserId = 5;
89

9-
if($_SERVER['REQUEST_METHOD'] === 'POST') {
10-
echo "Deleting...";
11-
}
10+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
11+
// form was submitted delete the current note
12+
$note = $db->query('SELECT * FROM notes where id = :id', [
13+
'id' => $_GET['id']
14+
])->findOrFail();
15+
16+
17+
authorize($note['user_id'] === $currentUserId);
18+
19+
// dd($_POST);
20+
$db->query('DELETE FROM notes WHERE id = :id', [
21+
':id' => $_POST['id'],
22+
]);
1223

13-
// $heading = 'Note';
14-
$currentUserId = 5;
1524

16-
$note = $db->query('SELECT * FROM notes where id = :id', [
17-
'id' => $_GET['id']
18-
])->findOrFail();
25+
header('location: /notes');
26+
exit();
27+
} else {
1928

2029

21-
authorize($note['user_id'] === $currentUserId);
30+
// $heading = 'Note';
31+
// $currentUserId = 5;
2232

23-
// include base_path('views/notes/show.view.php');
24-
view('notes/show.view.php', [
25-
'heading' => 'Note',
26-
'note' => $note
27-
]);
33+
$note = $db->query('SELECT * FROM notes where id = :id', [
34+
'id' => $_GET['id']
35+
])->findOrFail();
2836

29-
// both is acceptable with : and without : no difference
30-
// $notes = $db->query('SELECT * FROM notes where id = :id', ['id' => $id])->fetch();
31-
// $notes = $db->query('SELECT * FROM notes where id = :id', [':id' => $id])->fetch();
37+
38+
authorize($note['user_id'] === $currentUserId);
39+
40+
// include base_path('views/notes/show.view.php');
41+
view('notes/show.view.php', [
42+
'heading' => 'Note',
43+
'note' => $note
44+
]);
45+
46+
// both is acceptable with : and without : no difference
47+
// $notes = $db->query('SELECT * FROM notes where id = :id', ['id' => $id])->fetch();
48+
// $notes = $db->query('SELECT * FROM notes where id = :id', [':id' => $id])->fetch();
49+
50+
}

views/notes/show.view.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212

1313
<form class="mt-6" method="POST">
14+
<input type="hidden" name="id" value="<?= $note['id'] ?>">
1415
<button class="text-sm text-red-500">Delete</button>
1516
</form>
1617
</div>

0 commit comments

Comments
 (0)