68 lines
1.6 KiB
PHP
68 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
|
|
class PostsController extends Controller
|
|
{
|
|
public function index(): JsonResponse
|
|
{
|
|
return response()->json([
|
|
[
|
|
'id' => 1,
|
|
'title' => 'My first post',
|
|
],
|
|
[
|
|
'id' => 2,
|
|
'title' => 'New post',
|
|
]
|
|
]);
|
|
}
|
|
|
|
public function destroy(int $id): JsonResponse
|
|
{
|
|
// мы бы здесь написали вызов запроса delete из БД
|
|
return response()->json([
|
|
'success' => true,
|
|
], Response::HTTP_ACCEPTED);
|
|
}
|
|
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'title' => $request->get('title'),
|
|
'text' => $request->get('text'),
|
|
], Response::HTTP_CREATED);
|
|
}
|
|
|
|
public function update(Request $request, int $id): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'title' => $request->get('title'),
|
|
'text' => $request->get('text'),
|
|
], Response::HTTP_ACCEPTED);
|
|
}
|
|
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
if ($id === 5) {
|
|
return response()->json(
|
|
[
|
|
'status' => 'Not found'
|
|
],
|
|
Response::HTTP_NOT_FOUND
|
|
);
|
|
}
|
|
|
|
return response()->json([
|
|
'id' => $id,
|
|
'title' => 'My first post',
|
|
'text' => 'My first post text',
|
|
]);
|
|
}
|
|
}
|