feat: paginate workshops and videos pages
This commit is contained in:
@@ -25,42 +25,196 @@ class VideosController extends Controller
|
||||
public function index(Request $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
|
||||
if (!$user) {
|
||||
return response()->json([
|
||||
'message' => 'Utilizador não autenticado',
|
||||
'data' => null,
|
||||
'errors' => null,
|
||||
], 404);
|
||||
], 401);
|
||||
}
|
||||
|
||||
|
||||
$search = trim((string) $request->query('search', ''));
|
||||
$videosQuery = Video::with('categories');
|
||||
$categoryId = $request->query('category');
|
||||
$watched = $request->query('watched');
|
||||
$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);
|
||||
}
|
||||
])
|
||||
|
||||
->when($search !== '', function ($q) use ($search) {
|
||||
$q->where(function ($sub) use ($search) {
|
||||
$sub->where('title', 'like', "%{$search}%")
|
||||
->orWhere('tags', 'like', "%{$search}%");
|
||||
});
|
||||
})
|
||||
|
||||
->when($categoryId, function ($q) use ($categoryId) {
|
||||
$q->whereHas('categories', function ($c) use ($categoryId) {
|
||||
$c->where('categories.id', $categoryId);
|
||||
});
|
||||
})
|
||||
|
||||
->when($user->role_id !== 1, function ($q) {
|
||||
$q->where('is_active', true);
|
||||
})
|
||||
|
||||
->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);
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
->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])
|
||||
->paginate($perPage);
|
||||
|
||||
$query->getCollection()->transform(function ($video) {
|
||||
return [
|
||||
'id' => $video->id,
|
||||
'title' => $video->title,
|
||||
'thumbnail' => $video->thumbnail,
|
||||
'is_active' => $video->is_active,
|
||||
'categories' => $video->categories,
|
||||
'watched' => $video->views->isNotEmpty(),
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Vídeos obtidos com sucesso',
|
||||
'data' => $query->items(),
|
||||
'meta' => [
|
||||
'current_page' => $query->currentPage(),
|
||||
'last_page' => $query->lastPage(),
|
||||
'per_page' => $query->perPage(),
|
||||
'total' => $query->total(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
if ($search !== '') {
|
||||
$videosQuery->where(function ($query) use ($search) {
|
||||
$query->where('title', 'like', "%{$search}%")
|
||||
->orWhere('tags', 'like', "%{$search}%");
|
||||
});
|
||||
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();
|
||||
|
||||
$search = trim((string) $request->query('search', ''));
|
||||
|
||||
$videos = Video::select(['id', 'title', 'thumbnail', 'is_active'])
|
||||
->with([
|
||||
'categories:id,name',
|
||||
'views' => function ($query) use ($user) {
|
||||
$query->select('id', 'video_id', 'user_id')
|
||||
->where('user_id', $user->id);
|
||||
}
|
||||
])
|
||||
->when($user->role_id !== 1, function ($query) {
|
||||
$query->where('is_active', true);
|
||||
})
|
||||
->where(function ($query) use ($search) {
|
||||
$query->where('title', 'like', "%{$search}%")
|
||||
->orWhere('tags', 'like', "%{$search}%");
|
||||
})
|
||||
->limit(20)
|
||||
->get()
|
||||
->map(function ($video) {
|
||||
return [
|
||||
'id' => $video->id,
|
||||
'title' => $video->title,
|
||||
'thumbnail' => $video->thumbnail,
|
||||
'is_active' => $video->is_active,
|
||||
'categories' => $video->categories,
|
||||
'watched' => $video->views->isNotEmpty(),
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Resultados obtidos com sucesso',
|
||||
'data' => $videos,
|
||||
]);
|
||||
}
|
||||
|
||||
public function videosLength()
|
||||
{
|
||||
$user = auth()->user();
|
||||
$userID = $user->id;
|
||||
|
||||
if ($user->role_id !== 1) {
|
||||
$videosQuery->where('is_active', true);
|
||||
}
|
||||
|
||||
$videos = $videosQuery->get();
|
||||
|
||||
if ($videos->isEmpty()) {
|
||||
return response()->json([
|
||||
'message' => $search !== '' ? 'Sem resultados para a pesquisa' : 'Não foram encontrados vídeos',
|
||||
'data' => [],
|
||||
'errors' => null,
|
||||
], $search !== '' ? 200 : 404); // 200 se for pesquisa, 404 se for listagem normal
|
||||
$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 obtidos com sucesso',
|
||||
'data' => $videos,
|
||||
'message' => 'Vídeos ativos obtidos com sucesso',
|
||||
'data' => [
|
||||
'videos' => $videos,
|
||||
'videosWatched' => $videosWatched ?? 0,
|
||||
],
|
||||
'errors' => null,
|
||||
], 200);
|
||||
}
|
||||
@@ -77,8 +231,18 @@ class VideosController extends Controller
|
||||
], 404);
|
||||
}
|
||||
|
||||
$userID = $user->id;
|
||||
|
||||
if ($user->role_id !== 1) {
|
||||
$video = Video::with('categories')->where('is_active', true)->find($id);
|
||||
/* $video = Video::with('categories')->where('is_active', true)->find($id); */
|
||||
$video = Video::with([
|
||||
'categories' => function ($query) use ($userID) {
|
||||
$query->where('is_active', true);
|
||||
},
|
||||
'views' => function ($query) use ($userID) {
|
||||
$query->where('user_id', $userID);
|
||||
}
|
||||
])->find($id);
|
||||
|
||||
/* Para não mostrar vídeos inactivos para utilizadores não administradores */
|
||||
if (!$video || $video->is_active === false) {
|
||||
@@ -106,6 +270,7 @@ class VideosController extends Controller
|
||||
'thumbnail' => $video->thumbnail,
|
||||
'duration' => $video->duration,
|
||||
'tags' => $video->tags,
|
||||
'order' => $video->order,
|
||||
'categories' => $video->categories->map(function ($category) {
|
||||
return [
|
||||
'id' => $category->id,
|
||||
@@ -113,6 +278,7 @@ class VideosController extends Controller
|
||||
];
|
||||
})->values(),
|
||||
'is_active' => $video->is_active,
|
||||
'watched' => $video->views->isNotEmpty(),
|
||||
],
|
||||
'errors' => null,
|
||||
], 200);
|
||||
@@ -150,6 +316,7 @@ class VideosController extends Controller
|
||||
'thumbnail' => $thumbnailPath,
|
||||
'duration' => $validated['duration'] ?? '00:00',
|
||||
'tags' => $validated['tags'],
|
||||
'order' => $validated['order'],
|
||||
]);
|
||||
|
||||
if ($request->filled('category_ids')) {
|
||||
@@ -159,7 +326,7 @@ class VideosController extends Controller
|
||||
$baseUrl = $request->getSchemeAndHttpHost();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Vídeo criado com sucesso',
|
||||
'message' => 'Vídeo adicionado com sucesso',
|
||||
'data' => [
|
||||
'id' => $video->id,
|
||||
'title' => $video->title,
|
||||
@@ -168,6 +335,7 @@ class VideosController extends Controller
|
||||
'thumbnail' => $baseUrl . Storage::url($video->thumbnail),
|
||||
'duration' => $video->duration,
|
||||
'tags' => $video->tags,
|
||||
'order' => $video->order,
|
||||
'categories' => $video->categories->pluck('name'),
|
||||
'is_active' => $video->is_active,
|
||||
],
|
||||
@@ -215,6 +383,7 @@ class VideosController extends Controller
|
||||
'tags' => $validated['tags'] ?? $videoToUpdate->tags,
|
||||
'thumbnail' => $validated['thumbnail'] ?? $videoToUpdate->thumbnail,
|
||||
'is_active' => array_key_exists('is_active', $validated) ? $validated['is_active'] : $videoToUpdate->is_active,
|
||||
'order' => $validated['order'] ?? $videoToUpdate->order,
|
||||
]);
|
||||
|
||||
if ($request->has('category_ids')) {
|
||||
|
||||
Reference in New Issue
Block a user