Skip to content

Commit

Permalink
fix: conflict resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
highfrezh committed Nov 18, 2022
2 parents 7922ce2 + 966077d commit 46da302
Show file tree
Hide file tree
Showing 74 changed files with 69,559 additions and 126 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ How to run locally:
We use GitHub issues to track public bugs. Please ensure your description is
clear and has sufficient instructions to be able to reproduce the issue. Report a bug by <a href="https://github.com/workshopapps/engineerprofile360.web/issues">opening a new issue</a>; it's that easy!

> **_Note!!** each issues given, must be followed by a specific track to ease the assessibility of other devs. for eg :- Reacreate the login screen page (FE), fix typo in utility class (BE). where FE & BE are Frontend and Backend respectively.
> **_Note!!** each issues given, must be followed by a specific track to ease the assessibility of other devs. for eg :- Reacreate the login screen page (FE), fix typo in utility class (BE). where FE & BE are Frontend and Backend respectively.
5 changes: 5 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ yarn-error.log
/.fleet
/.idea
/.vscode
composer.lock

# ignore .http file

/.http
7 changes: 4 additions & 3 deletions backend/API_ROUTE.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ Below are the endpoints which would be used when adding any features.

| Class Methods | Method / Endpoints | Auth | Payload |
| ------------- |:-------------:| -----:| -----:
|`addUserScore()`| `POST /api/userScore/create` |`JWT`| `[question_id, assessment_id, category_id, category, question,wrong_answers, correct_answers, option, timeframe,is_multiple_answer]` |
|`getUserScoreByUserId()`| `GET /api/userScore/get/{user_id}` |`JWT`| `user_id` |
|`getUserScoreByAss()`| `GET /api/userScore/get/{ass_id}` |`JWT`| `asS_id` |
|`store()`| `POST /api/userScore/create` |`JWT`| `[assessment_id, employee_id, categories, passed_questions]` |
|`getScores()`| `GET /api/userScore/employee/{employee_id}` |`JWT`| `employee_id` |
|`getScores()`| `GET /api/userScore/assessment/{ass_id}` |`JWT`| `ass_id` |
|`getScores()`| `GET /api/userScore/{employee_id}/{ass_id}` |`JWT`| `employee_id, ass_id` |

**AuthenticationController**

Expand Down
96 changes: 96 additions & 0 deletions backend/app/Http/Controllers/AssessmentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Assessment;
use Exception;
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class AssessmentController extends Controller
{


public function create_assessment(Request $request)
{
$result = $request->validate([
'user_id' => 'required',
'name' => 'required',
'start_date' => 'required',
'start_time' => 'required',
]);

if ($result) {
try {
$data = file_get_contents("php://input");
$assessment = new Assessment();
$assessment->user_id = $data->user_id;
$assessment->name = $data->name;
$assessment->start_date = $data->start_date;
$assessment->start_time = $data->start_time;
$assessment->save();
return $this->successResponse(true, 'Assessment created successfully', Response::HTTP_CREATED);

} catch (Exception $e) {
return $this->errorResponse('Assessment could not be created', $e->getMessage());
}

}
}

// @dreywandowski ---- delete an assessment
public function deleteAss($ass_id): JsonResponse
{
try{
$assessment = Assessment::findorFail($ass_id);

if( !$assessment) {
return $this->errorResponse(
'Assessment does not exist',
'Assessment not found',
Response::HTTP_NOT_FOUND
);
}
$assessment->delete();

return $this->successResponse(true, 'Assessment deleted successfully', Response::HTTP_OK);
} catch (Exception $e) {
return $this->errorResponse('Assessment not fetched', $e->getMessage());
}

}

public function update(Request $request, $id){
try {
// $user = auth('sanctum')->user()->id;
$this->validate($request, [
'name' =>'required',
'start_date' =>'required',
'start_time' =>'required',
]);
$assessment = Assessment::findorFail($id);
if( !$assessment) {
return $this->errorResponse(
'Assessment does not exist',
'Assessment not found',
Response::HTTP_NOT_FOUND
);
}
$assessment->name = $request->input['name'];
$assessment->start_date = $request->input['start_date'];
$assessment->start_date = $request->input['start_date'];
$assessment->save();
return response()->json([
'status' => 'Assessment Successfully Updated',
'Data' => $assessment
]);
} catch (Exception $e) {
return $this->errorResponse('Assessment not fetched', $e->getMessage());
}




}
}

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

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Support\Facades\Hash;

