From 9af10695bfc63701f247a414835e856540548509 Mon Sep 17 00:00:00 2001 From: Sens van Aert Date: Tue, 5 Dec 2023 15:31:40 +0100 Subject: [PATCH 1/4] Answer controller changes --- code/webapp/app/Http/Controllers/Api/AnswerController.php | 3 +++ code/webapp/app/Http/Requests/StoreAnswerRequest.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/code/webapp/app/Http/Controllers/Api/AnswerController.php b/code/webapp/app/Http/Controllers/Api/AnswerController.php index 29391a0b..8c390ba5 100644 --- a/code/webapp/app/Http/Controllers/Api/AnswerController.php +++ b/code/webapp/app/Http/Controllers/Api/AnswerController.php @@ -202,6 +202,9 @@ public function create() */ public function store(StoreAnswerRequest $request) { + $user = auth()->user(); + $id = $user->id; + $request->merge(["user_id", $id]); $answer = Answer::create($request->all()); return response()->json( diff --git a/code/webapp/app/Http/Requests/StoreAnswerRequest.php b/code/webapp/app/Http/Requests/StoreAnswerRequest.php index e7f68dc4..d07ec7ad 100644 --- a/code/webapp/app/Http/Requests/StoreAnswerRequest.php +++ b/code/webapp/app/Http/Requests/StoreAnswerRequest.php @@ -24,7 +24,7 @@ public function authorize() public function rules() { return [ - "user_id" => "integer|required", + "user_id" => "integer", "question_id" => "integer|required", "answer" => "string|required", ]; From 50823b836da0a775a749ad77f6e0a34d75b26708 Mon Sep 17 00:00:00 2001 From: Sens van Aert Date: Wed, 6 Dec 2023 13:26:37 +0100 Subject: [PATCH 2/4] Working controller --- code/webapp/app/Http/Controllers/Api/AnswerController.php | 8 +++++--- code/webapp/app/Http/Requests/StoreAnswerRequest.php | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/code/webapp/app/Http/Controllers/Api/AnswerController.php b/code/webapp/app/Http/Controllers/Api/AnswerController.php index 8c390ba5..f5e954cc 100644 --- a/code/webapp/app/Http/Controllers/Api/AnswerController.php +++ b/code/webapp/app/Http/Controllers/Api/AnswerController.php @@ -202,9 +202,11 @@ public function create() */ public function store(StoreAnswerRequest $request) { - $user = auth()->user(); - $id = $user->id; - $request->merge(["user_id", $id]); + if($request->user_id == null){ + $user = auth()->user(); + $id = $user->id; + $request->merge(["user_id", $id]); + } $answer = Answer::create($request->all()); return response()->json( diff --git a/code/webapp/app/Http/Requests/StoreAnswerRequest.php b/code/webapp/app/Http/Requests/StoreAnswerRequest.php index d07ec7ad..e7f68dc4 100644 --- a/code/webapp/app/Http/Requests/StoreAnswerRequest.php +++ b/code/webapp/app/Http/Requests/StoreAnswerRequest.php @@ -24,7 +24,7 @@ public function authorize() public function rules() { return [ - "user_id" => "integer", + "user_id" => "integer|required", "question_id" => "integer|required", "answer" => "string|required", ]; From 67b1d5b192d9fac310c4064779d0c67e76f70bce Mon Sep 17 00:00:00 2001 From: Sens van Aert Date: Wed, 6 Dec 2023 14:27:16 +0100 Subject: [PATCH 3/4] Changed login to also send the user --- code/webapp/app/Http/Controllers/Api/AuthController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/code/webapp/app/Http/Controllers/Api/AuthController.php b/code/webapp/app/Http/Controllers/Api/AuthController.php index 54065632..110bf347 100644 --- a/code/webapp/app/Http/Controllers/Api/AuthController.php +++ b/code/webapp/app/Http/Controllers/Api/AuthController.php @@ -156,6 +156,7 @@ public function loginUser(Request $request) "status" => true, "message" => "User Logged In Successfully", "Token" => $user->createToken("API TOKEN")->plainTextToken, + "User" => $user, ], 200 ); From 98c6ad8792da738b8ed1344a040290526edbe7c7 Mon Sep 17 00:00:00 2001 From: Sens van Aert <91054402+SensvanAert@users.noreply.github.com> Date: Wed, 6 Dec 2023 14:32:03 +0100 Subject: [PATCH 4/4] Update AnswerController.php --- code/webapp/app/Http/Controllers/Api/AnswerController.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/code/webapp/app/Http/Controllers/Api/AnswerController.php b/code/webapp/app/Http/Controllers/Api/AnswerController.php index f5e954cc..29391a0b 100644 --- a/code/webapp/app/Http/Controllers/Api/AnswerController.php +++ b/code/webapp/app/Http/Controllers/Api/AnswerController.php @@ -202,11 +202,6 @@ public function create() */ public function store(StoreAnswerRequest $request) { - if($request->user_id == null){ - $user = auth()->user(); - $id = $user->id; - $request->merge(["user_id", $id]); - } $answer = Answer::create($request->all()); return response()->json(