Skip to content

Changes made #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
57 changes: 55 additions & 2 deletions VoiceController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public function voice(Request $request){
<!-- public function voice(Request $request){
$request->validate([
'question_id'=>'required|int|exists:questions,id',
'value'=>'required|boolean',
Expand Down Expand Up @@ -45,4 +45,57 @@ public function voice(Request $request){
'status'=>200,
'message'=>'Voting completed successfully'
]);
}
} -->

public function voice(Request $request) {

// using validator() helper method will throw correct validation error

validator ($request->all(),[
'question_id' => 'required|int|exists:questions,id',
'value' => 'required|boolean',
])->validate();


// Assuming Voice model is associated with one Question model

$voice = Voice::with('question')
->where('question_id', $request->post('question_id')
->where('user_id', auth()->id)
->first();

if ($voice !== null && $voice->value !== $request->post('value')) {
$voice->update([
'value'=>$request->post('value')
]);

return response()->json([
'message'=>'Your voice has been updated'
], 201);
}

else if ($voice !== null && $voice->value == $request->post('value')) {
return response()->json([
'message' => 'Sorry you are not allowed to vote more than once'
], 500);
}
else {
$voice->create([
'user_id' => auth()->id(),
'question_id' => $request->post('question_id),
'value' => $request->post('value')
]);

return response()->json([
'message'=>'Voting completed successfully'
], 200);
}

}

// The modal function that shows the one to one association of Voice to Question Model

public function question()
{
return $this->belongsTo(Question::class);
}