-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* create models * ignore .vite folder * create basic quiz CRUD * fix code style * create tests for answer controller * create tests for question controller * fix edit locked tests * fix typo * create tests for quiz controller * fix code style * improve question delete test * add marking answers as correct/incorrect * add answer cloning * fix typo * add question cloning * add quiz cloning * fix database config * fix linter errors * remove inertia comments * fix code style * replace authorize with can middleware * move user to setUp * move polices to route * fix code style
- Loading branch information
1 parent
787975d
commit ef852c0
Showing
44 changed files
with
2,118 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ google-credentials.json | |
.idea/ | ||
.vscode/ | ||
.composer | ||
.vite | ||
/public/build/ | ||
supervisord.pid | ||
*.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\AnswerRequest; | ||
use App\Http\Resources\AnswerResource; | ||
use App\Models\Answer; | ||
use App\Models\Question; | ||
use Illuminate\Http\RedirectResponse; | ||
use Inertia\Inertia; | ||
use Inertia\Response; | ||
|
||
class QuestionAnswerController extends Controller | ||
{ | ||
public function index(Question $question): Response | ||
{ | ||
return Inertia::render("Answer/Index", [ | ||
"answers" => AnswerResource::collection($question->answers), | ||
]); | ||
} | ||
|
||
public function store(Question $question, AnswerRequest $request): RedirectResponse | ||
{ | ||
Answer::query() | ||
->make($request->validated()) | ||
->question()->associate($question) | ||
->save(); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Answer added successfully"); | ||
} | ||
|
||
public function show(Answer $answer): Response | ||
{ | ||
return Inertia::render("Answer/Show", ["answer" => new AnswerResource($answer)]); | ||
} | ||
|
||
public function markAsCorrect(Answer $answer): RedirectResponse | ||
{ | ||
$answer->question->correctAnswer()->associate($answer)->save(); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Answer marked as correct"); | ||
} | ||
|
||
public function markAsInvalid(Answer $answer): RedirectResponse | ||
{ | ||
if ($answer->isCorrect) { | ||
$answer->question->correct_answer_id = null; | ||
$answer->save(); | ||
} | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Answer marked as incorrect"); | ||
} | ||
|
||
public function update(AnswerRequest $request, Answer $answer): RedirectResponse | ||
{ | ||
$answer->update($request->validated()); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Answer updated"); | ||
} | ||
|
||
public function destroy(Answer $answer): RedirectResponse | ||
{ | ||
$answer->delete(); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Answer deleted"); | ||
} | ||
|
||
public function clone(Answer $answer, Question $question): RedirectResponse | ||
{ | ||
$answer->cloneTo($question); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Answer cloned"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\QuizRequest; | ||
use App\Http\Resources\QuizResource; | ||
use App\Models\Quiz; | ||
use Carbon\Carbon; | ||
use Illuminate\Http\RedirectResponse; | ||
use Inertia\Inertia; | ||
use Inertia\Response; | ||
|
||
class QuizController extends Controller | ||
{ | ||
public function index(): Response | ||
{ | ||
$quizzes = Quiz::query() | ||
->with("questions.answers") | ||
->get(); | ||
|
||
return Inertia::render("Quiz/Index", ["quizzes" => QuizResource::collection($quizzes)]); | ||
} | ||
|
||
public function store(QuizRequest $request): RedirectResponse | ||
{ | ||
Quiz::query()->create($request->validated()); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Quiz added successfully"); | ||
} | ||
|
||
public function show(int $quiz): Response | ||
{ | ||
$quiz = Quiz::query() | ||
->with("questions.answers") | ||
->findOrFail($quiz); | ||
|
||
return Inertia::render("Quiz/Show", ["quiz" => new QuizResource($quiz)]); | ||
} | ||
|
||
public function update(QuizRequest $request, Quiz $quiz): RedirectResponse | ||
{ | ||
$quiz->update($request->validated()); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Quiz updated"); | ||
} | ||
|
||
public function lock(Quiz $quiz): RedirectResponse | ||
{ | ||
$quiz->locked_at = Carbon::now(); | ||
$quiz->save(); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Quiz locked"); | ||
} | ||
|
||
public function destroy(Quiz $quiz): RedirectResponse | ||
{ | ||
$quiz->delete(); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Quiz deleted"); | ||
} | ||
|
||
public function clone(Quiz $quiz): RedirectResponse | ||
{ | ||
$quiz->clone(); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Quiz cloned"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\QuestionRequest; | ||
use App\Http\Resources\QuestionResource; | ||
use App\Models\Question; | ||
use App\Models\Quiz; | ||
use Illuminate\Http\RedirectResponse; | ||
use Inertia\Inertia; | ||
use Inertia\Response; | ||
|
||
class QuizQuestionController extends Controller | ||
{ | ||
public function index(Quiz $quiz): Response | ||
{ | ||
$questions = $quiz->questions() | ||
->with("answers") | ||
->get(); | ||
|
||
return Inertia::render("Question/Index", [ | ||
"questions" => QuestionResource::collection($questions), | ||
]); | ||
} | ||
|
||
public function store(Quiz $quiz, QuestionRequest $request): RedirectResponse | ||
{ | ||
Question::query() | ||
->make($request->validated()) | ||
->quiz()->associate($quiz) | ||
->save(); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Question added successfully"); | ||
} | ||
|
||
public function show(int $question): Response | ||
{ | ||
$test = Question::query() | ||
->with("answers") | ||
->findOrFail($question); | ||
|
||
return Inertia::render("Question/Show", ["question" => new QuestionResource($test)]); | ||
} | ||
|
||
public function update(QuestionRequest $request, Question $question): RedirectResponse | ||
{ | ||
$question->update($request->validated()); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Question updated"); | ||
} | ||
|
||
public function destroy(Question $question): RedirectResponse | ||
{ | ||
$question->delete(); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Question deleted"); | ||
} | ||
|
||
public function clone(Question $question, Quiz $quiz): RedirectResponse | ||
{ | ||
$question->cloneTo($quiz); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Question cloned"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Illuminate\Http\Request; | ||
use Inertia\Middleware; | ||
|
||
class HandleInertiaRequests extends Middleware | ||
{ | ||
/** @var string */ | ||
protected $rootView = "app"; | ||
|
||
public function version(Request $request): ?string | ||
{ | ||
return parent::version($request); | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function share(Request $request): array | ||
{ | ||
return array_merge(parent::share($request), []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class AnswerRequest extends FormRequest | ||
{ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @return array<string, ValidationRule|array|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
"text" => ["required", "string"], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class QuestionRequest extends FormRequest | ||
{ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @return array<string, ValidationRule|array|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
"text" => ["required", "string"], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class QuizRequest extends FormRequest | ||
{ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @return array<string, ValidationRule|array|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
"name" => ["required", "string"], | ||
]; | ||
} | ||
} |
Oops, something went wrong.