authorize('create', Category::class); $fields = $request->validate([ 'name' => 'required|max:150' ]); $category = Category::create($fields); Log::writeLog("Category '" . $category->name . "' is created"); 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); } /** * @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 ."'"); 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(Category $category) { $this->authorize('delete', $category); $category->delete(); Log::writeLog("Category '" . $category->name . "' is deleted"); return ['message' => 'The category was deleted']; } }