feat: add tags
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
|
||||
use App\Models\Media;
|
||||
use App\Models\Category;
|
||||
use App\Models\Tag;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
@@ -24,6 +26,9 @@ public function store(Request $request)
|
||||
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:50480',
|
||||
'external_url' => 'nullable|string',
|
||||
'visibility' => 'nullable|in:public,private',
|
||||
|
||||
'tags' => 'nullable|array',
|
||||
'tags.*' => 'string',
|
||||
]);
|
||||
|
||||
$categoryId = $data['category_id'] ?? null;
|
||||
@@ -51,17 +56,26 @@ public function store(Request $request)
|
||||
'duration' => $data['duration'] ?? null,
|
||||
'visibility' => $data['visibility'] ?? 'public',
|
||||
]);
|
||||
if (!empty($data['tags'])) {
|
||||
$tagIds = [];
|
||||
|
||||
foreach ($data['tags'] as $tagName) {
|
||||
$tag = Tag::firstOrCreate(['name' => $tagName]);
|
||||
$tagIds[] = $tag->id;
|
||||
}
|
||||
|
||||
$media->tags()->sync($tagIds);
|
||||
}
|
||||
return response()->json([
|
||||
'message' => 'Media created successfully',
|
||||
'media' => $media->load(['image', 'category']),
|
||||
'media' => $media->load(['image', 'category' , 'tags']),
|
||||
]);
|
||||
}
|
||||
|
||||
// GET all visible media
|
||||
public function index()
|
||||
{
|
||||
$media = Media::with(['image', 'category', 'myNote'])
|
||||
$media = Media::with(['image', 'category', 'myNote' , 'tags'])
|
||||
->where('visibility', 'public')
|
||||
->orWhere('user_id', auth()->id())
|
||||
->get();
|
||||
@@ -69,6 +83,17 @@ public function index()
|
||||
return response()->json($media);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$media = Media::with(['image', 'category', 'myNote' , 'tags'])
|
||||
->where('id', $id)
|
||||
->where('visibility', 'public')
|
||||
->orWhere('user_id', auth()->id())
|
||||
->firstOrFail();
|
||||
|
||||
return response()->json($media);
|
||||
}
|
||||
|
||||
// UPDATE media
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
@@ -90,6 +115,8 @@ public function update(Request $request, $id)
|
||||
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:50480',
|
||||
'external_url' => 'nullable|string',
|
||||
'visibility' => 'nullable|in:public,private',
|
||||
'tags' => 'nullable|array',
|
||||
'tags.*' => 'string',
|
||||
]);
|
||||
|
||||
// --- handle auto-create category ---
|
||||
@@ -113,10 +140,20 @@ public function update(Request $request, $id)
|
||||
|
||||
// --- update media ---
|
||||
$media->update($data);
|
||||
if (isset($data['tags'])) {
|
||||
$tagIds = [];
|
||||
|
||||
foreach ($data['tags'] as $tagName) {
|
||||
$tag = Tag::firstOrCreate(['name' => $tagName]);
|
||||
$tagIds[] = $tag->id;
|
||||
}
|
||||
|
||||
$media->tags()->sync($tagIds);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Media updated successfully',
|
||||
'media' => $media->load(['image', 'category']),
|
||||
'media' => $media->load(['image', 'category' , 'tags']),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -145,7 +182,7 @@ public function saveMedia($id)
|
||||
// GET saved
|
||||
public function saved()
|
||||
{
|
||||
return auth()->user()->savedMedia()->with(['image','category' , 'myNote'])->get();
|
||||
return auth()->user()->savedMedia()->with(['image','category' , 'myNote' , 'tags'])->get();
|
||||
}
|
||||
|
||||
public function storeNote(Request $request, $mediaId)
|
||||
|
||||
Reference in New Issue
Block a user