Feat: Organize videos by sector

This commit is contained in:
Xavier Oliveira
2026-06-16 12:25:58 +01:00
parent c5ed77945e
commit 84e9bf7307
21 changed files with 477 additions and 116 deletions

View File

@@ -105,16 +105,33 @@ class AuthController extends Controller
$user = auth()->user();
$userId = $user->id;
$role = $user->role_id;
$userSector = $user->sector_id;
if ($role !== 1) {
$stats = Video::query()
->where('is_active', true)
->whereHas('sectors', function ($s) use ($userSector) {
$s->where('sectors.id', $userSector)
->orWhere('sectors.slug', "global");
})
->selectRaw('
COUNT(*) as active_count,
COUNT(CASE WHEN EXISTS (
SELECT 1 FROM video_views
WHERE video_views.video_id = videos.id
AND video_views.user_id = ?
) THEN 1 END) as watched_count
', [$userId])
->first();
} else {
$stats = Video::selectRaw('
COUNT(CASE WHEN is_active = 1 THEN 1 END) as active_count,
COUNT(CASE WHEN is_active = 1 AND EXISTS (
SELECT 1 FROM video_views
WHERE video_views.video_id = videos.id
AND video_views.user_id = ?
) THEN 1 END) as watched_count
', [$userId])
COUNT(CASE WHEN is_active = 1 THEN 1 END) as active_count,
COUNT(CASE WHEN is_active = 1 AND EXISTS (
SELECT 1 FROM video_views
WHERE video_views.video_id = videos.id
AND video_views.user_id = ?
) THEN 1 END) as watched_count
', [$userId])
->first();
}