Skip to content

Commit 37b15b5

Browse files
committed
updating with patch requests
1 parent 8c81fa2 commit 37b15b5

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

controllers/notes/edit.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Core\App;
4+
use Core\Database;
5+
6+
// $config = require base_path('config.php');
7+
// $db = new Database($config['database']);
8+
$db = App::resolve(Database::class);
9+
10+
$currentUserId = 1;
11+
12+
$note = $db->query('select * from notes where id = :id', [
13+
'id' => $_GET['id']
14+
])->findOrFail();
15+
16+
authorize($note['user_id'] === $currentUserId);
17+
18+
view("notes/edit.view.php", [
19+
'heading' => 'Edit Note',
20+
'errors' => [],
21+
'note' => $note
22+
]);

controllers/notes/update.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use Core\App;
4+
use Core\Database;
5+
use Core\Validator;
6+
7+
// $config = require base_path('config.php');
8+
// $db = new Database($config['database']);
9+
$db = App::resolve(Database::class);
10+
11+
$currentUserId = 1;
12+
13+
//find the corresponding note
14+
$note = $db->query('select * from notes where id = :id', [
15+
'id' => $_POST['id']
16+
])->findOrFail();
17+
18+
// authorize the current user can update/edit the note
19+
authorize($note['user_id'] === $currentUserId);
20+
21+
// validate the form like if you don't include anything or you include too much characters
22+
$errors = [];
23+
24+
if (! Validator::string($_POST['body'], 1, 1000)) {
25+
$errors['body'] = 'A body of no more than 1,000 characters is required.';
26+
}
27+
28+
29+
// if no validation errors, update the record in the notes database table.
30+
31+
// if if we do have validation errors, we return the view with the errors
32+
// but again later we will learn about a process where if the validation fails,
33+
// you redirect to the specific controller (ex sessions and flash messages etc.)
34+
// for now we return the view directly with the errors
35+
if (count($errors)){
36+
return view('notes/edit.view.php', [
37+
'heading' => 'Edit Note',
38+
'errors' => $errors,
39+
'note' => $note
40+
]);
41+
}
42+
43+
$db->query('UPDATE notes SET body = :body where id = :id', [
44+
'id' => $_POST['id'],
45+
'body' => $_POST['body'],
46+
]);
47+
48+
// redirect the user
49+
header('location: /notes');
50+
die();

routes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
$router->get('/note', 'controllers/notes/show.php');
99
$router->delete('/note', 'controllers/notes/destroy.php');
1010

11+
$router->get('/note/edit', 'controllers/notes/edit.php');
12+
$router->patch('/note', 'controllers/notes/update.php');
13+
1114
$router->get('/notes/create', 'controllers/notes/create.php');
1215
$router->post('/notes', 'controllers/notes/store.php');
1316

views/notes/edit.view.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php require base_path('views/partials/head.php') ?>
2+
<?php require base_path('views/partials/nav.php') ?>
3+
<?php require base_path('views/partials/banner.php') ?>
4+
5+
<main>
6+
<div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
7+
<div class="md:grid md:grid-cols-3 md:gap-6">
8+
<div class="mt-5 md:col-span-2 md:mt-0">
9+
<form method="POST" action="/note">
10+
<input type="hidden" name="_method" value="PATCH">
11+
<input type="hidden" name="id" value="<?= $note['id'] ?>">
12+
<div class="shadow sm:overflow-hidden sm:rounded-md">
13+
<div class="space-y-6 bg-white px-4 py-5 sm:p-6">
14+
<div>
15+
<label
16+
for="body"
17+
class="block text-sm font-medium text-gray-700"
18+
>Body</label>
19+
20+
<div class="mt-1">
21+
<textarea
22+
id="body"
23+
name="body"
24+
rows="3"
25+
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm"
26+
placeholder="Here's an idea for a note..."
27+
><?= $note['body'] ?></textarea>
28+
29+
<?php if (isset($errors['body'])) : ?>
30+
<p class="text-red-500 text-xs mt-2"><?= $errors['body'] ?></p>
31+
<?php endif; ?>
32+
</div>
33+
</div>
34+
</div>
35+
36+
<div class="bg-gray-50 px-4 py-3 text-right sm:px-6 flex gap-x-4 justify-end">
37+
<a
38+
href="/notes"
39+
type="submit"
40+
class="inline-flex justify-center rounded-md border border-transparent bg-gray-500 py-2 px-4 text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
41+
>
42+
Cancel
43+
</a>
44+
<button
45+
type="submit"
46+
class="inline-flex justify-center rounded-md border border-transparent bg-indigo-600 py-2 px-4 text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
47+
>
48+
Update
49+
</button>
50+
</div>
51+
</div>
52+
</form>
53+
</div>
54+
</div>
55+
</div>
56+
</main>
57+
58+
<?php require base_path('views/partials/footer.php') ?>

views/notes/show.view.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
<p><?= htmlspecialchars($note['body']) ?></p>
1212

13+
<footer class="mt-6">
14+
<a href="/note/edit?id=<?= $note['id']?>" class="inline-flex justify-center rounded-md border border-transparent bg-gray-500 py-2 px-4 text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2">Edit</a>
15+
</footer>
16+
1317
<form class="mt-6" method="POST">
1418
<input type="hidden" name="_method" value="DELETE">
1519
<input type="hidden" name="id" value="<?= $note['id'] ?>">

0 commit comments

Comments
 (0)