feat/user-reg-api #18
43
app/Http/Controllers/Api/UserController.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function registerUser(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'firstname' => ['required', 'string'],
|
||||
'lastname' => ['required', 'string'],
|
||||
'email' => ['required', 'string'],
|
||||
'uuid' => ['required', 'string'],
|
||||
'profile' => ['required', 'string'],
|
||||
]);
|
||||
if ($validator->fails()) {
|
||||
|
||||
|
|
||||
return response()
|
||||
->json($validator->errors());
|
||||
}
|
||||
|
||||
$validated = $validator->validated();
|
||||
$validated['is_admin'] = false;
|
||||
$validated['password'] = Str::random(8);
|
||||
$validated['status'] = true;
|
||||
try {
|
||||
$user = User::create($validated);
|
||||
}catch (\Illuminate\Database\QueryException $exception){
|
||||
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($exception);
|
||||
}
|
||||
|
||||
|
||||
return response()
|
||||
->json(['message' => 'Successfully created user ' . $user->firstname]);
|
||||
|
llama marked this conversation as resolved
Outdated
llama
commented
same thing, probably can be just Throwable instead of a specific thing, same thing, probably can be just Throwable instead of a specific thing,
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\Api\WebHookController;
|
||||
use App\Http\Controllers\Api\UserController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
/*
|
||||
@ -18,4 +19,5 @@ 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']);
|
||||
Route::post('/registerUser', [UserController::class, 'registerUser']);
|
||||
|
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
|
||||
});
|
||||
|
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
|
||||
|
||||
This probably can just be "Throwable" instead of a specific catch