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 }}
+Link | +Destination | +Created at | +
---|---|---|
{{ $shortlink['shortid'] }} | +{{ $shortlink['destination'] }} | +{{ $shortlink['created_at'] }} | +