feat: add media
This commit is contained in:
@@ -0,0 +1,166 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Media;
|
||||||
|
use App\Models\Category;
|
||||||
|
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:50480',
|
||||||
|
'external_url' => 'nullable|string',
|
||||||
|
'visibility' => 'nullable|in:public,private',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$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',
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Media created successfully',
|
||||||
|
'media' => $media->load(['image', 'category']),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET all visible media
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$media = Media::with(['image', 'category', 'myNote'])
|
||||||
|
->where('visibility', 'public')
|
||||||
|
->orWhere('user_id', auth()->id())
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return response()->json($media);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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:50480',
|
||||||
|
'external_url' => 'nullable|string',
|
||||||
|
'visibility' => 'nullable|in:public,private',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// --- 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');
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- merge final category ---
|
||||||
|
$data['category_id'] = $categoryId;
|
||||||
|
|
||||||
|
// --- update media ---
|
||||||
|
$media->update($data);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Media updated successfully',
|
||||||
|
'media' => $media->load(['image', 'category']),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 saveMedia($id)
|
||||||
|
{
|
||||||
|
auth()->user()->savedMedia()->syncWithoutDetaching([$id]);
|
||||||
|
|
||||||
|
return response()->json(['message' => 'Saved!']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET saved
|
||||||
|
public function saved()
|
||||||
|
{
|
||||||
|
return auth()->user()->savedMedia()->with(['image','category' , 'myNote'])->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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Media extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'image_id',
|
||||||
|
'category_id',
|
||||||
|
'title',
|
||||||
|
'caption',
|
||||||
|
'type',
|
||||||
|
'file_path',
|
||||||
|
'external_url',
|
||||||
|
'duration',
|
||||||
|
'visibility',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function image()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Image::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function category()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Category::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function notes()
|
||||||
|
{
|
||||||
|
return $this->morphMany(Note::class, 'noteable');
|
||||||
|
}
|
||||||
|
public function myNote()
|
||||||
|
{
|
||||||
|
return $this->morphOne(Note::class, 'noteable')->where('user_id', auth()->id());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function savedBy()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(User::class, 'saved_media')
|
||||||
|
->withTimestamps();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUrlAttribute()
|
||||||
|
{
|
||||||
|
if ($this->file_path)
|
||||||
|
return asset('storage/' . $this->file_path);
|
||||||
|
|
||||||
|
return $this->external_url;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -105,4 +105,10 @@ public function breathingSessions()
|
|||||||
{
|
{
|
||||||
return $this->hasMany(UserBreathingSession::class, 'user_id');
|
return $this->hasMany(UserBreathingSession::class, 'user_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function savedMedia()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Media::class, 'saved_media')->withTimestamps();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Generated
+960
-740
File diff suppressed because it is too large
Load Diff
@@ -1,28 +0,0 @@
|
|||||||
<?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::table('user_product', function (Blueprint $table) {
|
|
||||||
$table->renameColumn('subscription_token' ,'gateway');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reverse the migrations.
|
|
||||||
*/
|
|
||||||
public function down(): void
|
|
||||||
{
|
|
||||||
// Schema::table('user_product', function (Blueprint $table) {
|
|
||||||
// //
|
|
||||||
// });
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<?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', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->unsignedBigInteger('image_id')->nullable();
|
||||||
|
$table->unsignedBigInteger('category_id')->nullable();
|
||||||
|
$table->unsignedBigInteger('user_id')->nullable(); // owner (optional)
|
||||||
|
|
||||||
|
$table->string('title');
|
||||||
|
$table->text('caption')->nullable();
|
||||||
|
|
||||||
|
// media type (audio / video)
|
||||||
|
$table->enum('type', ['audio', 'video']);
|
||||||
|
|
||||||
|
// File or URL
|
||||||
|
$table->string('file_path')->nullable(); // stored file
|
||||||
|
$table->string('external_url')->nullable(); // YouTube, etc.
|
||||||
|
|
||||||
|
$table->integer('duration')->nullable(); // seconds
|
||||||
|
|
||||||
|
// public/private
|
||||||
|
$table->enum('visibility', ['public', 'private'])->default('public');
|
||||||
|
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->foreign('image_id')->references('id')->on('images')->nullOnDelete();
|
||||||
|
$table->foreign('category_id')->references('id')->on('categories')->nullOnDelete();
|
||||||
|
$table->foreign('user_id')->references('id')->on('users')->nullOnDelete();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('media');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
<?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('saved_media', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->unsignedBigInteger('media_id');
|
||||||
|
$table->unsignedBigInteger('user_id');
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['media_id', 'user_id']);
|
||||||
|
$table->foreign('media_id')->references('id')->on('media')->cascadeOnDelete();
|
||||||
|
$table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('saved_media');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
use App\Http\Controllers\SliderController;
|
use App\Http\Controllers\SliderController;
|
||||||
use App\Http\Controllers\ImageController;
|
use App\Http\Controllers\ImageController;
|
||||||
use App\Http\Controllers\MusicController;
|
use App\Http\Controllers\MusicController;
|
||||||
|
use App\Http\Controllers\MediaController;
|
||||||
|
|
||||||
Route::get('/test-hash', function() {
|
Route::get('/test-hash', function() {
|
||||||
$plain = 'amnk1380';
|
$plain = 'amnk1380';
|
||||||
@@ -131,4 +132,16 @@
|
|||||||
Route::put('/slider/{id}', [SliderController::class, 'update']); // update slider
|
Route::put('/slider/{id}', [SliderController::class, 'update']); // update slider
|
||||||
Route::delete('/slider/{id}', [SliderController::class, 'destroy']); // delete slider
|
Route::delete('/slider/{id}', [SliderController::class, 'destroy']); // delete slider
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
///media
|
||||||
|
Route::post('/media', [MediaController::class, 'store']);
|
||||||
|
Route::get('/media', [MediaController::class, 'index']);
|
||||||
|
Route::put('/media/{id}', [MediaController::class, 'update']);
|
||||||
|
Route::delete('/media/{id}', [MediaController::class, 'destroy']);
|
||||||
|
|
||||||
|
Route::post('/media/{id}/save', [MediaController::class, 'saveMedia']);
|
||||||
|
Route::get('/media/saved', [MediaController::class, 'saved']);
|
||||||
|
|
||||||
|
Route::post('/media/{id}/note', [MediaController::class, 'storeNote']);
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user