Skip to content

Commit

Permalink
#14 - FAQ management (#49)
Browse files Browse the repository at this point in the history
* #14 - wip

* #14 - wip

* #14 - added tests

* #14 - fix

* #14 - fix
  • Loading branch information
KarolZygadlo authored Oct 1, 2023
1 parent 47e333a commit da58f9b
Show file tree
Hide file tree
Showing 16 changed files with 922 additions and 4 deletions.
64 changes: 64 additions & 0 deletions app/Http/Controllers/FaqController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Http\Requests\FaqRequest;
use App\Models\Faq;
use Illuminate\Http\RedirectResponse;
use Inertia\Response;

class FaqController extends Controller
{
public function index(): Response
{
$faqs = Faq::query()
->orderByDesc("created_at")
->get();

return inertia("Dashboard/FAQ/Index", [
"faqs" => $faqs,
"total" => Faq::query()->count(),
"lastUpdate" => Faq::query()->orderByDesc("updated_at")->first()?->updated_at->diffForHumans(),
]);
}

public function create(): Response
{
return inertia("Dashboard/FAQ/Create");
}

public function store(FaqRequest $request): RedirectResponse
{
Faq::query()->create($request->validated());

return redirect()
->route("faqs.index")
->with("success", "Dodano FAQ");
}

public function edit(Faq $faq): Response
{
return inertia("Dashboard/FAQ/Edit", [
"faq" => $faq,
]);
}

public function update(FaqRequest $request, Faq $faq): RedirectResponse
{
$faq->update($request->validated());

return redirect()
->route("faqs.index")
->with("success", "Zaktualizowano FAQ");
}

public function destroy(Faq $faq): RedirectResponse
{
$faq->delete();

return redirect()->back()
->with("success", "Usunięto FAQ");
}
}
18 changes: 18 additions & 0 deletions app/Http/Requests/FaqRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class FaqRequest extends FormRequest
{
public function rules(): array
{
return [
"question" => ["required", "max:255"],
"answer" => ["required", "max:65000"],
];
}
}
34 changes: 34 additions & 0 deletions app/Models/Faq.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Stevebauman\Purify\Facades\Purify;

/**
* @property string $id
* @property string $question
* @property string $answer
* @property Carbon $created_at
* @property Carbon $updated_at
*/
class Faq extends Model
{
use HasFactory;
use HasUlids;

protected $guarded = [];

protected function answer(): Attribute
{
return Attribute::make(
set: fn(?string $value): string => Purify::clean($value),
);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"inertiajs/inertia-laravel": "^0.6.9",
"laravel/framework": "^10.13.0",
"laravel/sanctum": "^3.2.5",
"laravel/tinker": "^2.8.1"
"laravel/tinker": "^2.8.1",
"stevebauman/purify": "^6.0"
},
"require-dev": {
"blumilksoftware/codestyle": "^2.3.0",
Expand Down
131 changes: 129 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions database/factories/FaqFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\Faq;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<Faq>
*/
class FaqFactory extends Factory
{
public function definition(): array
{
return [
"question" => fake()->sentence(),
"answer" => fake()->realText(),
];
}
}
24 changes: 24 additions & 0 deletions database/migrations/2023_09_27_193906_create_faqs_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
Schema::create("faqs", function (Blueprint $table): void {
$table->ulid("id")->primary();
$table->string("question");
$table->longText("answer");
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists("faqs");
}
};
Loading

0 comments on commit da58f9b

Please sign in to comment.