fix: videos/workshops query optmization
This commit is contained in:
@@ -40,15 +40,13 @@ class VideosController extends Controller
|
||||
$search = trim((string) $request->query('search', ''));
|
||||
$categoryId = $request->query('category');
|
||||
$watched = $request->query('watched');
|
||||
$status = $request->query('status');
|
||||
$perPage = $request->query('per_page', 9);
|
||||
|
||||
$query = Video::select(['id', 'title', 'thumbnail', 'is_active'])
|
||||
->with([
|
||||
'categories:id,name',
|
||||
'views' => function ($q) use ($user) {
|
||||
$q->select('id', 'video_id', 'user_id')
|
||||
->where('user_id', $user->id);
|
||||
}
|
||||
->with('categories:id,name')
|
||||
->withCount([
|
||||
'views as watched' => fn($q) => $q->where('user_id', $userId)
|
||||
])
|
||||
|
||||
->when($search !== '', function ($q) use ($search) {
|
||||
@@ -64,45 +62,48 @@ class VideosController extends Controller
|
||||
});
|
||||
})
|
||||
|
||||
->when($user->role_id !== 1, function ($q) {
|
||||
$q->where('is_active', true);
|
||||
->when($role !== 1, fn($q) => $q->where('is_active', true))
|
||||
|
||||
->when(
|
||||
$role === 1 && $status === 'active',
|
||||
fn($q) =>
|
||||
$q->where('is_active', true)
|
||||
)
|
||||
|
||||
->when(
|
||||
$role === 1 && $status === 'inactive',
|
||||
fn($q) =>
|
||||
$q->where('is_active', false)
|
||||
)
|
||||
|
||||
->when($watched !== null, function ($q) use ($watched, $userId) {
|
||||
(int) $watched === 1
|
||||
? $q->whereHas('views', fn($v) => $v->where('user_id', $userId))
|
||||
: $q->whereDoesntHave('views', fn($v) => $v->where('user_id', $userId));
|
||||
})
|
||||
|
||||
->when($watched !== null, function ($q) use ($watched, $user) {
|
||||
|
||||
if ((int) $watched === 1) {
|
||||
$q->whereHas('views', function ($v) use ($user) {
|
||||
$v->where('user_id', $user->id);
|
||||
});
|
||||
}
|
||||
|
||||
if ((int) $watched === 0) {
|
||||
$q->whereDoesntHave('views', function ($v) use ($user) {
|
||||
$v->where('user_id', $user->id);
|
||||
});
|
||||
}
|
||||
// left join para trazer os vídeos vistos pelo utilizador
|
||||
->leftJoin('video_views as vv', function ($join) use ($userId) {
|
||||
$join->on('vv.video_id', '=', 'videos.id')
|
||||
->where('vv.user_id', '=', $userId);
|
||||
})
|
||||
|
||||
->orderByRaw('
|
||||
CASE WHEN EXISTS (
|
||||
SELECT 1 FROM video_views
|
||||
WHERE video_views.video_id = videos.id
|
||||
AND video_views.user_id = ?
|
||||
) THEN 1 ELSE 0 END ASC,
|
||||
videos.order ASC
|
||||
', [$user->id])
|
||||
->orderByRaw('CASE WHEN vv.id IS NOT NULL THEN 1 ELSE 0 END ASC')
|
||||
->orderBy('videos.order', 'ASC')
|
||||
->select(['videos.id', 'videos.title', 'videos.thumbnail', 'videos.is_active'])
|
||||
|
||||
->paginate($perPage);
|
||||
|
||||
$videosActive = Video::select('id')
|
||||
->where('is_active', true)
|
||||
->count();
|
||||
|
||||
$videosWatched = Video::select('id')
|
||||
->where('is_active', true)
|
||||
->whereHas('views', function ($q) use ($user) {
|
||||
$q->where('user_id', $user->id);
|
||||
})
|
||||
->count();
|
||||
// 1 única query para obter o número de vídeos ativos e vistos
|
||||
$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])
|
||||
->first();
|
||||
|
||||
$query->getCollection()->transform(function ($video) {
|
||||
return [
|
||||
@@ -115,11 +116,17 @@ class VideosController extends Controller
|
||||
];
|
||||
});
|
||||
|
||||
$categories = Category::select('id', 'name')
|
||||
->where('is_active', true)
|
||||
->whereHas('videos')
|
||||
->orderBy('name', 'asc')
|
||||
->get();
|
||||
// Guarda as categorias em cache durante 1 minuto para evitar consultas repetidas
|
||||
$categories = cache()->remember(
|
||||
'active_categories_with_videos',
|
||||
60,
|
||||
fn() =>
|
||||
Category::select('id', 'name')
|
||||
->where('is_active', true)
|
||||
->whereHas('videos')
|
||||
->orderBy('name')
|
||||
->get()
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Vídeos obtidos com sucesso',
|
||||
@@ -133,55 +140,11 @@ class VideosController extends Controller
|
||||
'userId' => $userId,
|
||||
'role' => $role,
|
||||
'categories' => $categories,
|
||||
'videosActive' => $videosActive,
|
||||
'videosWatched' => $videosWatched,
|
||||
'videosActive' => $stats->active_count,
|
||||
'videosWatched' => $stats->watched_count ?? 0,
|
||||
]);
|
||||
}
|
||||
|
||||
public function nextVideos()
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
if (!$user) {
|
||||
return response()->json([
|
||||
'message' => 'Não autenticado',
|
||||
], 401);
|
||||
}
|
||||
|
||||
$videos = Video::select(['id', 'title', 'thumbnail', 'is_active', 'order'])
|
||||
->with(['categories:id,name'])
|
||||
|
||||
// 👇 regra base: vídeos não vistos
|
||||
->whereDoesntHave('views', function ($q) use ($user) {
|
||||
$q->where('user_id', $user->id);
|
||||
})
|
||||
|
||||
// 👇 users normais não veem inativos
|
||||
->when($user->role_id !== 1, function ($q) {
|
||||
$q->where('is_active', true);
|
||||
})
|
||||
|
||||
->orderBy('order', 'asc')
|
||||
->limit(3)
|
||||
->get()
|
||||
->map(function ($video) {
|
||||
return [
|
||||
'id' => $video->id,
|
||||
'title' => $video->title,
|
||||
'thumbnail' => $video->thumbnail,
|
||||
'is_active' => $video->is_active,
|
||||
'categories' => $video->categories,
|
||||
'watched' => false, // aqui já sabes que não foram vistos
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Dashboard videos',
|
||||
'data' => $videos,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function search(Request $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
@@ -222,30 +185,6 @@ class VideosController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function videosLength()
|
||||
{
|
||||
$user = auth()->user();
|
||||
$userID = $user->id;
|
||||
|
||||
if ($user->role_id !== 1) {
|
||||
$videos = Video::select('id')->with('views')->where('is_active', true)->count();
|
||||
$videosWatched = Video::with('views')->where('is_active', true)->whereHas('views', function ($query) use ($userID) {
|
||||
$query->where('user_id', $userID);
|
||||
})->count();
|
||||
} else {
|
||||
$videos = Video::select('id')->with('views')->where('is_active', true)->count();
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Vídeos ativos obtidos com sucesso',
|
||||
'data' => [
|
||||
'videos' => $videos,
|
||||
'videosWatched' => $videosWatched ?? 0,
|
||||
],
|
||||
'errors' => null,
|
||||
], 200);
|
||||
}
|
||||
|
||||
public function getVideo($id)
|
||||
{
|
||||
$user = auth()->user();
|
||||
@@ -286,7 +225,6 @@ class VideosController extends Controller
|
||||
|
||||
$video = Video::with('categories')->find($id);
|
||||
|
||||
|
||||
if ($video) {
|
||||
$video->url = Storage::url($video->url);
|
||||
$video->thumbnail = Storage::url($video->thumbnail);
|
||||
|
||||
Reference in New Issue
Block a user