Skip to content

Commit

Permalink
Add sortable table columns for clicks and created_at on short URLs in…
Browse files Browse the repository at this point in the history
…dex page
  • Loading branch information
Harrk committed May 2, 2024
1 parent a406d4d commit b8eb196
Show file tree
Hide file tree
Showing 35 changed files with 224 additions and 952 deletions.
18 changes: 12 additions & 6 deletions app/Http/Controllers/ShortUrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enums\ShortUrlStatus;
use App\Http\Requests\ShortUrl\ShortUrlDestroyRequest;
use App\Http\Requests\ShortUrl\ShortUrlIndexRequest;
use App\Http\Requests\ShortUrl\ShortUrlStoreRequest;
use App\Http\Requests\ShortUrl\ShortUrlUpdateRequest;
use App\Http\Resources\ShortUrlResource;
Expand All @@ -23,22 +24,27 @@ public function __construct()
$this->authorizeResource(ShortUrl::class, 'short_url');
}

public function index()
public function index(ShortUrlIndexRequest $request)
{
$filters = request()->get('filters');

$shortUrls = ShortUrl::query()
->when(! Auth::user()->isSuper(), function ($q) {
$q->where('user_id', Auth::id());
})
->when(Arr::get($filters, 'domain_id'), fn ($q) => $q->where('domain_id', $filters['domain_id']))
->latest()
->when(
$request->input('filters.domain_id'),
fn ($q) => $q->where('domain_id', $request->input('filters.domain_id'))
)
->orderBy(
$request->input('sort.field', 'created_at'),
$request->input('sort.order', 'desc')
)
->paginate();

return Inertia::render('ShortUrl/Index', [
'shortUrls' => ShortUrlResource::collection($shortUrls),
'domains' => Domain::pluck('url', 'id'),
'filters' => $filters,
'filters' => $request->input('filters'),
'sort' => $request->input('sort'),
]);
}

Expand Down
18 changes: 18 additions & 0 deletions app/Http/Requests/ShortUrl/ShortUrlIndexRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Requests\ShortUrl;

use Illuminate\Foundation\Http\FormRequest;

class ShortUrlIndexRequest extends FormRequest
{
public function rules(): array
{
return [
'filters' => 'array',
'sort' => 'array',
'sort.field' => 'nullable|string|in:created_at,clicks',
'sort.order' => 'nullable|string|in:desc,asc'
];
}
}
1 change: 1 addition & 0 deletions database/factories/ShortUrlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function definition(): array
'user_id' => User::factory(),
'slug' => Str::random(6),
'url' => $this->faker->url,
'clicks' => $this->faker->numberBetween(0, 10000),
];
}
}
1 change: 0 additions & 1 deletion public/build/assets/AuthenticatedLayout-4508e713.js

This file was deleted.

1 change: 0 additions & 1 deletion public/build/assets/Block-c5c12da7.js

This file was deleted.

1 change: 0 additions & 1 deletion public/build/assets/ClicksOverTime-d15f0f64.js

This file was deleted.

1 change: 0 additions & 1 deletion public/build/assets/ConfirmPassword-d3123119.js

This file was deleted.

1 change: 0 additions & 1 deletion public/build/assets/Dashboard-3be3d3bf.js

This file was deleted.

1 change: 0 additions & 1 deletion public/build/assets/Edit-15ca66c3.js

This file was deleted.

Loading

0 comments on commit b8eb196

Please sign in to comment.