Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat get verified user by #30

Merged
merged 10 commits into from
Nov 17, 2022
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;
}
}
7 changes: 5 additions & 2 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;

// util functions
require_once "../util/sendResponse.php";
Expand All @@ -26,9 +27,11 @@

// other route functions here
Route::get("/test", function(){

// execute the function
return sendResponse(false, 200, "Test case pass", null);
return sendResponse(false, 200, "Test case pass", null);
});

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