diff --git a/App/Helpers/Helper.php b/App/Helpers/Helper.php new file mode 100644 index 0000000..5c26c60 --- /dev/null +++ b/App/Helpers/Helper.php @@ -0,0 +1,8 @@ +json([ + 'status'=>$status, + 'message'=>$message + ]); +} \ No newline at end of file diff --git a/App/Http/Controllers/VoiceController.php b/App/Http/Controllers/VoiceController.php new file mode 100644 index 0000000..ad7fc9d --- /dev/null +++ b/App/Http/Controllers/VoiceController.php @@ -0,0 +1,5 @@ +public function voice(VoiceRequest $request,Question $question){ + + return (new VoteService)->validateOrUpdateOrCreate($request->validated(),$question); + +} \ No newline at end of file diff --git a/App/Http/Providers/AppServiceProvider.php b/App/Http/Providers/AppServiceProvider.php new file mode 100644 index 0000000..dc84d22 --- /dev/null +++ b/App/Http/Providers/AppServiceProvider.php @@ -0,0 +1,9 @@ + + + + +// + +require '../../Helpers/Helper.php'; + +// \ No newline at end of file diff --git a/App/Http/Requests/VoiceRequest.php b/App/Http/Requests/VoiceRequest.php new file mode 100644 index 0000000..6fe1fc0 --- /dev/null +++ b/App/Http/Requests/VoiceRequest.php @@ -0,0 +1,17 @@ +$question = Question::find(request['question_id']); +request('auth_id') = auth()->user()->id; +request('question_user_id') = $question->user_id; + +return [ + 'question_id'=>'required|int|exists:questions,id', + 'value'=>'required|boolean', + 'auth_id'=>'different:question_user_id' +]; + + + +public function messages(){ + return [ + 'auth_id.different' => response_json(500,'The user is not allowed to vote to your question') + ]; +} \ No newline at end of file diff --git a/App/Model/Vote.php b/App/Model/Vote.php new file mode 100644 index 0000000..edfaa14 --- /dev/null +++ b/App/Model/Vote.php @@ -0,0 +1,6 @@ +public function scopeFindQuestion($builder,int $question_id){ + return $builder->where([ + ['user_id','=',auth()->id()], + ['question_id','=',$question_id + ]); +} \ No newline at end of file diff --git a/App/Observers/VoiceObserver.php b/App/Observers/VoiceObserver.php new file mode 100644 index 0000000..6df6a6c --- /dev/null +++ b/App/Observers/VoiceObserver.php @@ -0,0 +1,3 @@ +public function creating(Voice $voice){ + $voice['user_id'] = auth()->id(); +} \ No newline at end of file diff --git a/App/Services/VoteService.php b/App/Services/VoteService.php new file mode 100644 index 0000000..78c5027 --- /dev/null +++ b/App/Services/VoteService.php @@ -0,0 +1,54 @@ +validate($request); + + if ( $validation['needs update'] ) { + return $this->update($validation['needs update'],$request['value']); + }elseif ($validation['error']) { + return $validation['error']; + } + + return $this->create($question,$request['value']); + } + + public function update($voice,string $value) + { + $voice->update([ + 'value'=>$value + ]); + + return response_json(201,'update your voice'); + } + + public function create($question,$value) + { + $question->voice()->create([ + 'value'=>$value + ]); + + return response_json(200,'Voting completed successfully'); + } + + public function validate($request) + { + $voice=Voice::findQuestion($request['question_id'])->first(); + + if ( !is_null($voice)&&$voice->value!==$request['value'] ) { + return ['needs update'=>$voice]; + + }elseif( is_null($voice)&&$voice->value==$request['value'] ){ + return [ + 'error'=>response_json(500,'The user is not allowed to vote more than once') + ]; + } + } +} \ No newline at end of file diff --git a/VoiceController.php b/VoiceController.php deleted file mode 100644 index b0a3f1c..0000000 --- a/VoiceController.php +++ /dev/null @@ -1,48 +0,0 @@ -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' - ]); - - //check if user voted - $voice=Voice::where([ - ['user_id','=',auth()->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' - ]); - } - - $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