-
Notifications
You must be signed in to change notification settings - Fork 74
First attempt to improve the code #47
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
base: main
Are you sure you want to change the base?
Conversation
$voice->update([ | ||
'value'=>$request->post('value') | ||
|
||
private $user; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This value use only in one method, better make as local
|
||
public function voice(Request $request) | ||
{ | ||
$this->usre = Auth::user(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mistake "$this->usre"
better to use $userId = Auth::id();
]); | ||
|
||
$question = Question::findOrfail($request->question_id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This never fail, we already validate "exists:questions,id"
]); | ||
} else if (($voice) && $voice->value !== $request->value) { | ||
//update or create first param is the condition of match values, second indicate fields to be updated | ||
$voice->updateOrCreate([ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$voice exists, we don't need create record
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with this parameters user will vote 2 times
$question = Question::findOrfail($request->question_id); | ||
if ($question->user_id == $this->user->id) | ||
return response()->json([ | ||
'status' => 500, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
status code must be 401 "Unauthorized"
'message' => 'update your voice' | ||
]); | ||
} | ||
|
||
return response()->json([ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't check !$voice. If record didn't exist we can't vote
No description provided.