Skip to content

Commit

Permalink
#13 - Basic CRUD for tests (#18)
Browse files Browse the repository at this point in the history
* 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
AmonDeShir authored Aug 13, 2024
1 parent 787975d commit ef852c0
Show file tree
Hide file tree
Showing 44 changed files with 2,118 additions and 18 deletions.
7 changes: 7 additions & 0 deletions .env.ci
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ QUEUE_CONNECTION=sync
SESSION_DRIVER=array
SESSION_LIFETIME=120
MAIL_MAILER=array

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=interns2024b
DB_USERNAME=interns2024b
DB_PASSWORD=password
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ google-credentials.json
.idea/
.vscode/
.composer
.vite
/public/build/
supervisord.pid
*.cache
3 changes: 3 additions & 0 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

abstract class Controller
{
use AuthorizesRequests;
}
88 changes: 88 additions & 0 deletions app/Http/Controllers/QuestionAnswerController.php
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");
}
}
80 changes: 80 additions & 0 deletions app/Http/Controllers/QuizController.php
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");
}
}
75 changes: 75 additions & 0 deletions app/Http/Controllers/QuizQuestionController.php
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");
}
}
27 changes: 27 additions & 0 deletions app/Http/Middleware/HandleInertiaRequests.php
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), []);
}
}
26 changes: 26 additions & 0 deletions app/Http/Requests/AnswerRequest.php
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"],
];
}
}
26 changes: 26 additions & 0 deletions app/Http/Requests/QuestionRequest.php
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"],
];
}
}
26 changes: 26 additions & 0 deletions app/Http/Requests/QuizRequest.php
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"],
];
}
}
Loading

0 comments on commit ef852c0

Please sign in to comment.