feat: add faq
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\FaqCategory;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class FaqCategoryController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = FaqCategory::query()->withCount('faqs');
|
||||
|
||||
if (!$request->boolean('include_inactive')) {
|
||||
$query->where('is_active', true);
|
||||
}
|
||||
|
||||
return response()->json($query->orderBy('order')->get());
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
return response()->json(FaqCategory::with('faqs')->withCount('faqs')->findOrFail($id));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'order' => 'nullable|integer',
|
||||
'is_active' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
$category = FaqCategory::create($data);
|
||||
|
||||
return response()->json(['message' => 'FAQ category created', 'category' => $category], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$category = FaqCategory::findOrFail($id);
|
||||
|
||||
$data = $request->validate([
|
||||
'name' => 'sometimes|string|max:255',
|
||||
'order' => 'nullable|integer',
|
||||
'is_active' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
$category->update($data);
|
||||
|
||||
return response()->json(['message' => 'FAQ category updated', 'category' => $category]);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
FaqCategory::findOrFail($id)->delete(); // faqs cascade
|
||||
|
||||
return response()->json(['message' => 'FAQ category deleted']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Faq;
|
||||
use App\Models\FaqCategory;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class FaqController extends Controller
|
||||
{
|
||||
// APP: active FAQ categories, each with their active questions (grouped).
|
||||
// Pass ?include_inactive=1 (admin) to get everything.
|
||||
public function index(Request $request)
|
||||
{
|
||||
$includeInactive = $request->boolean('include_inactive');
|
||||
|
||||
$categories = FaqCategory::query()
|
||||
->with(['faqs' => function ($q) use ($includeInactive) {
|
||||
if (!$includeInactive) {
|
||||
$q->where('is_active', true);
|
||||
}
|
||||
$q->orderBy('order');
|
||||
}])
|
||||
->when(!$includeInactive, fn ($q) => $q->where('is_active', true))
|
||||
->orderBy('order')
|
||||
->get();
|
||||
|
||||
return response()->json($categories);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
return response()->json(Faq::with('category')->findOrFail($id));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'faq_category_id' => 'required|exists:faq_categories,id',
|
||||
'question' => 'required|string|max:500',
|
||||
'answer' => 'required|string',
|
||||
'order' => 'nullable|integer',
|
||||
'is_active' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
$faq = Faq::create($data);
|
||||
|
||||
return response()->json(['message' => 'FAQ created', 'faq' => $faq->load('category')], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$faq = Faq::findOrFail($id);
|
||||
|
||||
$data = $request->validate([
|
||||
'faq_category_id' => 'sometimes|exists:faq_categories,id',
|
||||
'question' => 'sometimes|string|max:500',
|
||||
'answer' => 'sometimes|string',
|
||||
'order' => 'nullable|integer',
|
||||
'is_active' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
$faq->update($data);
|
||||
|
||||
return response()->json(['message' => 'FAQ updated', 'faq' => $faq->load('category')]);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
Faq::findOrFail($id)->delete();
|
||||
|
||||
return response()->json(['message' => 'FAQ deleted']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Faq extends Model
|
||||
{
|
||||
protected $fillable = ['faq_category_id', 'question', 'answer', 'order', 'is_active'];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
'order' => 'integer',
|
||||
];
|
||||
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FaqCategory::class, 'faq_category_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class FaqCategory extends Model
|
||||
{
|
||||
protected $fillable = ['name', 'order', 'is_active'];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
'order' => 'integer',
|
||||
];
|
||||
|
||||
public function faqs(): HasMany
|
||||
{
|
||||
return $this->hasMany(Faq::class)->orderBy('order');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
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('faq_categories', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->integer('order')->default(0);
|
||||
$table->boolean('is_active')->default(true);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('faq_categories');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
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) {
|
||||
$table->id();
|
||||
$table->foreignId('faq_category_id')->constrained('faq_categories')->cascadeOnDelete();
|
||||
$table->string('question');
|
||||
$table->text('answer');
|
||||
$table->integer('order')->default(0);
|
||||
$table->boolean('is_active')->default(true);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('faqs');
|
||||
}
|
||||
};
|
||||
@@ -15,6 +15,8 @@
|
||||
use App\Http\Controllers\SurveyQuestionController;
|
||||
use App\Http\Controllers\ChatTopicController;
|
||||
use App\Http\Controllers\AppFeedbackController;
|
||||
use App\Http\Controllers\FaqController;
|
||||
use App\Http\Controllers\FaqCategoryController;
|
||||
use App\Http\Controllers\SliderController;
|
||||
use App\Http\Controllers\SceneController;
|
||||
use App\Http\Controllers\ThemeController;
|
||||
@@ -136,6 +138,15 @@
|
||||
Route::apiResource('chat-topics', ChatTopicController::class);
|
||||
|
||||
|
||||
/// FAQ (سوالات متداول) — app reads grouped /faqs; admin manages categories & items
|
||||
Route::get('/faqs', [FaqController::class, 'index']); // app: categories + their questions
|
||||
Route::apiResource('faq-categories', FaqCategoryController::class);
|
||||
Route::post('/faqs', [FaqController::class, 'store']);
|
||||
Route::get('/faqs/{id}', [FaqController::class, 'show']);
|
||||
Route::put('/faqs/{id}', [FaqController::class, 'update']);
|
||||
Route::delete('/faqs/{id}', [FaqController::class, 'destroy']);
|
||||
|
||||
|
||||
/// app feedback — ideas & reviews (نظرات و ایدهها)
|
||||
Route::get('/app-feedback', [AppFeedbackController::class, 'mine']);
|
||||
Route::post('/app-feedback', [AppFeedbackController::class, 'store']);
|
||||
|
||||
Reference in New Issue
Block a user