feat: paginate workshops and videos pages
This commit is contained in:
@@ -108,8 +108,6 @@ class AuthController extends Controller
|
||||
'message' => 'Utilizador obtido com sucesso',
|
||||
'data' => [
|
||||
'id' => $user->id,
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'role_id' => $user->role_id,
|
||||
],
|
||||
'errors' => null,
|
||||
|
||||
@@ -10,7 +10,7 @@ class CategoryController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$categories = Category::all();
|
||||
$categories = Category::select('id', 'name', 'is_active')->orderBy('name', 'asc')->get();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Categorias obtidas com sucesso',
|
||||
@@ -21,7 +21,7 @@ class CategoryController extends Controller
|
||||
|
||||
public function create(CreateCategoryRequest $request)
|
||||
{
|
||||
$category = Category::create($request->all());
|
||||
$category = Category::create($request->validated());
|
||||
return response()->json([
|
||||
'message' => 'Categoria criada com sucesso',
|
||||
'data' => $category,
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Video;
|
||||
use App\Models\VideoView;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class VideoViewController extends Controller
|
||||
{
|
||||
public function store(Video $video) {
|
||||
$user = auth()->user();
|
||||
|
||||
//firstOrCreate só cria se não existir, se existir, atualiza o watched_at
|
||||
VideoView::firstOrCreate([
|
||||
'user_id' => $user->id,
|
||||
'video_id' => $video->id,
|
||||
], [
|
||||
'watched_at' => now(),
|
||||
]);
|
||||
|
||||
return response()->json(['watched' => true]);
|
||||
}
|
||||
}
|
||||
@@ -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')) {
|
||||
|
||||
@@ -24,49 +24,153 @@ class WorkshopsController extends Controller
|
||||
|
||||
$search = trim((string) $request->query('search', ''));
|
||||
$status = trim((string) $request->query('status', ''));
|
||||
$workshopsQuery = Workshop::with('users');
|
||||
|
||||
if ($search !== '') {
|
||||
$workshopsQuery->where('title', 'like', "%{$search}%")->where('status', 'pending');
|
||||
}
|
||||
|
||||
if ($user->role_id !== 1) {
|
||||
$workshopsQuery->where(function ($query) use ($user) {
|
||||
$query->where('status', 'pending')
|
||||
->orWhere(function ($q) use ($user) {
|
||||
$q->whereIn('status', ['pending'])
|
||||
->whereHas('users', function ($q2) use ($user) {
|
||||
$q2->where('users.id', $user->id);
|
||||
});
|
||||
})
|
||||
->orWhere(function ($q) use ($user) {
|
||||
$q->whereIn('status', ['realized', 'canceled'])
|
||||
->whereHas('users', function ($q2) use ($user) {
|
||||
$q2->where('users.id', $user->id);
|
||||
});
|
||||
});
|
||||
$perPage = $request->query('per_page', 9);
|
||||
$query = Workshop::select('id', 'title', 'image', 'date', 'time_start', 'time_end', 'status')->with('users:id')
|
||||
->when($search !== '', function ($q) use ($search) {
|
||||
$q->where('title', 'like', "%{$search}%");
|
||||
})
|
||||
->when($status !== '' && $status !== 'inscrito', function ($q) use ($status) {
|
||||
$q->where('status', $status);
|
||||
})
|
||||
->when($status === 'inscrito', function ($q) use ($user) {
|
||||
$q->where('status', 'pending')
|
||||
->whereHas('users', function ($q2) use ($user) {
|
||||
$q2->where('users.id', $user->id);
|
||||
});
|
||||
}
|
||||
})
|
||||
->when($user->role_id !== 1, function ($q) use ($user, $status) {
|
||||
if($status === 'pending'){
|
||||
|
||||
$workshops = $workshopsQuery->orderBy('date', 'asc')->orderBy('time_start', 'asc')->get();
|
||||
} else {
|
||||
$q->whereHas('users', function ($q2) use ($user) {
|
||||
$q2->where('users.id', $user->id);
|
||||
});
|
||||
};
|
||||
})
|
||||
->orderBy('date', 'asc')
|
||||
->orderBy('time_start', 'asc')
|
||||
->paginate($perPage);
|
||||
|
||||
/* Os workshops são atualizados automaticamente pelo command (app->Console->Commands->UpdateWorkshopStatus.php) 'workshops:update-status' de pending para realized se for uma data passada */
|
||||
|
||||
if($workshops->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
|
||||
}
|
||||
$query->getCollection()->transform(function ($workshop) {
|
||||
return [
|
||||
'id' => $workshop->id,
|
||||
'title' => $workshop->title,
|
||||
'image' => $workshop->image,
|
||||
'date' => $workshop->date,
|
||||
'time_start' => $workshop->time_start,
|
||||
'time_end' => $workshop->time_end,
|
||||
'status' => $workshop->status,
|
||||
'users' => $workshop->users,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Workshops obtidos com sucesso',
|
||||
'data' => $query->items(),
|
||||
'meta' => [
|
||||
'current_page' => $query->currentPage(),
|
||||
'last_page' => $query->lastPage(),
|
||||
'per_page' => $query->perPage(),
|
||||
'total' => $query->total(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function search(Request $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
$search = trim((string) $request->query('search', ''));
|
||||
|
||||
$workshops = Workshop::select(['id', 'title', 'image', 'date', 'time_start', 'time_end', 'status'])
|
||||
->when($user->role_id !== 1, function ($query) {
|
||||
$query->where('status', 'pending');
|
||||
})
|
||||
->where(function ($query) use ($search) {
|
||||
$query->where('title', 'like', "%{$search}%");
|
||||
})
|
||||
->orderBy('date', 'asc')
|
||||
->orderBy('time_start', 'asc')
|
||||
->limit(20)
|
||||
->get()
|
||||
->map(function ($workshop) {
|
||||
return [
|
||||
'id' => $workshop->id,
|
||||
'title' => $workshop->title,
|
||||
'image' => $workshop->image,
|
||||
'date' => $workshop->date,
|
||||
'time_start' => $workshop->time_start,
|
||||
'time_end' => $workshop->time_end,
|
||||
'status' => $workshop->status,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Resultados obtidos com sucesso',
|
||||
'data' => $workshops,
|
||||
]);
|
||||
}
|
||||
|
||||
public function workshopsLength()
|
||||
{
|
||||
$user = auth()->user();
|
||||
$userID = $user->id;
|
||||
|
||||
if ($user->role_id !== 1) {
|
||||
$workshops = Workshop::select('id')->where('status', 'pending')->count();
|
||||
$workshopsInscribed = Workshop::with('users')->where('status', 'pending')->whereHas('users', function ($query) use ($userID) {
|
||||
$query->where('user_id', $userID);
|
||||
})->count();
|
||||
} else {
|
||||
$workshops = Workshop::select('id')->where('status', 'pending')->count();
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Vídeos ativos obtidos com sucesso',
|
||||
'data' => [
|
||||
'workshops' => $workshops,
|
||||
'workshopsInscribed' => $workshopsInscribed ?? 0,
|
||||
],
|
||||
'errors' => null,
|
||||
], 200);
|
||||
}
|
||||
|
||||
public function nextWorkshops()
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
if (!$user) {
|
||||
return response()->json([
|
||||
'message' => 'Não autenticado',
|
||||
], 401);
|
||||
}
|
||||
|
||||
$workshops = Workshop::select(['id', 'title', 'image', 'date', 'time_start', 'time_end', 'status'])
|
||||
->with(['users:id'])
|
||||
->where('status', 'pending')
|
||||
->orderBy('date', 'asc')
|
||||
->orderBy('time_start', 'asc')
|
||||
->limit(3)
|
||||
->get()
|
||||
->map(function ($workshop) {
|
||||
return [
|
||||
'id' => $workshop->id,
|
||||
'title' => $workshop->title,
|
||||
'image' => $workshop->image,
|
||||
'date' => $workshop->date,
|
||||
'time_start' => $workshop->time_start,
|
||||
'time_end' => $workshop->time_end,
|
||||
'status' => $workshop->status,
|
||||
'users' => $workshop->users->pluck('id'),
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Dashboard workshops',
|
||||
'data' => $workshops,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getWorkshop($id)
|
||||
{
|
||||
$workshop = Workshop::with('users')->find($id);
|
||||
@@ -98,10 +202,9 @@ class WorkshopsController extends Controller
|
||||
}
|
||||
|
||||
$validated = $request->validated();
|
||||
try {
|
||||
|
||||
$imagePath = $request->file('image')->store('imageWorkshops', 'public');
|
||||
|
||||
try {
|
||||
$workshop = Workshop::create([
|
||||
'title' => $validated['title'],
|
||||
'description' => $validated['description'],
|
||||
|
||||
Reference in New Issue
Block a user