feat: add quetions feature

This commit is contained in:
2025-09-08 17:11:52 +03:30
parent 4c933782f4
commit 2e9a278a53
10 changed files with 384 additions and 0 deletions
+134
View File
@@ -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' => 'سوال حذف شد']);
}
}