Skip to content

refactoring code #27

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 1 commit into
base: main
Choose a base branch
from
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
31 changes: 31 additions & 0 deletions QuestionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class QuestionRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'question_id' => 'required|int|exists:questions,id',
'value' => 'required|boolean',
];
}
}
84 changes: 42 additions & 42 deletions VoiceController.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
public function voice(Request $request){
$request->validate([
'question_id'=>'required|int|exists:questions,id',
'value'=>'required|boolean',
]);
<?php

$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'
]);
namespace App\Http\Controllers;

//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')
use App\Http\Requests\QuestionRequest;
use App\Models\Question;
use Illuminate\Http\Request;

class VoiceController extends Controller
{
public function voice(QuestionRequest $request)
{
$question = Question::findOrFail($request->post('question_id'));

if ($question->user_id == auth()->id()) {
return response()->json([
'status' => 500,
'message' => 'The user is not allowed to vote to your question'
], 500);
}

$voice = Voice::firstOrCreate([
['user_id' => auth()->id(), 'question_id' => $request->post('question_id')],
['value' => $request->post('value')]
]);

if (!$voice->wasRecentlyCreated) {
if ($voice->value !== $request->post('value')) {
$voice->update([
'value' => $request->post('value')
]);
return response()->json([
'status' => 201,
'message' => 'Update your voice'
]);
}
return response()->json([
'status' => 500,
'message' => 'The user is not allowed to vote more than once'
]);
}

return response()->json([
'status'=>201,
'message'=>'update your voice'
'status' => 200,
'message' => 'Voting completed successfully'
]);
}

$question->voice()->create([
'user_id'=>auth()->id(),
'value'=>$request->post('value')
]);

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