Skip to content

Commit 22b76ec

Browse files
committed
capturing ids and using service to decrypt
1 parent ca732fb commit 22b76ec

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

app/Http/Controllers/MainController.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace App\Http\Controllers;
44

55
use App\Models\User;
6+
use App\Services\Operations;
67
use Illuminate\Http\Request;
8+
use Illuminate\Contracts\Encryption\DecryptException;
9+
use Illuminate\Support\Facades\Crypt;
710

811
class MainController extends Controller
912
{
@@ -25,4 +28,15 @@ public function newNote()
2528
echo "I'm creating a new note!";
2629
}
2730

31+
public function editNote($id)
32+
{
33+
$id = Operations::decryptId($id);
34+
echo "I'm editing note with id = {$id}";
35+
}
36+
37+
public function deleteNote($id)
38+
{
39+
$id = Operations::decryptId($id);
40+
echo "I'm deleting note with id = {$id}";
41+
}
2842
}

app/Services/Operations.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use Illuminate\Contracts\Encryption\DecryptException;
6+
use Illuminate\Support\Facades\Crypt;
7+
8+
class Operations
9+
{
10+
11+
public static function decryptId($value)
12+
{
13+
try {
14+
$value = Crypt::decrypt($value);
15+
} catch (DecryptException $e) {
16+
return redirect()->route('home');
17+
}
18+
19+
return $value;
20+
}
21+
}

resources/views/note.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
<div class="row">
55
<div class="col">
66
<h4 class="text-info">{{ $note['title'] }}</h4>
7-
<small class="text-secondary"><span class="opacity-75 me-2">Created at:</span><strong>{{ $note['created_at'] }}</strong></small>
7+
<small class="text-secondary"><span class="opacity-75 me-2">Created at:</span><strong>{{ date('Y-m-d H:i:s', strtotime($note['created_at'])) }}</strong></small>
88
</div>
99
<div class="col text-end">
10-
<a href="#" class="btn btn-outline-secondary btn-sm mx-1"><i class="fa-regular fa-pen-to-square"></i></a>
11-
<a href="#" class="btn btn-outline-danger btn-sm mx-1"><i class="fa-regular fa-trash-can"></i></a>
10+
<a href="{{ route('edit', [ 'id' => Crypt::encrypt($note['id']) ]) }}" class="btn btn-outline-secondary btn-sm mx-1"><i class="fa-regular fa-pen-to-square"></i></a>
11+
<a href="{{ route('delete', [ 'id' => Crypt::encrypt($note['id']) ]) }}" class="btn btn-outline-danger btn-sm mx-1"><i class="fa-regular fa-trash-can"></i></a>
1212
</div>
1313
</div>
1414
<hr>

routes/web.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
});
1313

1414
Route::middleware([CheckIsLogged::class]) ->group(function(){
15-
Route::get('/' , [ MainController::class, 'index' ])->name('home');
16-
Route::get('newNote' , [ MainController::class, 'newNote' ])->name('new');
17-
Route::get('logout' , [ AuthController::class, 'logout' ])->name('logout');
15+
Route::get('/' , [ MainController::class, 'index' ])->name('home');
16+
Route::get('/newNote' , [ MainController::class, 'newNote' ])->name('new');
17+
18+
19+
Route::get('/editNote/{id}' , [ MainController::class, 'editNote' ])->name('edit');
20+
Route::get('/deleteNote/{id}', [ MainController::class, 'deleteNote'])->name('delete');
21+
22+
Route::get('/logout' , [ AuthController::class, 'logout' ])->name('logout');
1823
});

0 commit comments

Comments
 (0)