feat: add quetions feature
This commit is contained in:
@@ -0,0 +1,134 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
use App\Models\Question;
|
||||||
|
use App\Models\Tag;
|
||||||
|
use App\Models\Category;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class QuestionController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Display a listing of the resource.
|
||||||
|
*/
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$query = Question::with(['tags', 'category']);
|
||||||
|
|
||||||
|
|
||||||
|
// تابع برای نرمالسازی رشته فارسی
|
||||||
|
$normalize = function ($string) {
|
||||||
|
// تبدیل نیمفاصله به فاصله معمولی
|
||||||
|
$string = str_replace('', ' ', $string);
|
||||||
|
// حذف فاصلههای اضافی
|
||||||
|
$string = preg_replace('/\s+/', ' ', $string);
|
||||||
|
return trim($string);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
if ($request->has('tag')) {
|
||||||
|
$tag = $normalize($request->tag);
|
||||||
|
$query->whereHas('tags', function ($q) use ($tag) {
|
||||||
|
$q->whereRaw("REPLACE(name, '', ' ') LIKE ?", ["%{$tag}%"]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has('category')) {
|
||||||
|
$category = $normalize($request->category);
|
||||||
|
$query->whereHas('category', function ($q) use ($category) {
|
||||||
|
$q->whereRaw("REPLACE(name, '', ' ') LIKE ?", ["%{$category}%"]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json($query->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a newly created resource in storage.
|
||||||
|
*/
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
$data = $request->validate([
|
||||||
|
'title' => 'required|string',
|
||||||
|
'category' => 'nullable|string',
|
||||||
|
'tags' => 'nullable|array',
|
||||||
|
'tags.*' => 'string'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$category = null;
|
||||||
|
if (!empty($data['category'])) {
|
||||||
|
$category = Category::firstOrCreate(['name' => $data['category']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$question = Question::create([
|
||||||
|
'title' => $data['title'],
|
||||||
|
'category_id' => $category?->id
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!empty($data['tags'])) {
|
||||||
|
$tagIds = [];
|
||||||
|
foreach ($data['tags'] as $tagName) {
|
||||||
|
$tag = Tag::firstOrCreate(['name' => $tagName]);
|
||||||
|
$tagIds[] = $tag->id;
|
||||||
|
}
|
||||||
|
$question->tags()->sync($tagIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json($question->load(['tags', 'category']), 201);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the specified resource.
|
||||||
|
*/
|
||||||
|
public function show(Question $question)
|
||||||
|
{
|
||||||
|
return response()->json($question->load(['tags', 'category']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the specified resource in storage.
|
||||||
|
*/
|
||||||
|
public function update(Request $request, Question $question)
|
||||||
|
{
|
||||||
|
$data = $request->validate([
|
||||||
|
'title' => 'sometimes|string',
|
||||||
|
'category' => 'nullable|string',
|
||||||
|
'tags' => 'nullable|array',
|
||||||
|
'tags.*' => 'string'
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (isset($data['title'])) {
|
||||||
|
$question->update(['title' => $data['title']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists('category', $data)) {
|
||||||
|
if ($data['category']) {
|
||||||
|
$category = Category::firstOrCreate(['name' => $data['category']]);
|
||||||
|
$question->update(['category_id' => $category->id]);
|
||||||
|
} else {
|
||||||
|
$question->update(['category_id' => null]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($data['tags'])) {
|
||||||
|
$tagIds = [];
|
||||||
|
foreach ($data['tags'] as $tagName) {
|
||||||
|
$tag = Tag::firstOrCreate(['name' => $tagName]);
|
||||||
|
$tagIds[] = $tag->id;
|
||||||
|
}
|
||||||
|
$question->tags()->sync($tagIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json($question->load(['tags', 'category']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the specified resource from storage.
|
||||||
|
*/
|
||||||
|
public function destroy(Question $question)
|
||||||
|
{
|
||||||
|
$question->delete();
|
||||||
|
return response()->json(['message' => 'سوال حذف شد']);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Category extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = ['name'];
|
||||||
|
|
||||||
|
public function questions()
|
||||||
|
{
|
||||||
|
return $this->hasMany(Question::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Question extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = ['title', 'category_id'];
|
||||||
|
|
||||||
|
public function tags()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Tag::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function category()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Category::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Tag extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = ['name'];
|
||||||
|
|
||||||
|
public function questions()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Question::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('categories', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name')->unique();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('categories');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('tags', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name')->unique();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('tags');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('questions', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('title');
|
||||||
|
$table->foreignId('category_id')->nullable()->constrained()->nullOnDelete();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('questions');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('question_tag', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('question_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->foreignId('tag_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('question_tag');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use App\Models\Question;
|
||||||
|
use App\Models\Tag;
|
||||||
|
use App\Models\Category;
|
||||||
|
|
||||||
|
class QuestionSeeder extends Seeder
|
||||||
|
{
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
$questions = [
|
||||||
|
['مدتیه احساس میکنم هیچ انگیزهای برای کار و زندگی ندارم.', 'افسردگی', ['افسردگی', 'بیانگیزگی']],
|
||||||
|
['شبها نمیتونم راحت بخوابم و مدام فکرم مشغوله.', 'اضطراب', ['بیخوابی', 'اضطراب']],
|
||||||
|
['همیشه نگران آیندهام هستم و این باعث میشه تمرکز نداشته باشم.', 'اضطراب', ['اضطراب', 'نگرانی']],
|
||||||
|
['در جمع آدمها حس خجالت و استرس زیادی دارم.', 'اضطراب اجتماعی', ['اضطراب اجتماعی', 'خجالت']],
|
||||||
|
['خیلی زود از کوره در میرم و عصبی میشم.', 'کنترل احساسات', ['عصبانیت', 'کنترل احساسات']],
|
||||||
|
['هیچ لذتی از کارهایی که قبلاً دوست داشتم نمیبرم.', 'افسردگی', ['افسردگی', 'بیانگیزگی']],
|
||||||
|
['همیشه احساس میکنم اعتماد به نفس کافی ندارم.', 'اعتماد به نفس', ['اعتماد به نفس', 'خودباوری']],
|
||||||
|
['در تصمیمگیریها خیلی شک و تردید دارم.', 'تصمیمگیری', ['تصمیمگیری', 'تردید']],
|
||||||
|
['بعد از شکست یا اشتباه مدتها خودم رو سرزنش میکنم.', 'عزت نفس', ['خودسرزنشگری', 'عزت نفس']],
|
||||||
|
['مدتیه احساس تنهایی زیادی دارم حتی وقتی بین آدمها هستم.', 'تنهایی', ['تنهایی', 'افسردگی']],
|
||||||
|
['در رابطه عاطفیام مدام احساس ناامنی دارم.', 'روابط', ['رابطه عاطفی', 'ناامنی']],
|
||||||
|
['انگیزهای برای ادامه تحصیل یا کار ندارم.', 'بیانگیزگی', ['بیانگیزگی', 'آینده شغلی']],
|
||||||
|
['وقتی با خانوادهام حرف میزنم خیلی زود بحثمون به دعوا میکشه.', 'خانواده', ['خانواده', 'ارتباط مؤثر']],
|
||||||
|
['نمیتونم احساساتم رو به دیگران بگم.', 'مهارت ارتباطی', ['بیان احساسات', 'مهارت ارتباطی']],
|
||||||
|
['استرس مصاحبه شغلی یا ارائه باعث میشه عملکردم ضعیف باشه.', 'شغل', ['اضطراب عملکرد', 'شغل']],
|
||||||
|
['خیلی به تأیید دیگران وابستهام.', 'اعتماد به نفس', ['وابستگی', 'عزت نفس']],
|
||||||
|
['احساس میکنم هیچ هدفی در زندگی ندارم.', 'بیهدفی', ['بیهدفی', 'افسردگی']],
|
||||||
|
['مدام گذشته رو مرور میکنم و نمیتونم رها کنم.', 'رها کردن', ['گذشته', 'رها کردن']],
|
||||||
|
['در محیط کار مدام حس میکنم دیگران بهتر از من هستند.', 'اعتماد به نفس', ['مقایسه', 'اعتماد به نفس']],
|
||||||
|
['از تغییر میترسم و ترجیح میدم همه چیز ثابت بمونه.', 'رشد فردی', ['ترس از تغییر', 'رشد فردی']],
|
||||||
|
['وقتی تحت فشارم، دچار تپش قلب و لرزش میشم.', 'اضطراب', ['اضطراب', 'علائم فیزیکی']],
|
||||||
|
['مدتیه تمرکز و حافظهام خیلی کم شده.', 'تمرکز', ['تمرکز', 'حافظه']],
|
||||||
|
['احساس میکنم کسی منو درک نمیکنه.', 'تنهایی', ['تنهایی', 'عدم درک']],
|
||||||
|
['توی رابطهها زود وابسته میشم و سخت جدا میشم.', 'روابط', ['وابستگی عاطفی', 'روابط']],
|
||||||
|
['بعد از بیدار شدن هم هنوز احساس خستگی دارم.', 'خستگی', ['خستگی', 'بیانرژی']],
|
||||||
|
['برای نه گفتن به دیگران مشکل دارم.', 'مرزبندی', ['مرزبندی', 'نه گفتن']],
|
||||||
|
['مدام حس میکنم کارهام کافی و خوب نیست.', 'کمالگرایی', ['کمالگرایی', 'خودانتقادی']],
|
||||||
|
['وقتی مشکلی پیش میاد نمیتونم آرامش خودمو حفظ کنم.', 'مدیریت استرس', ['مدیریت استرس', 'کنترل احساسات']],
|
||||||
|
['مدام نگران سلامت خودم یا اطرافیانم هستم.', 'اضطراب سلامت', ['اضطراب سلامت', 'نگرانی']],
|
||||||
|
['نمیدونم چطور با شکستها کنار بیام.', 'تابآوری', ['شکست', 'تابآوری']],
|
||||||
|
['وقتی تنها هستم احساس ترس یا اضطراب دارم.', 'ترس', ['ترس', 'تنهایی']],
|
||||||
|
['نمیتونم زمانم رو درست مدیریت کنم و همیشه کارهام عقب میفته.', 'بهرهوری', ['مدیریت زمان', 'بهرهوری']],
|
||||||
|
['در جمعهای خانوادگی احساس معذب بودن میکنم.', 'اضطراب اجتماعی', ['اضطراب اجتماعی', 'خانواده']],
|
||||||
|
['گاهی حس میکنم ارزشمند نیستم.', 'عزت نفس', ['عزت نفس', 'خودباوری']],
|
||||||
|
['وقتی کسی منو نقد میکنه خیلی ناراحت میشم.', 'اعتماد به نفس', ['انتقادپذیری', 'اعتماد به نفس']],
|
||||||
|
['مدام احساس میکنم تحت قضاوت دیگرانم.', 'اضطراب اجتماعی', ['ترس از قضاوت', 'اضطراب اجتماعی']],
|
||||||
|
['وقتی اتفاق بدی میفته مدتها درگیرش میشم.', 'اضطراب', ['نشخوار فکری', 'اضطراب']],
|
||||||
|
['انگیزه شروع کارهای جدید رو ندارم.', 'بیانگیزگی', ['بیانگیزگی', 'شروع کار']],
|
||||||
|
['نمیتونم روی یک موضوع برای مدت طولانی تمرکز کنم.', 'تمرکز', ['تمرکز', 'حواسپرتی']],
|
||||||
|
['مدام حس میکنم برای همه باید کاری انجام بدم حتی اگه خسته باشم.', 'مرزبندی', ['مرزبندی', 'خستگی روانی']],
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($questions as [$title, $categoryName, $tagNames]) {
|
||||||
|
$category = Category::firstOrCreate(['name' => $categoryName]);
|
||||||
|
$question = Question::create([
|
||||||
|
'title' => $title,
|
||||||
|
'category_id' => $category->id
|
||||||
|
]);
|
||||||
|
|
||||||
|
foreach ($tagNames as $tagName) {
|
||||||
|
$tag = Tag::firstOrCreate(['name' => $tagName]);
|
||||||
|
$question->tags()->attach($tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
use App\Http\Controllers\MoodController;
|
use App\Http\Controllers\MoodController;
|
||||||
use App\Http\Controllers\WorryController;
|
use App\Http\Controllers\WorryController;
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use App\Http\Controllers\QuestionController;
|
||||||
|
|
||||||
Route::get('/test-hash', function() {
|
Route::get('/test-hash', function() {
|
||||||
$plain = 'amnk1380';
|
$plain = 'amnk1380';
|
||||||
@@ -87,4 +88,13 @@
|
|||||||
Route::delete('/{id}', 'destroy'); // Delete worry
|
Route::delete('/{id}', 'destroy'); // Delete worry
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
/// questions feature
|
||||||
|
|
||||||
|
Route::get('/questions', [QuestionController::class, 'index']);
|
||||||
|
Route::post('/questions', [QuestionController::class, 'store']);
|
||||||
|
Route::get('/questions/{question}', [QuestionController::class, 'show']);
|
||||||
|
Route::put('/questions/{question}', [QuestionController::class, 'update']);
|
||||||
|
Route::delete('/questions/{question}', [QuestionController::class, 'destroy']);
|
||||||
|
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user