Skip to content

Commit aa77943

Browse files
author
Zander Lewis
committed
add top visited and recently created
1 parent ffcf7fe commit aa77943

File tree

6 files changed

+104
-48
lines changed

6 files changed

+104
-48
lines changed

app/Http/Controllers/RedirectController.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5-
use Illuminate\Http\Request;
6-
use App\Models\ShortenedUrl; // Assuming you have a ShortenedUrl model
5+
use App\Models\ShortenedUrl;
76

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

1514
if ($url) {
15+
// Increment the visit count
16+
$url->increment('hits');
1617
// Redirect to the original URL
1718
return redirect()->away($url->original_url);
1819
} else {

app/Http/Controllers/ShortenController.php

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@
88

99
class ShortenController extends Controller
1010
{
11+
12+
protected $dates = ['created_at', 'updated_at'];
13+
14+
public function getTopVisitedShortCodes()
15+
{
16+
$topVisited = ShortenedUrl::orderBy('hits', 'DESC')->take(10)->get(['short_code', 'hits', 'original_url']);
17+
return view('welcome', compact('topVisited'));
18+
}
19+
20+
public function getRecentlyCreatedShortCodes()
21+
{
22+
$recentlyCreated = ShortenedUrl::orderBy('created_at', 'DESC')->take(10)->get(['short_code', 'original_url', 'created_at']);
23+
return view('welcome', compact('recentlyCreated'));
24+
}
25+
1126
public function shorten(Request $request)
1227
{
1328
// Validate the input
@@ -39,4 +54,11 @@ public function shorten(Request $request)
3954
]);
4055
}
4156
}
57+
58+
public function index()
59+
{
60+
$topVisited = ShortenedUrl::orderBy('hits', 'DESC')->take(10)->get(['short_code', 'original_url', 'hits']);
61+
$recentlyCreated = ShortenedUrl::orderBy('created_at', 'DESC')->take(10)->get(['short_code', 'original_url', 'created_at']);
62+
return view('welcome', compact('topVisited', 'recentlyCreated'));
63+
}
4264
}

public/css/app.css

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)