Skip to content

Commit

Permalink
Streamline the controller files (#1044)
Browse files Browse the repository at this point in the history
* Merge to UrlController

* Merge to DashboardController
  • Loading branch information
realodix authored Dec 23, 2024
1 parent 3cabc4e commit b518158
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 121 deletions.
48 changes: 0 additions & 48 deletions app/Http/Controllers/Dashboard/AllUrlController.php

This file was deleted.

67 changes: 22 additions & 45 deletions app/Http/Controllers/Dashboard/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

use App\Http\Controllers\Controller;
use App\Models\Url;
use App\Models\User;
use App\Models\Visit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Routing\Controllers\{HasMiddleware, Middleware};

class DashboardController extends Controller
class DashboardController extends Controller implements HasMiddleware
{
public static function middleware(): array
{
return [new Middleware('role:admin', except: ['view'])];
}

/**
* Show all user short URLs.
* Show the dashboard and the URL list.
*
* @return \Illuminate\Contracts\View\View
*/
Expand All @@ -26,63 +31,35 @@ public function view()
}

/**
* Show shortened url details page.
* Show all short URLs created by all users.
*
* @param Url $url \App\Models\Url
* @return \Illuminate\Contracts\View\View
*/
public function edit(Url $url)
public function allUrlView()
{
Gate::authorize('updateUrl', $url);

return view('backend.edit', ['url' => $url]);
return view('backend.url-list');
}

/**
* Update the destination URL.
* Show all short links from specific user.
*
* @param Request $request \Illuminate\Http\Request
* @param Url $url \App\Models\Url
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Auth\Access\AuthorizationException
* @return \Illuminate\Contracts\View\View
*/
public function update(Request $request, Url $url)
public function userLinkView(string $author)
{
Gate::authorize('updateUrl', $url);

$request->validate([
'title' => ['max:' . Url::TITLE_LENGTH],
'long_url' => [
'required', 'url', 'max:65535',
new \App\Rules\NotBlacklistedDomain,
],
return view('backend.url-list-of-user', [
'authorName' => $author,
'authorId' => User::where('name', $author)->first()->id,
]);

$url->update([
'destination' => $request->long_url,
'title' => $request->title,
]);

return to_route('dashboard')
->with('flash_success', __('Link changed successfully !'));
}

/**
* Delete shortened URLs.
*
* @param Url $url \App\Models\Url
* @return \Illuminate\Http\RedirectResponse
* Show all short URLs created by guest.
*
* @throws \Illuminate\Auth\Access\AuthorizationException
* @return \Illuminate\Contracts\View\View
*/
public function delete(Url $url)
public function guestLinkView()
{
Gate::authorize('forceDelete', $url);

$url->delete();

return redirect()->back()
->with('flash_success', __('Link was successfully deleted.'));
return view('backend.url-list-of-guest');
}
}
52 changes: 51 additions & 1 deletion app/Http/Controllers/UrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\User;
use App\Models\Visit;
use App\Services\QrCodeService;
use Illuminate\Http\Request;
use Illuminate\Routing\Controllers\{HasMiddleware, Middleware};
use Illuminate\Support\Facades\Gate;

Expand Down Expand Up @@ -55,6 +56,49 @@ public function showDetail(Url $url)
return view('frontend.short', $data);
}

/**
* Show shortened url details page.
*
* @param Url $url \App\Models\Url
* @return \Illuminate\Contracts\View\View
*/
public function edit(Url $url)
{
Gate::authorize('updateUrl', $url);

return view('backend.edit', ['url' => $url]);
}

/**
* Update the destination URL.
*
* @param Request $request \Illuminate\Http\Request
* @param Url $url \App\Models\Url
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function update(Request $request, Url $url)
{
Gate::authorize('updateUrl', $url);

$request->validate([
'title' => ['max:' . Url::TITLE_LENGTH],
'long_url' => [
'required', 'url', 'max:65535',
new \App\Rules\NotBlacklistedDomain,
],
]);

$url->update([
'destination' => $request->long_url,
'title' => $request->title,
]);

return to_route('dashboard')
->with('flash_success', __('Link changed successfully !'));
}

/**
* Delete a shortened URL on user request.
*
Expand All @@ -69,6 +113,12 @@ public function delete(Url $url)

$url->delete();

return to_route('home');
// if requst from shorten url details page, return to home
if (request()->routeIs('su_delete')) {
return to_route('home');
}

return redirect()->back()
->with('flash_success', __('Link was successfully deleted.'));
}
}
13 changes: 6 additions & 7 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use App\Http\Controllers\Dashboard\AboutSystemController;
use App\Http\Controllers\Dashboard\AllUrlController;
use App\Http\Controllers\Dashboard\DashboardController;
use App\Http\Controllers\Dashboard\User\ChangePasswordController;
use App\Http\Controllers\Dashboard\User\UserController;
Expand All @@ -18,14 +17,14 @@
Route::middleware(['auth', 'auth.session'])->group(function () {
// Dashboard (My URLs)
Route::get('/', [DashboardController::class, 'view'])->name('dashboard');
Route::get('links/{url:keyword}/delete', [DashboardController::class, 'delete'])->name('dboard.url.delete');
Route::get('links/{url:keyword}/edit', [DashboardController::class, 'edit'])->name('dboard.url.edit.show');
Route::post('links/{url:keyword}/edit', [DashboardController::class, 'update'])->name('dboard.url.edit.store');
Route::get('links/{url:keyword}/delete', [UrlController::class, 'delete'])->name('dboard.url.delete');
Route::get('links/{url:keyword}/edit', [UrlController::class, 'edit'])->name('dboard.url.edit.show');
Route::post('links/{url:keyword}/edit', [UrlController::class, 'update'])->name('dboard.url.edit.store');

// All URLs
Route::get('/links', [AllUrlController::class, 'view'])->name('dashboard.allurl');
Route::get('/links/u/guest', [AllUrlController::class, 'guestLinkView'])->name('dashboard.allurl.u-guest');
Route::get('/links/u/{user:name}', [AllUrlController::class, 'userLinkView'])->name('dashboard.allurl.u-user');
Route::get('/links', [DashboardController::class, 'allUrlView'])->name('dashboard.allurl');
Route::get('/links/u/guest', [DashboardController::class, 'guestLinkView'])->name('dashboard.allurl.u-guest');
Route::get('/links/u/{user:name}', [DashboardController::class, 'userLinkView'])->name('dashboard.allurl.u-user');

// User
Route::namespace('User')->prefix('user')->group(function () {
Expand Down
14 changes: 7 additions & 7 deletions tests/Feature/AuthPage/DashboardPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function canAccessPage(): void
/**
* Test that an authenticated user can delete a link.
*
* @see App\Http\Controllers\Dashboard\DashboardController::delete()
* @see App\Http\Controllers\UrlController::delete()
*/
#[PHPUnit\Test]
public function canDelete(): void
Expand All @@ -45,7 +45,7 @@ public function canDelete(): void
/**
* Test that an authorized user can access the edit page.
*
* @see App\Http\Controllers\Dashboard\DashboardController::edit()
* @see App\Http\Controllers\UrlController::edit()
*/
#[PHPUnit\Test]
public function canAccessEditLinkPage(): void
Expand All @@ -59,7 +59,7 @@ public function canAccessEditLinkPage(): void
/**
* Test that an authorized user can update a link.
*
* @see App\Http\Controllers\Dashboard\DashboardController::update()
* @see App\Http\Controllers\UrlController::update()
*/
#[PHPUnit\Test]
public function canUpdateLink(): void
Expand All @@ -80,7 +80,7 @@ public function canUpdateLink(): void
}

