278 lines
8.4 KiB
PHP
278 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Media;
|
|
use App\Models\Category;
|
|
use App\Models\Tag;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class MediaController extends Controller
|
|
{
|
|
// CREATE media
|
|
public function store(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'title' => 'required|string|max:255',
|
|
'caption' => 'nullable|string',
|
|
'type' => 'required|in:audio,video',
|
|
'category_id' => 'nullable|exists:categories,id',
|
|
'category_name' => 'nullable|string|max:255',
|
|
'image_id' => 'nullable|exists:images,id',
|
|
'duration' => 'nullable|integer',
|
|
|
|
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:512000',
|
|
'external_url' => 'nullable|string',
|
|
'visibility' => 'nullable|in:public,private',
|
|
|
|
'tags' => 'nullable|array',
|
|
'tags.*' => 'string',
|
|
]);
|
|
|
|
$categoryId = $data['category_id'] ?? null;
|
|
|
|
if (!$categoryId && isset($data['category_name'])) {
|
|
$category = Category::firstOrCreate([
|
|
'name' => $data['category_name']
|
|
]);
|
|
$categoryId = $category->id;
|
|
}
|
|
$path = null;
|
|
if ($request->hasFile('file')) {
|
|
$path = $request->file('file')->store('media', 'public');
|
|
}
|
|
|
|
$media = Media::create([
|
|
'user_id' => auth()->id(),
|
|
'title' => $data['title'],
|
|
'caption' => $data['caption'] ?? null,
|
|
'type' => $data['type'],
|
|
'file_path' => $path,
|
|
'external_url' => $data['external_url'] ?? null,
|
|
'image_id' => $data['image_id'] ?? null,
|
|
'category_id' => $categoryId,
|
|
'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' , 'tags']),
|
|
]);
|
|
}
|
|
public function index(Request $request)
|
|
{
|
|
$query = Media::with(['image', 'category', 'myNote', 'tags'])
|
|
->where(function($q) {
|
|
$q->where('visibility', 'public')
|
|
->orWhere('user_id', auth()->id());
|
|
});
|
|
if ($request->filled('category')) {
|
|
$query->where('category_id', $request->category);
|
|
}
|
|
if ($request->filled('tags')) {
|
|
$tags = explode(',', $request->tags);
|
|
|
|
$query->whereHas('tags', function($q) use ($tags) {
|
|
$q->whereIn('name', $tags);
|
|
});
|
|
}
|
|
if ($request->filled('search')) {
|
|
$search = $request->search;
|
|
|
|
$query->where(function($q) use ($search) {
|
|
|
|
// title & caption
|
|
$q->where('title', 'LIKE', "%$search%")
|
|
->orWhere('caption', 'LIKE', "%$search%")
|
|
|
|
// category (join)
|
|
->orWhereHas('category', function($c) use ($search) {
|
|
$c->where('name', 'LIKE', "%$search%");
|
|
})
|
|
|
|
// tags
|
|
->orWhereHas('tags', function($t) use ($search) {
|
|
$t->where('name', 'LIKE', "%$search%");
|
|
});
|
|
});
|
|
}
|
|
return response()->json(
|
|
$query->orderBy('created_at', 'desc')->get()
|
|
);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$media = Media::with(['image', 'category', 'myNote', 'tags'])
|
|
->where('id', $id)
|
|
->where(function($query) {
|
|
$query->where('visibility', 'public')
|
|
->orWhere('user_id', auth()->id());
|
|
})
|
|
->firstOrFail();
|
|
|
|
return response()->json([
|
|
'id' => $media->id,
|
|
'title' => $media->title,
|
|
'caption' => $media->caption,
|
|
'type' => $media->type,
|
|
'file_path' => $media->file_path,
|
|
'external_url' => $media->external_url,
|
|
'duration' => $media->duration,
|
|
'visibility' => $media->visibility,
|
|
'created_at' => $media->created_at,
|
|
'updated_at' => $media->updated_at,
|
|
'image' => $media->image,
|
|
'category' => $media->category,
|
|
'tags' => $media->tags,
|
|
'myNote' => $media->myNote,
|
|
'is_saved' => $media->is_saved,
|
|
]);
|
|
}
|
|
|
|
// UPDATE media
|
|
public function update(Request $request, $id)
|
|
{
|
|
$media = Media::where('id', $id)
|
|
->where('user_id', auth()->id())
|
|
->firstOrFail();
|
|
|
|
$data = $request->validate([
|
|
'title' => 'nullable|string',
|
|
'caption' => 'nullable|string',
|
|
'type' => 'nullable|in:audio,video',
|
|
|
|
// support both: category_id or category_name
|
|
'category_id' => 'nullable|exists:categories,id',
|
|
'category_name' => 'nullable|string|max:255',
|
|
|
|
'image_id' => 'nullable|exists:images,id',
|
|
'duration' => 'nullable|integer',
|
|
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:512000',
|
|
'external_url' => 'nullable|string',
|
|
'visibility' => 'nullable|in:public,private',
|
|
'tags' => 'nullable|array',
|
|
'tags.*' => 'string',
|
|
]);
|
|
|
|
// --- handle auto-create category ---
|
|
$categoryId = $data['category_id'] ?? $media->category_id;
|
|
|
|
if (isset($data['category_name'])) {
|
|
$category = Category::firstOrCreate([
|
|
'name' => $data['category_name']
|
|
]);
|
|
$categoryId = $category->id;
|
|
}
|
|
|
|
// --- handle file replace ---
|
|
if ($request->hasFile('file')) {
|
|
Storage::disk('public')->delete($media->file_path);
|
|
$data['file_path'] = $request->file('file')->store('media', 'public');
|
|
}
|
|
|
|
// --- Prepare update data ---
|
|
$updateData = [
|
|
'title' => $data['title'] ?? $media->title,
|
|
'caption' => $data['caption'] ?? $media->caption,
|
|
'type' => $data['type'] ?? $media->type,
|
|
'category_id' => $categoryId,
|
|
'duration' => $data['duration'] ?? $media->duration,
|
|
'external_url' => $data['external_url'] ?? $media->external_url,
|
|
'visibility' => $data['visibility'] ?? $media->visibility,
|
|
'file_path' => $data['file_path'] ?? $media->file_path,
|
|
];
|
|
|
|
// --- Handle image_id specifically ---
|
|
// If image_id is provided in request, use it (even if null to remove association)
|
|
// If not provided, keep the existing value
|
|
if (array_key_exists('image_id', $data)) {
|
|
$updateData['image_id'] = $data['image_id'];
|
|
} else {
|
|
$updateData['image_id'] = $media->image_id;
|
|
}
|
|
|
|
// --- update media ---
|
|
$media->update($updateData);
|
|
|
|
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' , 'tags']),
|
|
]);
|
|
}
|
|
|
|
|
|
public function destroy($id)
|
|
{
|
|
$media = Media::where('id', $id)
|
|
->where('user_id', auth()->id())
|
|
->firstOrFail();
|
|
|
|
Storage::disk('public')->delete($media->file_path);
|
|
|
|
$media->delete();
|
|
|
|
return response()->json(['message' => 'Media deleted']);
|
|
}
|
|
|
|
// SAVE media
|
|
public function toggleSaveMedia($id)
|
|
{
|
|
$user = auth()->user();
|
|
|
|
if ($user->savedMedia()->where('media_id', $id)->exists()) {
|
|
// already saved → unsave
|
|
$user->savedMedia()->detach($id);
|
|
return response()->json(['message' => 'Unsaved!']);
|
|
} else {
|
|
// not saved → save
|
|
$user->savedMedia()->attach($id);
|
|
return response()->json(['message' => 'Saved!']);
|
|
}
|
|
}
|
|
|
|
// GET saved
|
|
public function saved()
|
|
{
|
|
return auth()->user()->savedMedia()->with(['image','category' , 'myNote' , 'tags'])->get();
|
|
}
|
|
|
|
public function storeNote(Request $request, $mediaId)
|
|
{
|
|
$data = $request->validate([
|
|
'content' => 'required|string'
|
|
]);
|
|
|
|
$media = Media::findOrFail($mediaId);
|
|
|
|
$note = $media->notes()->create([
|
|
'user_id' => auth()->id(),
|
|
'content' => $data['content'],
|
|
]);
|
|
|
|
return response()->json(['message' => 'Note added', 'note' => $note]);
|
|
}
|
|
} |