Skip to content

Commit

Permalink
feat: added get verified user by id in issue #18
Browse files Browse the repository at this point in the history
  • Loading branch information
dasaint001 committed Nov 17, 2022
1 parent ef0ec77 commit 770527c
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
54 changes: 54 additions & 0 deletions backend/app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,58 @@
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

/**
* @param $message
* @return Response
*/
protected function respondWithMissingField($message)
{
return response()->json([
'status' => 400,
'message' => $message,
], 400);
}

/**
* @param $message
* @return Response
*/
private function respondWithValidationError($message)
{
return response()->json([
'status' => false,
'message' => $message,
], 400);
}

/**
* @param $validator
* @return Response
*/
protected function respondWithErrorMessage($validator)
{
$required = $messages = [];
$validatorMessages = $validator->errors()->toArray();
foreach($validatorMessages as $field => $message) {
if (strpos($message[0], 'required')) {
$required[] = $field;
}

foreach ($message as $error) {
$messages[] = $error;
}
}

if (count($required) > 0) {
$fields = implode(', ', $required);
$message = "Missing required fields $fields";

return $this->respondWithMissingField($message);
}


return $this->respondWithValidationError(implode(', ', $messages));
}

}
27 changes: 27 additions & 0 deletions backend/app/Http/Controllers/User/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Services\UserService;

class UserController extends Controller
{
public function getVerifiedUserById($userId)
{
$verified_user = UserService::getVerifiedUser($userId);

if (!$verified_user) {
return response()->json([
'status' => false,
'message' => 'User does not exist or is not a verified user'
], 404);
}

return response()->json([
'status' => true,
'user' => $verified_user
], 200);
}
}
19 changes: 19 additions & 0 deletions backend/app/Services/UserService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php


namespace App\Services;


use App\Models\User;


class UserService
{

public static function getVerifiedUser($userId)
{
$user = User::where('id', $userId)->where('isVerified', true)->first();

return $user;
}
}
5 changes: 5 additions & 0 deletions backend/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\User\UserController;

/*
|--------------------------------------------------------------------------
Expand All @@ -17,3 +18,7 @@
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});

Route::group(['prefix' => 'users'], function() {
Route::get('verified/{userId}', [UserController::class, 'getVerifiedUserById']);
});

0 comments on commit 770527c

Please sign in to comment.