class AuthenticationController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', ['except' => ['register', 'login']]);
}

/**
* Get a JWT via given credentials.
*
* @return \Illuminate\Http\JsonResponse
*/
public function register(Request $request){
$request->validate([
'full_name' => 'required|string|max:255',
'username' => 'required|string|max:255|unique:users',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6',
]);

$user = User::create([
'full_name' => $request->full_name,
'username' => $request->username,
'email' => $request->email,
'password' => Hash::make($request->password),
]);

return response()->json([
"error" => false,
"code" => 200,
"message" => "User Registration successfull"
]);
}

/**
* Get a JWT via given credentials.
*
* @return \Illuminate\Http\JsonResponse
*/
public function login(Request $request)
{
$request->validate([
'email' => 'required|string|email',
'password' => 'required|string',
]);

$credentials = $request->only('email', 'password');

if (!auth()->validate($credentials)) {
return response()->json([
"error" => true,
"code" => 400,
"message" => "Invalid email or Password"
], 400);
}

$token = auth()->attempt($credentials);
if (!$token) {
return response()->json([
'error' => 'true',
"code" => 401,
'message' => 'Unauthorized, Log in to continue',
], 401);
}

$user = auth()->user();
return response()->json([
"error" => false,
"code" => 200,
"message" => "User Logged in",
"data" => [
'user' => $user,
'authorisation' => [
'token' => $token,
'type' => 'bearer',
]
]
]);
}


/**
* Log the user out (Invalidate the token).
*
* @return \Illuminate\Http\JsonResponse
*/
public function logout()
{
auth()->logout();

return response()->json([
"error" => false,
"code" => 200,
"message" => "User Logged out"
]);
}

/**
* Refresh a token.
*
* @return \Illuminate\Http\JsonResponse
*/
public function refresh()
{
return response()->json([
"error" => false,
"code" => 200,
"message" => "User token refreshed",
"data" => [
'user' => auth()->user(),
'authorisation' => [
'token' => auth()->refresh(),
'type' => 'bearer',
]
]
]);
}

}
88 changes: 49 additions & 39 deletions backend/app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,73 @@
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Log;


/**
* @OA\Info(title="My First API", version="0.1")
*/
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

/**
* @param $message
* @return Response

/**
* @param bool $status = true
* @param null $data
* @param string $message
* @param int $code
*
* @return JsonResponse
*/
protected function respondWithMissingField($message)
public function successResponse(
bool $status = true,
string $message = 'OK',
$data = null,
int $code = 200
) : JsonResponse
{
return response()->json([
'status' => 400,
'status' => $status,
'message' => $message,
], 400);
'data' => $data,
], $code);
}

/**
* @param $message
* @return Response
* Empty response without data
* @param $status
* @param int $code
*
* @return JsonResponse
*/
private function respondWithValidationError($message)
public function emptySuccessResponse($status, int $code = 201): JsonResponse
{
return response()->json([
'status' => false,
'message' => $message,
], 400);
'status' => $code,
'data' => $status,
], $code);
}

/**
* @param $validator
* @return Response
*/
protected function respondWithErrorMessage($validator)
* Error response. It uses code 200 because "API client can't parse the error"
* @param string $message
* @param null $errors
* @param int $code
* @return JsonResponse
*/
public function errorResponse(
string $message = 'Error message',
$errors = null,
int $code = 400
) : JsonResponse
{
$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));
Log::error($errors);
return response()->json([
'status' => 'error',
'message' => $message,
'errors' => $errors,
], $code);
}

}
Loading

0 comments on commit 46da302

Please sign in to comment.