Skip to content

Commit

Permalink
User link table from spesified user (#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix authored May 13, 2024
1 parent d58b1dc commit 18a0231
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 6 deletions.
14 changes: 14 additions & 0 deletions app/Http/Controllers/Dashboard/AllUrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Controllers\Controller;
use App\Models\Url;
use App\Models\User;
use Illuminate\Routing\Controllers\{HasMiddleware, Middleware};

class AllUrlController extends Controller implements HasMiddleware
Expand All @@ -23,6 +24,19 @@ public function view()
return view('backend.url-list');
}

/**
* Show all short links from specific user.
*
* @return \Illuminate\Contracts\View\View
*/
public function userLinkView(string $author)
{
return view('backend.url-list-of-user', [
'authorName' => $author,
'authorId' => User::where('name', $author)->first()->id,
]);
}

/**
* Show all short URLs created by guest.
*
Expand Down
100 changes: 100 additions & 0 deletions app/Livewire/Table/UrlListOfUsersTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace App\Livewire\Table;

use App\Models\Url;
use Illuminate\Database\Eloquent\Builder;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Footer;
use PowerComponents\LivewirePowerGrid\Header;
use PowerComponents\LivewirePowerGrid\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;

/**
* @codeCoverageIgnore
*/
final class UrlListOfUsersTable extends PowerGridComponent
{
const STR_LIMIT = 95;

public int $user_id;

public int $perPage = 25;

public bool $showUpdateMessages = true;

public string $sortDirection = 'desc';

public function setUp(): array
{
return [
Header::make()
->showToggleColumns()
->showSearchInput(),
Footer::make()
->showPerPage($this->perPage)
->showRecordCount('full'),
];
}

public function datasource(): Builder
{
return Url::where('user_id', $this->user_id);
}

public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('keyword', function (Url $url) {
return view('components.table.keyword', ['url' => $url])
->render();
})
->add('destination', function (Url $url) {
return view('components.table.destination', [
'url' => $url,
'limit' => self::STR_LIMIT,
])->render();
})
->add('t_clicks', function (Url $url) {
return view('components.table.visit', ['url' => $url])
->render();
})
->add('created_at_formatted', function (Url $url) {
return view('components.table.date-created', ['url' => $url])
->render();
})
->add('action', function (Url $url) {
return view('components.table.action-button', ['url' => $url])
->render();
});
}

/**
* @return array<\PowerComponents\LivewirePowerGrid\Column>
*/
public function columns(): array
{
return [
Column::make('Short URL', 'keyword')
->sortable()
->searchable(),

Column::make('Destination URL', 'destination')
->sortable()
->searchable(),
Column::make('title', 'title')
->searchable()
->hidden(),

Column::make('CLICKS', 't_clicks'),

Column::make('CREATED AT', 'created_at_formatted', 'created_at')
->searchable()
->sortable(),

Column::make('ACTIONS', 'action')
->bodyAttribute(styleAttr: ';padding-left: 8px'),
];
}
}
3 changes: 2 additions & 1 deletion app/Livewire/Table/UrlListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('author', function (Url $url) {
return '<span class="font-semibold">'.$url->author->name.'</span>';
return view('components.table.author', ['url' => $url])
->render();
})
->add('keyword', function (Url $url) {
return view('components.table.keyword', ['url' => $url])
Expand Down
21 changes: 21 additions & 0 deletions resources/views/backend/url-list-of-user.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@extends('layouts.backend')

@section('title', __('Links').' > '. $authorName)

@section('content')
<main>
<div class="common-card-style">
<div class="card_header__v2">
<div class="w-1/2">
<span class="text-2xl text-uh-1">
{{__('Links created by')}} {{$authorName}}
</span>
</div>
</div>

@include('partials/messages')

@livewire('table.UrlListOfUsersTable', ['user_id' => $authorId])
</div>
</main>
@endsection
5 changes: 5 additions & 0 deletions resources/views/components/table/author.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<span class="font-semibold">
<a href="{{ route('dashboard.allurl.u-user', $url->author->name) }}">
{{ $url->author->name }}
</a>
</span>
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
Route::get('/allurl', [AllUrlController::class, 'view'])->name('dashboard.allurl');
Route::get('/allurl/delete/{url:keyword}', [AllUrlController::class, 'delete'])->name('dashboard.allurl.su_delete');
Route::get('/allurl/u/guest', [AllUrlController::class, 'guestLinkView'])->name('dashboard.allurl.u-guest');
Route::get('/allurl/u/{user:name}', [AllUrlController::class, 'userLinkView'])->name('dashboard.allurl.u-user');

// User
Route::namespace('User')->prefix('user')->group(function () {
Expand Down
36 changes: 31 additions & 5 deletions tests/Feature/AuthPage/AllUrlsPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ public function auAdminCanAccessThisPage(): void
$response = $this->actingAs($this->adminUser())
->get(route('dashboard.allurl'));
$response->assertOk();

$response = $this->actingAs($this->adminUser())
->get(route('dashboard.allurl.u-guest'));
$response->assertOk();
}

#[Test]
Expand All @@ -29,8 +25,38 @@ public function auNormalUserCantAccessThisPage(): void
$response = $this->actingAs($this->normalUser())
->get(route('dashboard.allurl'));
$response->assertForbidden();
}

$response = $this->actingAs($this->normalUser())
/**
* Admin can access user links and guest links table page
*/
#[Group('f-allurl')]
public function testAdminCanAccessUserLinksTablePage(): void
{
$user = $this->adminUser();

$response = $this->actingAs($user)
->get(route('dashboard.allurl.u-user', $user->name));
$response->assertOk();

$response = $this->actingAs($user)
->get(route('dashboard.allurl.u-guest'));
$response->assertOk();
}

/**
* Non admin users can't access user links and guest links table page
*/
#[Group('f-allurl')]
public function testNonAdminUsersCantAccessUserLinksTablePage(): void
{
$user = $this->normalUser();

$response = $this->actingAs($user)
->get(route('dashboard.allurl.u-user', $this->adminUser()->name));
$response->assertForbidden();

$response = $this->actingAs($user)
->get(route('dashboard.allurl.u-guest'));
$response->assertForbidden();
}
Expand Down

0 comments on commit 18a0231

Please sign in to comment.