104 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			104 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
declare(strict_types=1);
 | 
						|
 | 
						|
namespace App\Http\Controllers;
 | 
						|
 | 
						|
use App\Http\Requests\Category\DestroyCategoryRequest;
 | 
						|
use App\Http\Requests\Category\ListCategoryRequest;
 | 
						|
use App\Http\Requests\Category\ShowCategoryRequest;
 | 
						|
use App\Http\Requests\Category\StoreCategoryRequest;
 | 
						|
use App\Http\Requests\Category\UpdateCategoryRequest;
 | 
						|
use App\Http\Resources\CategoryCollection;
 | 
						|
use App\Http\Resources\CategoryResource;
 | 
						|
use App\Http\Resources\PaginableResource;
 | 
						|
use App\Services\Category\CategoryServiceInterface;
 | 
						|
use Illuminate\Http\JsonResponse;
 | 
						|
use OpenApi\Annotations as OA;
 | 
						|
 | 
						|
final class CategoryController
 | 
						|
{
 | 
						|
    public function __construct(
 | 
						|
        private readonly CategoryServiceInterface $categoryService,
 | 
						|
    )
 | 
						|
    {
 | 
						|
    }
 | 
						|
 | 
						|
    public function list(ListCategoryRequest $request, int $page): JsonResponse
 | 
						|
    {
 | 
						|
        $categories = $this->categoryService->fetchCategories(
 | 
						|
            $page,
 | 
						|
            $request->filters(),
 | 
						|
            $request->order()
 | 
						|
        );
 | 
						|
        return response()->json(PaginableResource::make($categories, CategoryCollection::class));
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @OA\Post(
 | 
						|
     *     path="/api/v1/categories",
 | 
						|
     *     operationId="storeCategory",
 | 
						|
     *     tags={"Categories"},
 | 
						|
     *     summary="Create a new category",
 | 
						|
     *     description="Creates a new category and returns the created category data.",
 | 
						|
     *     @OA\RequestBody(
 | 
						|
     *         required=true,
 | 
						|
     *         @OA\JsonContent(
 | 
						|
     *             type="object",
 | 
						|
     *             required={"name"},
 | 
						|
     *             @OA\Property(property="name", type="string", example="Technology", description="The name of the category")
 | 
						|
     *         )
 | 
						|
     *     ),
 | 
						|
     *     @OA\Response(
 | 
						|
     *         response=201,
 | 
						|
     *         description="Category successfully created",
 | 
						|
     *         @OA\JsonContent(
 | 
						|
     *             type="object",
 | 
						|
     *                 ref="#/components/schemas/CategoryResource"
 | 
						|
     *             )
 | 
						|
     *         )
 | 
						|
     *     ),
 | 
						|
     * @OA\Response(
 | 
						|
     *         response=422,
 | 
						|
     *         description="Validation error",
 | 
						|
     *         @OA\JsonContent(ref="#/components/schemas/ValidationError")
 | 
						|
     *     )
 | 
						|
     * )
 | 
						|
     */
 | 
						|
    public function store(StoreCategoryRequest $request): JsonResponse
 | 
						|
    {
 | 
						|
        return response()->json(CategoryResource::make($this->categoryService->storeCategory($request->all())), 201);
 | 
						|
    }
 | 
						|
 | 
						|
    public function show(ShowCategoryRequest $request, int $id): JsonResponse
 | 
						|
    {
 | 
						|
        $post = $this->categoryService->findCategory($id);
 | 
						|
 | 
						|
        if ($post === null) {
 | 
						|
            return response()->json(null, 404);
 | 
						|
        }
 | 
						|
 | 
						|
        return response()->json(CategoryResource::make($post));
 | 
						|
    }
 | 
						|
 | 
						|
    public function update(UpdateCategoryRequest $request, int $id): JsonResponse
 | 
						|
    {
 | 
						|
        $post = $this->categoryService->updateCategory($request->all(), $id);
 | 
						|
 | 
						|
        if ($post === null) {
 | 
						|
            return response()->json(null, 404);
 | 
						|
        }
 | 
						|
 | 
						|
        return response()->json(CategoryResource::make($post));
 | 
						|
    }
 | 
						|
 | 
						|
    public function destroy(DestroyCategoryRequest $post, int $id): JsonResponse
 | 
						|
    {
 | 
						|
        $isSuccessfullyDeleted = $this->categoryService->deleteCategory($id);
 | 
						|
        return match ($isSuccessfullyDeleted) {
 | 
						|
            false => response()->json(null, 404),
 | 
						|
            true => response()->json(null, 204),
 | 
						|
        };
 | 
						|
    }
 | 
						|
}
 |