mirror of https://github.com/Devoalda/LaDo.git
parent
da2715f240
commit
2266976361
|
@ -4,8 +4,10 @@ namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Http\Requests\Project\StoreProjectRequest;
|
use App\Http\Requests\Project\StoreProjectRequest;
|
||||||
use App\Http\Requests\Project\UpdateProjectRequest;
|
use App\Http\Requests\Project\UpdateProjectRequest;
|
||||||
|
use App\Http\Resources\ProjectResource;
|
||||||
use App\Models\Project;
|
use App\Models\Project;
|
||||||
use App\Models\Todo;
|
use App\Models\Todo;
|
||||||
|
use App\Models\User;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Auth\Access\AuthorizationException;
|
use Illuminate\Auth\Access\AuthorizationException;
|
||||||
use Illuminate\Contracts\Foundation\Application;
|
use Illuminate\Contracts\Foundation\Application;
|
||||||
|
@ -14,8 +16,6 @@ use Illuminate\Contracts\View\View;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\RedirectResponse;
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Models\User;
|
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
|
||||||
use Illuminate\Support\Facades\Response;
|
use Illuminate\Support\Facades\Response;
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,8 +24,17 @@ class ProjectController extends Controller
|
||||||
/**
|
/**
|
||||||
* Display Listing of all Projects.
|
* Display Listing of all Projects.
|
||||||
*/
|
*/
|
||||||
public function index(Request $request): Application|Factory|View|JsonResponse
|
public function index(Request $request): View|Factory|Application|JsonResponse|ProjectResource
|
||||||
{
|
{
|
||||||
|
// Check if API Call, get userID from request
|
||||||
|
if ($request->is('api/*')) {
|
||||||
|
$user = User::find($request->user_id);
|
||||||
|
$this->authorize('viewAny', $user);
|
||||||
|
|
||||||
|
$projects = $user->projects()->paginate(4);
|
||||||
|
return new ProjectResource($projects);
|
||||||
|
}
|
||||||
|
|
||||||
$user = User::find(auth()->user()->id);
|
$user = User::find(auth()->user()->id);
|
||||||
$projects = $user->projects()->paginate(4);
|
$projects = $user->projects()->paginate(4);
|
||||||
// Aggregate all todos for all projects
|
// Aggregate all todos for all projects
|
||||||
|
@ -63,10 +72,24 @@ class ProjectController extends Controller
|
||||||
/**
|
/**
|
||||||
* Store a newly created project in storage.
|
* Store a newly created project in storage.
|
||||||
*/
|
*/
|
||||||
public function store(StoreProjectRequest $request): RedirectResponse
|
public function store(StoreProjectRequest $request): RedirectResponse|JsonResponse
|
||||||
{
|
{
|
||||||
$data = $request->validated();
|
$data = $request->validated();
|
||||||
|
|
||||||
|
// Check if API Call, get userID from request
|
||||||
|
if ($request->is('api/*')) {
|
||||||
|
$user = User::find($request->user_id);
|
||||||
|
if (!$user) {
|
||||||
|
return response()->json(['error' => 'User not found'], 404);
|
||||||
|
}
|
||||||
|
$user->projects()->create($data);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Project created successfully',
|
||||||
|
'data' => $data,
|
||||||
|
], 201);
|
||||||
|
}
|
||||||
|
|
||||||
auth()->user()->projects()->create($data);
|
auth()->user()->projects()->create($data);
|
||||||
|
|
||||||
return redirect()->route('project.index')
|
return redirect()->route('project.index')
|
||||||
|
@ -77,8 +100,32 @@ class ProjectController extends Controller
|
||||||
* Display the specified resource.
|
* Display the specified resource.
|
||||||
* @throws AuthorizationException
|
* @throws AuthorizationException
|
||||||
*/
|
*/
|
||||||
public function show(Project $project): RedirectResponse
|
public function show(Request $request, $project_id): RedirectResponse|JsonResponse
|
||||||
{
|
{
|
||||||
|
// Check if API Call, get userID from request
|
||||||
|
if ($request->is('api/*')) {
|
||||||
|
$user = User::find($request->user_id);
|
||||||
|
if (!$user) {
|
||||||
|
return response()->json(['error' => 'User not found'], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
$project = $user->projects()->find($project_id);
|
||||||
|
|
||||||
|
if (!$project) {
|
||||||
|
return response()->json([
|
||||||
|
'error' => 'Project not found',
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Project retrieved successfully',
|
||||||
|
'data' => $project,
|
||||||
|
], 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Non-API request handling
|
||||||
|
$project = Project::findOrFail($project_id); // Assumes the Project model is imported
|
||||||
|
|
||||||
$this->authorize('view', $project);
|
$this->authorize('view', $project);
|
||||||
|
|
||||||
return redirect()->route('project.index');
|
return redirect()->route('project.index');
|
||||||
|
@ -99,12 +146,44 @@ class ProjectController extends Controller
|
||||||
/**
|
/**
|
||||||
* Update the specified Project in storage.
|
* Update the specified Project in storage.
|
||||||
*/
|
*/
|
||||||
public function update(UpdateProjectRequest $request, Project $project): RedirectResponse
|
public function update(UpdateProjectRequest $request, $project_id): RedirectResponse|JsonResponse
|
||||||
{
|
{
|
||||||
$this->authorize('update', $project);
|
|
||||||
|
|
||||||
$data = $request->validatedWithCompletedAt();
|
$data = $request->validatedWithCompletedAt();
|
||||||
|
|
||||||
|
// API Call
|
||||||
|
if ($request->is('api/*')) {
|
||||||
|
$user = User::find($request->user_id);
|
||||||
|
if (!$user) {
|
||||||
|
return response()->json(['error' => 'User not found'], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
$project = $user->projects()->find($project_id);
|
||||||
|
|
||||||
|
if (!$project) {
|
||||||
|
return response()->json([
|
||||||
|
'error' => 'Project not found',
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
$project->update($data);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Project updated successfully',
|
||||||
|
'data' => $project,
|
||||||
|
], 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
$project = Project::find($project_id);
|
||||||
|
|
||||||
|
if (!$project) {
|
||||||
|
return response()->json([
|
||||||
|
'error' => 'Project not found',
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->authorize('update', $project);
|
||||||
|
|
||||||
$project->update($data);
|
$project->update($data);
|
||||||
|
|
||||||
// Complete all todos in project
|
// Complete all todos in project
|
||||||
|
@ -117,13 +196,42 @@ class ProjectController extends Controller
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the specified Project from storage.
|
* Remove the specified Project from storage.
|
||||||
|
* @throws AuthorizationException
|
||||||
*/
|
*/
|
||||||
public function destroy(Project $project)
|
public function destroy($project_id, Request $request): RedirectResponse|JsonResponse
|
||||||
{
|
{
|
||||||
|
// Check if API Call and $project_id is provided
|
||||||
|
if ($request->is('api/*')) {
|
||||||
|
$user_id = $request->user_id;
|
||||||
|
$user = User::find($user_id);
|
||||||
|
if (!$user) {
|
||||||
|
return response()->json(['error' => 'User not found'], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
$project = $user->projects()->find($project_id);
|
||||||
|
|
||||||
|
if (!$project) {
|
||||||
|
return response()->json([
|
||||||
|
'error' => 'Project not found',
|
||||||
|
'data' => $request->all(),
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
$project->delete();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Project deleted successfully',
|
||||||
|
], 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Non-API request handling
|
||||||
|
$project = Project::findOrFail($project_id);
|
||||||
|
|
||||||
$this->authorize('delete', $project);
|
$this->authorize('delete', $project);
|
||||||
|
|
||||||
$project->delete();
|
$project->delete();
|
||||||
|
|
||||||
return redirect()->route('project.index');
|
return redirect()->route('project.index');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,10 @@ class StoreProjectRequest extends FormRequest
|
||||||
*/
|
*/
|
||||||
public function authorize(): bool
|
public function authorize(): bool
|
||||||
{
|
{
|
||||||
|
if ($this->is('api/*')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return auth()->check();
|
return auth()->check();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,10 @@ class UpdateProjectRequest extends FormRequest
|
||||||
*/
|
*/
|
||||||
public function authorize(): bool
|
public function authorize(): bool
|
||||||
{
|
{
|
||||||
|
if ($this->is('api/*')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return auth()->check();
|
return auth()->check();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||||
|
|
||||||
|
class ProjectResource extends ResourceCollection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource collection into an array.
|
||||||
|
*
|
||||||
|
* @return array<int|string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'data' => $this->collection,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,7 +17,7 @@ class RouteServiceProvider extends ServiceProvider
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public const HOME = '/project';
|
public const HOME = '/dashboard';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define your route model bindings, pattern filters, and other route configuration.
|
* Define your route model bindings, pattern filters, and other route configuration.
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Http\Controllers\ProjectController;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
|
@ -17,3 +18,6 @@ use Illuminate\Support\Facades\Route;
|
||||||
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
|
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
|
||||||
return $request->user();
|
return $request->user();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Resources route to /api/projects
|
||||||
|
Route::apiResource('projects', ProjectController::class);
|
||||||
|
|
Loading…
Reference in New Issue