187 lines
5.5 KiB
PHP
187 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Resources\CategoryResource;
|
|
use App\Models\Category;
|
|
use App\Models\Log;
|
|
use Illuminate\Http\Request;
|
|
|
|
class CategoryController extends Controller
|
|
{
|
|
/**
|
|
* @OA\Get(
|
|
* path="/api/categories",
|
|
* summary="Get all categories",
|
|
* tags={"Categories"},
|
|
* @OA\Response(
|
|
* response=200,
|
|
* description="List of categories",
|
|
* @OA\JsonContent(
|
|
* type="array",
|
|
* @OA\Items(ref="#/components/schemas/CategoryResource")
|
|
* )
|
|
* )
|
|
* )
|
|
*/
|
|
public function index()
|
|
{
|
|
$categories = Category::withCount('user_tests', 'questions')->get();
|
|
return CategoryResource::collection($categories);
|
|
}
|
|
|
|
/**
|
|
* @OA\Post(
|
|
* path="/api/categories",
|
|
* summary="Create a new category (only admin)",
|
|
* tags={"Categories"},
|
|
* security={{"bearerAuth": {}}},
|
|
* @OA\RequestBody(
|
|
* required=true,
|
|
* @OA\JsonContent(
|
|
* required={"name"},
|
|
* @OA\Property(property="name", type="string", example="Physics")
|
|
* )
|
|
* ),
|
|
* @OA\Response(
|
|
* response=200,
|
|
* description="Category created successfully",
|
|
* @OA\JsonContent(ref="#/components/schemas/CategoryResource")
|
|
* ),
|
|
* @OA\Response(
|
|
* response=403,
|
|
* description="Forbidden"
|
|
* )
|
|
* )
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$this->authorize('create', Category::class);
|
|
$fields = $request->validate([
|
|
'name' => 'required|max:150'
|
|
]);
|
|
$category = Category::create($fields);
|
|
|
|
Log::writeLog("Category '" . $category->name . "' is created by " . $request->user()->username);
|
|
|
|
return new CategoryResource($category);
|
|
}
|
|
|
|
/**
|
|
* @OA\Get(
|
|
* path="/api/categories/{id}",
|
|
* summary="Get a specific category",
|
|
* tags={"Categories"},
|
|
* @OA\Parameter(
|
|
* name="id",
|
|
* in="path",
|
|
* required=true,
|
|
* description="ID of the category",
|
|
* @OA\Schema(type="integer", example=1)
|
|
* ),
|
|
* @OA\Response(
|
|
* response=200,
|
|
* description="Category retrieved successfully",
|
|
* @OA\JsonContent(ref="#/components/schemas/CategoryResource")
|
|
* ),
|
|
* @OA\Response(
|
|
* response=404,
|
|
* description="Category not found"
|
|
* )
|
|
* )
|
|
*/
|
|
public function show(Category $category)
|
|
{
|
|
return new CategoryResource($category->withCount('user_tests', 'questions'));
|
|
}
|
|
|
|
/**
|
|
* @OA\Put(
|
|
* path="/api/categories/{id}",
|
|
* summary="Update a category (only admin)",
|
|
* tags={"Categories"},
|
|
* security={{"bearerAuth": {}}},
|
|
* @OA\Parameter(
|
|
* name="id",
|
|
* in="path",
|
|
* required=true,
|
|
* description="ID of the category",
|
|
* @OA\Schema(type="integer", example=1)
|
|
* ),
|
|
* @OA\RequestBody(
|
|
* required=true,
|
|
* @OA\JsonContent(
|
|
* required={"name"},
|
|
* @OA\Property(property="name", type="string", example="Physics Updated")
|
|
* )
|
|
* ),
|
|
* @OA\Response(
|
|
* response=200,
|
|
* description="Category updated successfully",
|
|
* @OA\JsonContent(ref="#/components/schemas/CategoryResource")
|
|
* ),
|
|
* @OA\Response(
|
|
* response=403,
|
|
* description="Forbidden"
|
|
* ),
|
|
* @OA\Response(
|
|
* response=404,
|
|
* description="Category not found"
|
|
* )
|
|
* )
|
|
*/
|
|
public function update(Request $request, Category $category)
|
|
{
|
|
$this->authorize('update', $category);
|
|
$fields = $request->validate([
|
|
'name' => 'required|max:150'
|
|
]);
|
|
$old_category_name = $category->name;
|
|
$category->update($fields);
|
|
|
|
Log::writeLog("Category '$old_category_name' is renamed to '" . $category->name ."' by " . $request->user()->username);
|
|
|
|
return new CategoryResource($category);
|
|
}
|
|
|
|
/**
|
|
* @OA\Delete(
|
|
* path="/api/categories/{id}",
|
|
* summary="Delete a category (only admin)",
|
|
* tags={"Categories"},
|
|
* security={{"bearerAuth": {}}},
|
|
* @OA\Parameter(
|
|
* name="id",
|
|
* in="path",
|
|
* required=true,
|
|
* description="ID of the category",
|
|
* @OA\Schema(type="integer", example=1)
|
|
* ),
|
|
* @OA\Response(
|
|
* response=200,
|
|
* description="Category deleted successfully",
|
|
* @OA\JsonContent(
|
|
* @OA\Property(property="message", type="string", example="The category was deleted")
|
|
* )
|
|
* ),
|
|
* @OA\Response(
|
|
* response=403,
|
|
* description="Forbidden"
|
|
* ),
|
|
* @OA\Response(
|
|
* response=404,
|
|
* description="Category not found"
|
|
* )
|
|
* )
|
|
*/
|
|
public function destroy(Request $request, Category $category)
|
|
{
|
|
$this->authorize('delete', $category);
|
|
$category->delete();
|
|
|
|
Log::writeLog("Category '" . $category->name . "' is deleted by " . $request->user()->username);
|
|
|
|
return ['message' => 'The category was deleted'];
|
|
}
|
|
}
|