feat/user-reg-api #18
38
app/Http/Controllers/Api/UserController.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Requests\StoreUserRequest;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function registerUser(StoreUserRequest $request)
|
||||
{
|
||||
|
||||
$validated = $request->validated();
|
||||
$validated['is_admin'] = false;
|
||||
$validated['password'] = Str::random(8);
|
||||
$validated['status'] = true;
|
||||
try {
|
||||
$user = User::create($validated);
|
||||
}catch (Throwable $exception){
|
||||
|
|
||||
return response()
|
||||
->json($exception);
|
||||
}
|
||||
|
||||
|
||||
return response()
|
||||
->json(['message' => 'Successfully created user ' . $user->firstname]);
|
||||
}
|
||||
|
||||
public function getUser(Request $request, User $user)
|
||||
{
|
||||
return response()
|
||||
|
llama
commented
Normally it's a good idea to reduce if statement nesting so you'd want to do something like This removes the need for else's everywhere and removes nesting so it's easier to read Normally it's a good idea to reduce if statement nesting so you'd want to do something like
```
if (empty($id)) {
return response()
->json(["status" => "false", "message" => "Please specify UUID"]);
}
...
```
This removes the need for else's everywhere and removes nesting so it's easier to read
|
||||
->json(["status" => true]);
|
||||
}
|
||||
}
|
||||
32
app/Http/Requests/StoreUserRequest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreUserRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'firstname' => ['required', 'string'],
|
||||
'lastname' => ['required', 'string'],
|
||||
'email' => ['required', 'string'],
|
||||
'uuid' => ['required', 'string'],
|
||||
'profile' => ['required', 'string'],
|
||||
];
|
||||
}
|
||||
}
|
||||
BIN
public/images/PriceyBot.webp
Normal file
|
After Width: | Height: | Size: 94 KiB |
@ -15,10 +15,6 @@
|
||||
|
||||
<title>{{ config('app.name', 'Laravel') }}</title>
|
||||
|
||||
<!-- Fonts -->
|
||||
<link rel="preconnect" href="https://fonts.bunny.net">
|
||||
<link href="https://fonts.bunny.net/css?family=figtree:400,500,600&display=swap" rel="stylesheet" />
|
||||
|
||||
<!-- Scripts -->
|
||||
@vite(['resources/css/app.css', 'resources/js/app.js'])
|
||||
</head>
|
||||
|
||||
@ -14,10 +14,6 @@
|
||||
|
||||
<title>{{ config('app.name', 'Laravel') }}</title>
|
||||
|
||||
<!-- Fonts -->
|
||||
<link rel="preconnect" href="https://fonts.bunny.net">
|
||||
<link href="https://fonts.bunny.net/css?family=figtree:400,500,600&display=swap" rel="stylesheet" />
|
||||
|
||||
<!-- Scripts -->
|
||||
@vite(['resources/css/app.css', 'resources/js/app.js'])
|
||||
</head>
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\Api\WebHookController;
|
||||
use App\Http\Controllers\Api\UserController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
@ -18,4 +20,9 @@ Route::middleware(['header.auth', 'throttle:api'])->group(function () {
|
||||
Route::get('/test', [WebHookController::class, 'test']);
|
||||
Route::post('/sendQuote', [WebHookController::class, 'sendQuote']);
|
||||
Route::post('/randomQuote', [WebHookController::class, 'sendRandomQuote']);
|
||||
|
llama
commented
we can probably also put this under /user/register and /user since that'll be closer to how standard crud stuff works https://laravel.com/docs/10.x/controllers#actions-handled-by-resource-controller This way we'd have consistency once we start adding more models like quotes and stuff to the endpoints we can probably also put this under /user/register and /user since that'll be closer to how standard crud stuff works
https://laravel.com/docs/10.x/controllers#actions-handled-by-resource-controller
This way we'd have consistency once we start adding more models like quotes and stuff to the endpoints
|
||||
Route::post('/user/register', [UserController::class, 'registerUser']);
|
||||
|
llama
commented
https://laravel.com/docs/10.x/routing#implicit-model-binding-scoping We can do something called model binding, this will allow us to automatically map this to a model without us needing to fetch it afterwards. and then inside the controller you'd do and if you dumb out $user it should be the model already, I think this also handles invalid uuids too with a response https://laravel.com/docs/10.x/routing#implicit-model-binding-scoping
We can do something called model binding, this will allow us to automatically map this to a model without us needing to fetch it afterwards.
```
Route::get('/getUser/{user:uuid}', [UserController::class, 'getUser']);
```
and then inside the controller you'd do
```
public function getUser(Request $request, User $user)
```
and if you dumb out $user it should be the model already, I think this also handles invalid uuids too with a response
|
||||
Route::get('/user/{user:uuid}', [UserController::class, 'getUser'])
|
||||
->missing(function (Request $request) {
|
||||
return response()->json(["status" => false, "message" => "User not found"]);
|
||||
});
|
||||
});
|
||||
|
||||
This probably can just be "Throwable" instead of a specific catch