From 08c21644bf66013e4146e48080ed8d0157535bdd Mon Sep 17 00:00:00 2001 From: hamza Date: Fri, 30 Jul 2021 19:26:07 +0500 Subject: [PATCH] Controller Refactored into Services --- Services/QuestionService.php | 27 +++++++++++++++++++ Services/VoiceService.php | 30 ++++++++++++++++++++++ VoiceController.php | 50 ++++++++++-------------------------- 3 files changed, 71 insertions(+), 36 deletions(-) create mode 100644 Services/QuestionService.php create mode 100644 Services/VoiceService.php diff --git a/Services/QuestionService.php b/Services/QuestionService.php new file mode 100644 index 0000000..e4c9726 --- /dev/null +++ b/Services/QuestionService.php @@ -0,0 +1,27 @@ + validate([ + 'question_id'=>'required|int|exists:questions,id', + 'value'=>'required|boolean', + ]); + + $question=$this->findQuestion($request); + + 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' + ]); + } + + public function findQuestion($request){ + + return Question::find($request->post('question_id')); + } \ No newline at end of file diff --git a/Services/VoiceService.php b/Services/VoiceService.php new file mode 100644 index 0000000..3f5a964 --- /dev/null +++ b/Services/VoiceService.php @@ -0,0 +1,30 @@ +voice()->where('user_id','=',auth()->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') + ]); + + return response()->json([ + 'status'=>201, + 'message'=>'update your voice' + ]); + } + +} + + + +?> \ No newline at end of file diff --git a/VoiceController.php b/VoiceController.php index b0a3f1c..8d5290d 100644 --- a/VoiceController.php +++ b/VoiceController.php @@ -1,40 +1,17 @@ -public function voice(Request $request){ - $request->validate([ - 'question_id'=>'required|int|exists:questions,id', - 'value'=>'required|boolean', - ]); +use Services\QuestionService; +use Services\VoiceService; - $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') - ]); - return response()->json([ - 'status'=>201, - 'message'=>'update your voice' - ]); - } + //find question by id + $question=QuestionService::findQuestion($request); + + //perform voice related operations into seprate voice service class + VoiceService::checkVoice($request,$question); $question->voice()->create([ 'user_id'=>auth()->id(), @@ -45,4 +22,5 @@ public function voice(Request $request){ 'status'=>200, 'message'=>'Voting completed successfully' ]); -} \ No newline at end of file +} +?> \ No newline at end of file