Added table to users page

This commit is contained in:
Notoric 2024-06-10 23:29:22 +01:00
parent d9a5ab53e5
commit c6f0058d62
4 changed files with 28 additions and 19 deletions

View File

@ -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]);
}
}

View File

@ -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();
}

View File

@ -9,4 +9,18 @@
<p>Username: <em>{{ Auth::user()->name }}</em></p>
<p>Email: <em>{{ Auth::user()->email }}</em></p>
<p>Created at: <em>{{ Auth::user()->created_at }}</em></p>
<table>
<tr>
<th>Link</th>
<th>Destination</th>
<th>Created at</th>
</tr>
@foreach ($shortlinks as $shortlink)
<tr>
<td>{{ $shortlink['shortid'] }}</td>
<td>{{ $shortlink['destination'] }}</td>
<td>{{ $shortlink['created_at'] }}</td>
</tr>
@endforeach
</table>
@endsection

View File

@ -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']);