Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Services/QuestionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?
public function question($request){

$request->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'));
}
30 changes: 30 additions & 0 deletions Services/VoiceService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

public function checkVoice($request,$question){

//get voice by question relationship
$voice=$question->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'
]);
}

}



?>
50 changes: 14 additions & 36 deletions VoiceController.php
Original file line number Diff line number Diff line change
@@ -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'
]);
<?php
public function voice(Request $request){

//perform question related operations into seprate quetion service class
QuestionService::question($request);

//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'
]);
}
//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(),
Expand All @@ -45,4 +22,5 @@ public function voice(Request $request){
'status'=>200,
'message'=>'Voting completed successfully'
]);
}
}
?>