From c6f0058d62a667680ddae577c99ce29c26a4ba97 Mon Sep 17 00:00:00 2001 From: Notoric Date: Mon, 10 Jun 2024 23:29:22 +0100 Subject: [PATCH] Added table to users page --- .../app/Http/Controllers/ShortlinkController.php | 16 ++++++++++++---- laravel/app/Models/Shortlink.php | 8 -------- laravel/resources/views/profile.blade.php | 14 ++++++++++++++ laravel/routes/web.php | 9 ++------- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/laravel/app/Http/Controllers/ShortlinkController.php b/laravel/app/Http/Controllers/ShortlinkController.php index 77426c4..ee8ecf9 100644 --- a/laravel/app/Http/Controllers/ShortlinkController.php +++ b/laravel/app/Http/Controllers/ShortlinkController.php @@ -48,7 +48,7 @@ class ShortlinkController extends Controller public static function goto(Request $request, $id) { try { - $shortlink = (new Shortlink())->get($id); + $shortlink = Shortlink::where('shortid', $id)->first(); // check if the link is expired or if it has reached the max clicks if ($shortlink->expires_at != null && strtotime($shortlink->expires_at) < time()) { return response()->json(['error' => 'This link has expired'], 404); @@ -69,7 +69,7 @@ class ShortlinkController extends Controller public function getDetails(Request $request, $id) { try { - $shortlink = (new Shortlink())->get($id); + $shortlink = Shortlink::where('shortid', $id)->first(); if ($shortlink->user_id != auth()->id()) { return response()->json(['error' => 'Unauthorized'], 401); } @@ -82,7 +82,7 @@ class ShortlinkController extends Controller public function update(Request $request, $id) { try { - $shortlink = (new Shortlink())->get($id); + $shortlink = Shortlink::where('shortid', $id)->first(); if ($shortlink->user_id != auth()->id()) { return response()->json(['error' => 'Unauthorized'], 401); } @@ -110,11 +110,19 @@ class ShortlinkController extends Controller public function delete(Request $request, $id) { try { - $shortlink = (new Shortlink())->get($id); + $shortlink = Shortlink::where('shortid', $id)->first(); $shortlink->delete(); return redirect('profile'); } catch (\Exception $e) { return response()->json(['error' => $e->getMessage()], 404); } } + + public function getLinksByUser(Request $request) { + if (auth()->user() == null) { + return redirect('home'); + } + $shortlinks = Shortlink::where('user_id', auth()->id())->get(); + return view('profile', ['shortlinks' => $shortlinks]); + } } diff --git a/laravel/app/Models/Shortlink.php b/laravel/app/Models/Shortlink.php index c25e9df..884f77e 100644 --- a/laravel/app/Models/Shortlink.php +++ b/laravel/app/Models/Shortlink.php @@ -30,14 +30,6 @@ class Shortlink extends Model return $this; } - public function get(string $id): Shortlink { - $shortlink = Shortlink::where('shortid', $id)->first(); - if ($shortlink == null) { - throw new \Exception('This shortened link does not exist'); - } - return $shortlink; - } - public function delete(): void { Shortlink::where('shortid', $this->id)->delete(); } diff --git a/laravel/resources/views/profile.blade.php b/laravel/resources/views/profile.blade.php index 457af28..bc67c2c 100644 --- a/laravel/resources/views/profile.blade.php +++ b/laravel/resources/views/profile.blade.php @@ -9,4 +9,18 @@

Username: {{ Auth::user()->name }}

Email: {{ Auth::user()->email }}

Created at: {{ Auth::user()->created_at }}

+ + + + + + + @foreach ($shortlinks as $shortlink) + + + + + + @endforeach +
LinkDestinationCreated at
{{ $shortlink['shortid'] }}{{ $shortlink['destination'] }}{{ $shortlink['created_at'] }}
@endsection \ No newline at end of file diff --git a/laravel/routes/web.php b/laravel/routes/web.php index a3fb480..07803b0 100644 --- a/laravel/routes/web.php +++ b/laravel/routes/web.php @@ -33,12 +33,7 @@ Route::get('/logout', function () { return redirect('home'); }); -Route::get('/profile', function () { - if (!auth()->check()) { - return redirect('home'); - } - return view('profile'); -}); +Route::get('/profile', [ShortlinkController::class, 'getLinksByUser']); Route::get('/home', function () { if (!auth()->check()) { @@ -49,7 +44,7 @@ Route::get('/home', function () { Route::post('/shorten', [ShortlinkController::class, 'create']); -Route::get('/l/{id}/details', [Link_interactionController::class, 'getCountryArray']); +Route::post('/l/{id}/update', [ShortlinkController::class, 'update']); Route::get('/l/{id}', [ShortlinkController::class, 'getDetails']);