114 lines
2.9 KiB
PHP
114 lines
2.9 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Http\Controllers\Api;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Http\Requests\StoreProjectRequest;
|
||
|
|
use App\Http\Requests\UpdateProjectRequest;
|
||
|
|
use App\Http\Resources\IdeaResource;
|
||
|
|
use App\Http\Resources\ProjectResource;
|
||
|
|
use App\Http\Resources\TaskResource;
|
||
|
|
use App\Models\Project;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
|
||
|
|
class ProjectController extends Controller
|
||
|
|
{
|
||
|
|
public function index(Request $request)
|
||
|
|
{
|
||
|
|
$query = Project::query();
|
||
|
|
|
||
|
|
if ($request->filled('status')) {
|
||
|
|
$query->where('status', $request->string('status'));
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($request->filled('name')) {
|
||
|
|
$query->where('name', 'like', '%' . $request->string('name') . '%');
|
||
|
|
}
|
||
|
|
|
||
|
|
$projects = $query->orderBy('name')->get();
|
||
|
|
|
||
|
|
// Eager load tasks if ?with_tasks=1
|
||
|
|
if ($request->boolean('with_tasks')) {
|
||
|
|
$projects->load(['tasks.comments']);
|
||
|
|
}
|
||
|
|
|
||
|
|
return ProjectResource::collection($projects);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store(StoreProjectRequest $request)
|
||
|
|
{
|
||
|
|
$project = Project::create($request->validated());
|
||
|
|
|
||
|
|
return (new ProjectResource($project))
|
||
|
|
->response()
|
||
|
|
->setStatusCode(201);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function show(Request $request, Project $project)
|
||
|
|
{
|
||
|
|
if ($request->boolean('with_tasks')) {
|
||
|
|
$project->load(['tasks.comments']);
|
||
|
|
}
|
||
|
|
|
||
|
|
return new ProjectResource($project);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update(UpdateProjectRequest $request, Project $project)
|
||
|
|
{
|
||
|
|
$project->update($request->validated());
|
||
|
|
|
||
|
|
return new ProjectResource($project);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function destroy(Project $project)
|
||
|
|
{
|
||
|
|
$project->delete();
|
||
|
|
|
||
|
|
return response()->noContent();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get tasks for a project.
|
||
|
|
*/
|
||
|
|
public function tasks(Request $request, Project $project)
|
||
|
|
{
|
||
|
|
$query = $project->tasks()->with(['comments']);
|
||
|
|
|
||
|
|
if ($request->filled('status')) {
|
||
|
|
$query->where('status', $request->string('status'));
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($request->filled('priority')) {
|
||
|
|
$query->where('priority', $request->integer('priority'));
|
||
|
|
}
|
||
|
|
|
||
|
|
$tasks = $query->orderBy('priority', 'desc')->orderBy('created_at')->get();
|
||
|
|
|
||
|
|
return TaskResource::collection($tasks);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get ideas for a project.
|
||
|
|
*/
|
||
|
|
public function ideas(Request $request, Project $project)
|
||
|
|
{
|
||
|
|
$query = $project->ideas()->with(['user']);
|
||
|
|
|
||
|
|
if ($request->filled('status')) {
|
||
|
|
$query->where('status', $request->string('status'));
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($request->filled('category')) {
|
||
|
|
$query->where('category', $request->string('category'));
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($request->filled('priority')) {
|
||
|
|
$query->where('priority', $request->integer('priority'));
|
||
|
|
}
|
||
|
|
|
||
|
|
$ideas = $query->orderBy('priority', 'desc')->orderBy('created_at', 'desc')->get();
|
||
|
|
|
||
|
|
return IdeaResource::collection($ideas);
|
||
|
|
}
|
||
|
|
}
|