Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

farah-darzaid
Copy link

No description provided.

$voice->update([
'value'=>$request->post('value')

private $user;
Copy link

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();
Copy link

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);
Copy link

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([
Copy link

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

Copy link

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,
Copy link

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([
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants