Current state
This commit is contained in:
129
app/Http/Controllers/Legacy/TopAuthorsController.php
Normal file
129
app/Http/Controllers/Legacy/TopAuthorsController.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Legacy;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Models\Artwork;
|
||||
use App\Models\ArtworkStats;
|
||||
use App\Models\User;
|
||||
|
||||
class TopAuthorsController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$perPage = 20;
|
||||
$metric = strtolower($request->query('metric', 'views'));
|
||||
|
||||
if (! in_array($metric, ['views', 'downloads'])) {
|
||||
$metric = 'views';
|
||||
}
|
||||
|
||||
// Aggregate artwork_stats grouped by artwork.user_id, filtering only public+approved+published artworks
|
||||
$sub = Artwork::query()
|
||||
->select('artworks.user_id')
|
||||
->join('artwork_stats', 'artwork_stats.artwork_id', '=', 'artworks.id')
|
||||
->where('artworks.is_public', true)
|
||||
->where('artworks.is_approved', true)
|
||||
->whereNotNull('artworks.published_at')
|
||||
->where('artworks.published_at', '<=', now())
|
||||
->whereNull('artworks.deleted_at')
|
||||
->selectRaw('artworks.user_id, SUM(artwork_stats.' . $metric . ') as total_metric, MAX(artworks.published_at) as latest_published')
|
||||
->groupBy('artworks.user_id');
|
||||
|
||||
// Join with users to fetch profile info
|
||||
$query = DB::table(DB::raw('(' . $sub->toSql() . ') as t'))
|
||||
->mergeBindings($sub->getQuery())
|
||||
->join('users as u', 'u.id', '=', 't.user_id')
|
||||
->select('u.id as user_id', 'u.name as uname', 'u.username', 't.total_metric', 't.latest_published')
|
||||
->orderByDesc('t.total_metric')
|
||||
->orderByDesc('t.latest_published');
|
||||
|
||||
$authors = $query->paginate($perPage)->withQueryString();
|
||||
|
||||
// Map to legacy view shape
|
||||
$authors->getCollection()->transform(function ($row) use ($metric) {
|
||||
return (object) [
|
||||
'user_id' => $row->user_id,
|
||||
'uname' => $row->uname,
|
||||
'username' => $row->username,
|
||||
'total' => (int) $row->total_metric,
|
||||
'metric' => $metric,
|
||||
];
|
||||
});
|
||||
|
||||
$page_title = 'Top Authors';
|
||||
|
||||
return view('legacy.top-authors', compact('page_title', 'authors', 'metric'));
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Legacy;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class TopAuthorsController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
// Top users (most active)
|
||||
try {
|
||||
$topUsers = DB::connection('legacy')->table('wallz as t1')
|
||||
->leftJoin('users as t2', 't1.user_id', '=', 't2.user_id')
|
||||
->select('t2.user_id', 't2.uname', 't2.icon', DB::raw('SUM(t1.dls) AS total_downloads'), DB::raw('COUNT(*) AS uploads'))
|
||||
->groupBy('t1.user_id')
|
||||
->orderByDesc('total_downloads')
|
||||
->limit(23)
|
||||
->get();
|
||||
} catch (\Throwable $e) {
|
||||
$topUsers = collect();
|
||||
}
|
||||
|
||||
// Top followers
|
||||
try {
|
||||
$topFollowers = DB::connection('legacy')->table('friends_list as t1')
|
||||
->rightJoin('users as t2', 't1.friend_id', '=', 't2.user_id')
|
||||
->where('t1.friend_id', '>', 0)
|
||||
->select('t2.uname', 't2.user_id', DB::raw('COUNT(*) as num'))
|
||||
->groupBy('t1.friend_id')
|
||||
->orderByDesc('num')
|
||||
->limit(10)
|
||||
->get();
|
||||
} catch (\Throwable $e) {
|
||||
$topFollowers = collect();
|
||||
}
|
||||
|
||||
// Top commentators
|
||||
try {
|
||||
$topCommentators = DB::connection('legacy')->table('artworks_comments as t1')
|
||||
->join('users as t2', 't1.user_id', '=', 't2.user_id')
|
||||
->where('t1.user_id', '>', 0)
|
||||
->select('t2.user_id','t2.uname','t2.user_type','t2.country', DB::raw('COUNT(*) as num_comments'))
|
||||
->groupBy('t1.user_id')
|
||||
->orderByDesc('num_comments')
|
||||
->limit(10)
|
||||
->get();
|
||||
|
||||
// enrich with country info if available
|
||||
$topCommentators->transform(function ($c) {
|
||||
if (!empty($c->country)) {
|
||||
$cn = DB::connection('legacy')->table('country')->select('name','flag')->where('id', $c->country)->first();
|
||||
$c->country_name = $cn->name ?? null;
|
||||
$c->country_flag = $cn->flag ?? null;
|
||||
} else {
|
||||
$c->country_name = null;
|
||||
$c->country_flag = null;
|
||||
}
|
||||
return $c;
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
$topCommentators = collect();
|
||||
}
|
||||
|
||||
return view('legacy.top-authors', compact('topUsers', 'topFollowers', 'topCommentators'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user