Skip to content

Commit

Permalink
add top visited and recently created
Browse files Browse the repository at this point in the history
  • Loading branch information
Zander Lewis committed Jul 5, 2024
1 parent ffcf7fe commit aa77943
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 48 deletions.
5 changes: 3 additions & 2 deletions app/Http/Controllers/RedirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\ShortenedUrl; // Assuming you have a ShortenedUrl model
use App\Models\ShortenedUrl;

class RedirectController extends Controller
{
Expand All @@ -13,6 +12,8 @@ public function redirect($code)
$url = ShortenedUrl::where('short_code', $code)->first();

if ($url) {
// Increment the visit count
$url->increment('hits');
// Redirect to the original URL
return redirect()->away($url->original_url);
} else {
Expand Down
22 changes: 22 additions & 0 deletions app/Http/Controllers/ShortenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@

class ShortenController extends Controller
{

protected $dates = ['created_at', 'updated_at'];

public function getTopVisitedShortCodes()
{
$topVisited = ShortenedUrl::orderBy('hits', 'DESC')->take(10)->get(['short_code', 'hits', 'original_url']);
return view('welcome', compact('topVisited'));
}

public function getRecentlyCreatedShortCodes()
{
$recentlyCreated = ShortenedUrl::orderBy('created_at', 'DESC')->take(10)->get(['short_code', 'original_url', 'created_at']);
return view('welcome', compact('recentlyCreated'));
}

public function shorten(Request $request)
{
// Validate the input
Expand Down Expand Up @@ -39,4 +54,11 @@ public function shorten(Request $request)
]);
}
}

public function index()
{
$topVisited = ShortenedUrl::orderBy('hits', 'DESC')->take(10)->get(['short_code', 'original_url', 'hits']);
$recentlyCreated = ShortenedUrl::orderBy('created_at', 'DESC')->take(10)->get(['short_code', 'original_url', 'created_at']);
return view('welcome', compact('topVisited', 'recentlyCreated'));
}
}
1 change: 1 addition & 0 deletions public/css/app.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit aa77943

Please sign in to comment.