/**
* @see App\Http\Controllers\Dashboard\DashboardController::update()
* @see App\Http\Controllers\UrlController::update()
*/
public function test_update_validates_title_length(): void
{
Expand All @@ -98,7 +98,7 @@ public function test_update_validates_title_length(): void
}

/**
* @see App\Http\Controllers\Dashboard\DashboardController::update()
* @see App\Http\Controllers\UrlController::update()
*/
public function test_update_validates_long_url_is_url(): void
{
Expand All @@ -116,7 +116,7 @@ public function test_update_validates_long_url_is_url(): void
}

/**
* @see App\Http\Controllers\Dashboard\DashboardController::update()
* @see App\Http\Controllers\UrlController::update()
*/
public function test_update_validates_long_url_max_length(): void
{
Expand All @@ -134,7 +134,7 @@ public function test_update_validates_long_url_max_length(): void
}

/**
* @see App\Http\Controllers\Dashboard\DashboardController::update()
* @see App\Http\Controllers\UrlController::update()
*/
public function test_update_validates_long_url_not_blacklisted()
{
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/AuthPage/UrlListPageSingleUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class UrlListPageSingleUserTest extends TestCase
/**
* Admin can access user links and guest links table page.
*
* @see App\Http\Controllers\Dashboard\AllUrlController::userLinkView()
* @see App\Http\Controllers\Dashboard\DashboardController::userLinkView()
*/
#[PHPUnit\Test]
public function adminCanAccessUserLinksTablePage(): void
Expand All @@ -28,7 +28,7 @@ public function adminCanAccessUserLinksTablePage(): void
/**
* Non admin users can't access user links and guest links table page.
*
* @see App\Http\Controllers\Dashboard\AllUrlController::userLinkView()
* @see App\Http\Controllers\Dashboard\DashboardController::userLinkView()
*/
#[PHPUnit\Test]
public function basicUsersCantAccessUserLinksTablePage(): void
Expand All @@ -43,7 +43,7 @@ public function basicUsersCantAccessUserLinksTablePage(): void
/**
* Test that an admin user can access the URL list page of a guest user.
*
* @see App\Http\Controllers\Dashboard\AllUrlController::guestLinkView()
* @see App\Http\Controllers\Dashboard\DashboardController::guestLinkView()
*/
#[PHPUnit\Test]
public function adminCanAccessTheUrlListPageOfAGuestUser(): void
Expand All @@ -56,7 +56,7 @@ public function adminCanAccessTheUrlListPageOfAGuestUser(): void
/**
* Non admin users can't access guest links table page.
*
* @see App\Http\Controllers\Dashboard\AllUrlController::guestLinkView()
* @see App\Http\Controllers\Dashboard\DashboardController::guestLinkView()
*/
#[PHPUnit\Test]
public function basicUsersCantAccessTheUrlListPageOfAGuestUser(): void
Expand Down
Loading

0 comments on commit b518158

Please sign in to comment.