feat: add tags for options
This commit is contained in:
@@ -2,7 +2,10 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Media;
|
||||
use App\Models\SurveyAnswer;
|
||||
use App\Models\SurveyQuestion;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
@@ -12,7 +15,7 @@ class SurveyQuestionController extends Controller
|
||||
// USER: list active questions with their options and the current user's own answers.
|
||||
public function index(Request $request)
|
||||
{
|
||||
$questions = SurveyQuestion::with(['options', 'userAnswers'])
|
||||
$questions = SurveyQuestion::with(['options.tags', 'userAnswers'])
|
||||
->where('is_active', true)
|
||||
->orderBy('order')
|
||||
->get();
|
||||
@@ -23,7 +26,7 @@ public function index(Request $request)
|
||||
// USER: a single question with its options and the current user's own answers.
|
||||
public function show($id)
|
||||
{
|
||||
$question = SurveyQuestion::with(['options', 'userAnswers'])->findOrFail($id);
|
||||
$question = SurveyQuestion::with(['options.tags', 'userAnswers'])->findOrFail($id);
|
||||
|
||||
return response()->json($question);
|
||||
}
|
||||
@@ -32,7 +35,7 @@ public function show($id)
|
||||
public function adminIndex(Request $request)
|
||||
{
|
||||
$questions = SurveyQuestion::with([
|
||||
'options' => fn ($q) => $q->withCount('answers'),
|
||||
'options' => fn ($q) => $q->withCount('answers')->with('tags'),
|
||||
'answers' => fn ($q) => $q->with(['user', 'option']),
|
||||
])
|
||||
->orderBy('order')
|
||||
@@ -45,7 +48,7 @@ public function adminIndex(Request $request)
|
||||
public function adminShow($id)
|
||||
{
|
||||
$question = SurveyQuestion::with([
|
||||
'options' => fn ($q) => $q->withCount('answers'),
|
||||
'options' => fn ($q) => $q->withCount('answers')->with('tags'),
|
||||
'answers' => fn ($q) => $q->with(['user', 'option']),
|
||||
])
|
||||
->findOrFail($id);
|
||||
@@ -113,6 +116,8 @@ public function store(Request $request)
|
||||
'options.*.label' => 'required|string|max:255',
|
||||
'options.*.value' => 'nullable|string|max:255',
|
||||
'options.*.order' => 'nullable|integer',
|
||||
'options.*.tags' => 'nullable|array',
|
||||
'options.*.tags.*' => 'string',
|
||||
]);
|
||||
|
||||
$question = DB::transaction(function () use ($data) {
|
||||
@@ -131,7 +136,7 @@ public function store(Request $request)
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Question created successfully',
|
||||
'question' => $question->load('options'),
|
||||
'question' => $question->load('options.tags'),
|
||||
], 201);
|
||||
}
|
||||
|
||||
@@ -150,6 +155,8 @@ public function update(Request $request, $id)
|
||||
'options.*.label' => 'required_with:options|string|max:255',
|
||||
'options.*.value' => 'nullable|string|max:255',
|
||||
'options.*.order' => 'nullable|integer',
|
||||
'options.*.tags' => 'nullable|array',
|
||||
'options.*.tags.*' => 'string',
|
||||
]);
|
||||
|
||||
DB::transaction(function () use ($question, $data) {
|
||||
@@ -174,7 +181,7 @@ public function update(Request $request, $id)
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Question updated successfully',
|
||||
'question' => $question->load('options'),
|
||||
'question' => $question->load('options.tags'),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -236,14 +243,63 @@ public function answer(Request $request, $id)
|
||||
]);
|
||||
}
|
||||
|
||||
// USER: suggest media based on the tags attached to the options this user has chosen.
|
||||
public function suggestedMedia(Request $request)
|
||||
{
|
||||
$userId = auth()->id();
|
||||
|
||||
$answersQuery = SurveyAnswer::where('user_id', $userId);
|
||||
if ($request->filled('question_id')) {
|
||||
$answersQuery->where('survey_question_id', $request->question_id);
|
||||
}
|
||||
$optionIds = $answersQuery->pluck('survey_option_id');
|
||||
|
||||
if ($optionIds->isEmpty()) {
|
||||
return response()->json([]);
|
||||
}
|
||||
|
||||
// Collect the tags behind the chosen options.
|
||||
$tagIds = DB::table('survey_option_tag')
|
||||
->whereIn('survey_option_id', $optionIds)
|
||||
->pluck('tag_id')
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
if ($tagIds->isEmpty()) {
|
||||
return response()->json([]);
|
||||
}
|
||||
|
||||
// Media sharing those tags, ranked by how many of them match.
|
||||
$media = Media::query()
|
||||
->whereHas('tags', fn ($q) => $q->whereIn('tags.id', $tagIds))
|
||||
->withCount(['tags as match_count' => fn ($q) => $q->whereIn('tags.id', $tagIds)])
|
||||
->where(function ($q) use ($userId) {
|
||||
$q->where('visibility', 'public')->orWhere('user_id', $userId);
|
||||
})
|
||||
->with(['image', 'categories', 'subCategories', 'tags'])
|
||||
->orderByDesc('match_count')
|
||||
->orderByDesc('created_at')
|
||||
->get();
|
||||
|
||||
return response()->json($media);
|
||||
}
|
||||
|
||||
private function syncOptions(SurveyQuestion $question, array $options): void
|
||||
{
|
||||
foreach (array_values($options) as $i => $option) {
|
||||
$question->options()->create([
|
||||
$created = $question->options()->create([
|
||||
'label' => $option['label'],
|
||||
'value' => $option['value'] ?? null,
|
||||
'order' => $option['order'] ?? $i,
|
||||
]);
|
||||
|
||||
if (!empty($option['tags'])) {
|
||||
$tagIds = collect($option['tags'])
|
||||
->map(fn ($name) => Tag::firstOrCreate(['name' => $name])->id)
|
||||
->all();
|
||||
|
||||
$created->tags()->sync($tagIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user