feat: add tags
This commit is contained in:
@@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
use App\Models\Media;
|
use App\Models\Media;
|
||||||
use App\Models\Category;
|
use App\Models\Category;
|
||||||
|
use App\Models\Tag;
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
@@ -24,6 +26,9 @@ public function store(Request $request)
|
|||||||
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:50480',
|
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:50480',
|
||||||
'external_url' => 'nullable|string',
|
'external_url' => 'nullable|string',
|
||||||
'visibility' => 'nullable|in:public,private',
|
'visibility' => 'nullable|in:public,private',
|
||||||
|
|
||||||
|
'tags' => 'nullable|array',
|
||||||
|
'tags.*' => 'string',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$categoryId = $data['category_id'] ?? null;
|
$categoryId = $data['category_id'] ?? null;
|
||||||
@@ -51,17 +56,26 @@ public function store(Request $request)
|
|||||||
'duration' => $data['duration'] ?? null,
|
'duration' => $data['duration'] ?? null,
|
||||||
'visibility' => $data['visibility'] ?? 'public',
|
'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([
|
return response()->json([
|
||||||
'message' => 'Media created successfully',
|
'message' => 'Media created successfully',
|
||||||
'media' => $media->load(['image', 'category']),
|
'media' => $media->load(['image', 'category' , 'tags']),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET all visible media
|
// GET all visible media
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$media = Media::with(['image', 'category', 'myNote'])
|
$media = Media::with(['image', 'category', 'myNote' , 'tags'])
|
||||||
->where('visibility', 'public')
|
->where('visibility', 'public')
|
||||||
->orWhere('user_id', auth()->id())
|
->orWhere('user_id', auth()->id())
|
||||||
->get();
|
->get();
|
||||||
@@ -69,6 +83,17 @@ public function index()
|
|||||||
return response()->json($media);
|
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
|
// UPDATE media
|
||||||
public function update(Request $request, $id)
|
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',
|
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:50480',
|
||||||
'external_url' => 'nullable|string',
|
'external_url' => 'nullable|string',
|
||||||
'visibility' => 'nullable|in:public,private',
|
'visibility' => 'nullable|in:public,private',
|
||||||
|
'tags' => 'nullable|array',
|
||||||
|
'tags.*' => 'string',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// --- handle auto-create category ---
|
// --- handle auto-create category ---
|
||||||
@@ -113,10 +140,20 @@ public function update(Request $request, $id)
|
|||||||
|
|
||||||
// --- update media ---
|
// --- update media ---
|
||||||
$media->update($data);
|
$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([
|
return response()->json([
|
||||||
'message' => 'Media updated successfully',
|
'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
|
// GET saved
|
||||||
public function 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)
|
public function storeNote(Request $request, $mediaId)
|
||||||
|
|||||||
@@ -28,7 +28,10 @@ public function category()
|
|||||||
{
|
{
|
||||||
return $this->belongsTo(Category::class);
|
return $this->belongsTo(Category::class);
|
||||||
}
|
}
|
||||||
|
public function tags()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Tag::class, 'media_tag');
|
||||||
|
}
|
||||||
public function notes()
|
public function notes()
|
||||||
{
|
{
|
||||||
return $this->morphMany(Note::class, 'noteable');
|
return $this->morphMany(Note::class, 'noteable');
|
||||||
|
|||||||
@@ -12,5 +12,9 @@ public function questions()
|
|||||||
{
|
{
|
||||||
return $this->belongsToMany(Question::class);
|
return $this->belongsToMany(Question::class);
|
||||||
}
|
}
|
||||||
|
public function media()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Media::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('media_tag', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('media_id')->constrained()->onDelete('cascade');
|
||||||
|
$table->foreignId('tag_id')->constrained()->onDelete('cascade');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('media_tag');
|
||||||
|
}
|
||||||
|
};
|
||||||
+1
-1
@@ -139,7 +139,7 @@
|
|||||||
Route::get('/media', [MediaController::class, 'index']);
|
Route::get('/media', [MediaController::class, 'index']);
|
||||||
Route::put('/media/{id}', [MediaController::class, 'update']);
|
Route::put('/media/{id}', [MediaController::class, 'update']);
|
||||||
Route::delete('/media/{id}', [MediaController::class, 'destroy']);
|
Route::delete('/media/{id}', [MediaController::class, 'destroy']);
|
||||||
|
Route::get('/media/{id}', [MediaController::class, 'show']);
|
||||||
Route::post('/media/{id}/save', [MediaController::class, 'saveMedia']);
|
Route::post('/media/{id}/save', [MediaController::class, 'saveMedia']);
|
||||||
Route::get('/media/saved', [MediaController::class, 'saved']);
|
Route::get('/media/saved', [MediaController::class, 'saved']);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user