feat/user-reg-api #18

Merged
llama merged 6 commits from feat/user-reg-api into master 2023-10-10 03:19:17 -04:00
2 changed files with 10 additions and 20 deletions
Showing only changes of commit a151f439cb - Show all commits

View File

@ -20,7 +20,7 @@ class UserController extends Controller
$validated['status'] = true;
try {
$user = User::create($validated);
}catch (\Illuminate\Database\QueryException $exception){
}catch (Throwable $exception){
Outdated
Review

This probably can just be "Throwable" instead of a specific catch

This probably can just be "Throwable" instead of a specific catch
return response()
->json($exception);
}
@ -30,23 +30,9 @@ class UserController extends Controller
->json(['message' => 'Successfully created user ' . $user->firstname]);
}
public function getUser(Request $request, $id)
public function getUser(Request $request, User $user)
{
if (!empty($id)){
try {
# The user exists
$user = User::where('uuid', $id)->firstOrfail();
return response()
->json(["status" => true]);
} catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
// The user does not exist
return response()
->json(["status" => false]);
}
} else {
return response()
->json(["status" => "false", "message" => "Please specify UUID"]);
}
return response()
Outdated
Review

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

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]);
}
}

View File

@ -3,6 +3,7 @@
use App\Http\Controllers\Api\WebHookController;
use App\Http\Controllers\Api\UserController;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
@ -19,6 +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']);
Outdated
Review

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('/registerUser', [UserController::class, 'registerUser']);
Route::get('/getUser/{id}', [UserController::class, 'getUser']);
Route::post('/user/register', [UserController::class, 'registerUser']);
Outdated
Review

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

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"]);
});
});