From c222e6af8e3bf07879cf015cf023e44bc6d244d9 Mon Sep 17 00:00:00 2001 From: devboyarif Date: Fri, 30 Jul 2021 15:21:19 +0600 Subject: [PATCH] code refactor voicerequst file add --- VoiceController.php | 83 ++++++++++++++++++++++----------------------- VoiceRequest.php | 31 +++++++++++++++++ 2 files changed, 71 insertions(+), 43 deletions(-) create mode 100644 VoiceRequest.php diff --git a/VoiceController.php b/VoiceController.php index b0a3f1c..a94f455 100644 --- a/VoiceController.php +++ b/VoiceController.php @@ -1,48 +1,45 @@ -public function voice(Request $request){ - $request->validate([ - 'question_id'=>'required|int|exists:questions,id', - 'value'=>'required|boolean', - ]); - - $question=Question::find($request->post('question_id')); - if (!$question) - return response()->json([ - 'status'=>404, - 'message'=>'not found question ..' - ]); - if ($question->user_id==auth()->id()) - return response()->json([ - 'status' => 500, - 'message' => 'The user is not allowed to vote to your question' - ]); +id()], - ['question_id','=',$request->post('question_id')] - ])->first(); - if (!is_null($voice)&&$voice->value===$request->post('value')) { - return response()->json([ - 'status' => 500, - 'message' => 'The user is not allowed to vote more than once' - ]); - }else if (!is_null($voice)&&$voice->value!==$request->post('value')){ - $voice->update([ - 'value'=>$request->post('value') +namespace App\Http\Controllers; +class VoiceController extends Controller +{ + public function voice(VoiceRequest $request){ + $question_id = $request->post('question_id'); + $value = $request->post('value'); + $auth_id = auth()->id(); + + $question = Question::find($question_id); + + if (!$question) return $this->response('not found question ..', 404); + + if ($question->user_id == $auth_id) return $this->response('The user is not allowed to vote to your question', 500); + + //check if user voted + $voice = Voice::where([ + ['user_id', $auth_id],['question_id', $question_id] + ])->first(); + + if ($voice && $voice->value === $value) { + return $this->response('The user is not allowed to vote more than once', 500); + }else if ($voice && $voice->value !== $value){ + $voice->update([ + 'value '=> $value + ]); + + return $this->response('update your voice', 201); + } + + $question->voice()->create([ + 'user_id' => $auth_id, + 'value' => $value ]); + + return $this->response('Voting completed successfully'); + } + + public function response($message = 'Data retrieved successfully', $status = 200){ return response()->json([ - 'status'=>201, - 'message'=>'update your voice' + 'status' => 200, + 'message' => $message ]); } - - $question->voice()->create([ - 'user_id'=>auth()->id(), - 'value'=>$request->post('value') - ]); - - return response()->json([ - 'status'=>200, - 'message'=>'Voting completed successfully' - ]); -} \ No newline at end of file diff --git a/VoiceRequest.php b/VoiceRequest.php new file mode 100644 index 0000000..c7d0c47 --- /dev/null +++ b/VoiceRequest.php @@ -0,0 +1,31 @@ + 'required|int|exists:questions,id', + 'value' => 'required|boolean', + ]; + } +}