laravel-api-simple-blog/app/Http/Controllers/UserController.php

53 lines
1.1 KiB
PHP
Raw Normal View History

2024-10-16 21:53:02 +03:00
<?php
namespace App\Http\Controllers;
use App\Models\User;
2024-10-16 21:53:02 +03:00
use Illuminate\Http\Request;
2024-10-17 19:15:24 +03:00
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
2024-10-16 21:53:02 +03:00
class UserController extends Controller
{
// GET /user
2024-10-16 21:53:02 +03:00
public function index()
{
return response(User::all());
2024-10-16 21:53:02 +03:00
}
// GET /user/{id}
public function show(string $id)
2024-10-16 21:53:02 +03:00
{
2024-10-17 19:15:24 +03:00
// FIXME: is there more shorter
// validation solution
$v = Validator::make(["id" => $id], [
"id" => 'integer'
]);
if ($v->fails())
throw new BadRequestException();
2024-10-17 18:34:22 +03:00
$user = User::query()->findOrFail($id);
return response($user);
2024-10-16 21:53:02 +03:00
}
// POST /user
public function store(Request $request)
2024-10-16 21:53:02 +03:00
{
return response("Create user");
2024-10-16 21:53:02 +03:00
}
// PUT /user/{id}
2024-10-16 21:53:02 +03:00
public function update(Request $request, string $id)
{
return response("Update user " . $id);
}
// DELETE /user/{id}
2024-10-16 21:53:02 +03:00
public function destroy(string $id)
{
return response("Destroy user " . $id);
}
}