feat: add save feature
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
<?php
|
||||
// app/Http/Controllers/SaveController.php
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\SavedItem;
|
||||
|
||||
class SaveController extends Controller
|
||||
{
|
||||
/**
|
||||
* Save an item (music, media, breathing template, etc.)
|
||||
*/
|
||||
public function save(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'type' => 'required|string|in:music,media,breathing-template',
|
||||
'id' => 'required|integer',
|
||||
]);
|
||||
|
||||
$model = $this->getModel($request->type, $request->id);
|
||||
|
||||
if (!$model) {
|
||||
return response()->json([
|
||||
'message' => 'Item not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$result = $model->addSave();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Item saved successfully',
|
||||
'is_saved' => true,
|
||||
'saved_count' => $model->saved_count
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsave an item
|
||||
*/
|
||||
public function unsave(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'type' => 'required|string|in:music,media,breathing-template',
|
||||
'id' => 'required|integer',
|
||||
]);
|
||||
|
||||
$model = $this->getModel($request->type, $request->id);
|
||||
|
||||
if (!$model) {
|
||||
return response()->json([
|
||||
'message' => 'Item not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$result = $model->removeSave();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Item removed from saved',
|
||||
'is_saved' => false,
|
||||
'saved_count' => $model->saved_count
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle save status
|
||||
*/
|
||||
public function toggleSave(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'type' => 'required|string|in:music,media,breathing-template',
|
||||
'id' => 'required|integer',
|
||||
]);
|
||||
|
||||
$model = $this->getModel($request->type, $request->id);
|
||||
|
||||
if (!$model) {
|
||||
return response()->json([
|
||||
'message' => 'Item not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$result = $model->toggleSaveStatus();
|
||||
|
||||
return response()->json([
|
||||
'message' => $result ? 'Item saved successfully' : 'Item removed from saved',
|
||||
'is_saved' => $model->is_saved,
|
||||
'saved_count' => $model->saved_count
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all saved items for the authenticated user
|
||||
*/
|
||||
public function mySavedItems(Request $request)
|
||||
{
|
||||
$type = $request->get('type'); // Optional filter by type
|
||||
|
||||
$query = SavedItem::with('saveable')
|
||||
->where('user_id', auth()->id());
|
||||
|
||||
if ($type) {
|
||||
$modelClass = $this->getModelClass($type);
|
||||
$query->where('saveable_type', $modelClass);
|
||||
}
|
||||
|
||||
$savedItems = $query->latest()->paginate(20);
|
||||
|
||||
return response()->json([
|
||||
'data' => $savedItems,
|
||||
'total' => $savedItems->total(),
|
||||
'types' => [
|
||||
'music' => 'App\\Models\\Music',
|
||||
'media' => 'App\\Models\\Media',
|
||||
'breathing-template' => 'App\\Models\\BreathingTemplate',
|
||||
]
|
||||
]);
|
||||
}
|
||||
/**
|
||||
* Check if specific item is saved by user
|
||||
*/
|
||||
public function checkSaved(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'type' => 'required|string|in:music,media,breathing-template',
|
||||
'id' => 'required|integer',
|
||||
]);
|
||||
|
||||
$model = $this->getModel($request->type, $request->id);
|
||||
|
||||
if (!$model) {
|
||||
return response()->json([
|
||||
'message' => 'Item not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'is_saved' => $model->is_saved,
|
||||
'saved_count' => $model->saved_count
|
||||
]);
|
||||
}
|
||||
|
||||
private function getModel($type, $id)
|
||||
{
|
||||
$modelClass = $this->getModelClass($type);
|
||||
return $modelClass::find($id);
|
||||
}
|
||||
|
||||
private function getModelClass($type)
|
||||
{
|
||||
$models = [
|
||||
'music' => \App\Models\Music::class,
|
||||
'media' => \App\Models\Media::class,
|
||||
'breathing-template' => \App\Models\BreathingTemplate::class,
|
||||
];
|
||||
|
||||
return $models[$type] ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// public function mySavedItems(Request $request)
|
||||
// {
|
||||
// $type = $request->get('type');
|
||||
|
||||
// $query = SavedItem::with('saveable')
|
||||
// ->where('user_id', auth()->id());
|
||||
|
||||
// if ($type) {
|
||||
// $modelClass = $this->getModelClass($type);
|
||||
// $query->where('saveable_type', $modelClass);
|
||||
// }
|
||||
|
||||
// $savedItems = $query->latest()->paginate(20);
|
||||
|
||||
// // Transform the response to include formatted data
|
||||
// $transformedItems = $savedItems->map(function ($savedItem) {
|
||||
// $item = $savedItem->saveable;
|
||||
|
||||
// if (!$item) return null;
|
||||
|
||||
// $baseData = [
|
||||
// 'saved_id' => $savedItem->id,
|
||||
// 'saved_at' => $savedItem->created_at,
|
||||
// 'type' => class_basename($savedItem->saveable_type),
|
||||
// ];
|
||||
|
||||
// // Add type-specific data
|
||||
// if ($item instanceof \App\Models\Music) {
|
||||
// return array_merge($baseData, [
|
||||
// 'id' => $item->id,
|
||||
// 'title' => $item->title,
|
||||
// 'artist' => $item->artist,
|
||||
// 'duration' => $item->duration_formatted ?? $item->duration,
|
||||
// 'image_url' => $item->image_url,
|
||||
// 'is_saved' => true,
|
||||
// ]);
|
||||
// } elseif ($item instanceof \App\Models\Media) {
|
||||
// return array_merge($baseData, [
|
||||
// 'id' => $item->id,
|
||||
// 'title' => $item->title,
|
||||
// 'caption' => $item->caption,
|
||||
// 'type' => $item->type,
|
||||
// 'duration' => $item->duration,
|
||||
// 'image_url' => $item->image->url ?? null,
|
||||
// 'is_saved' => true,
|
||||
// ]);
|
||||
// } elseif ($item instanceof \App\Models\BreathingTemplate) {
|
||||
// return array_merge($baseData, [
|
||||
// 'id' => $item->id,
|
||||
// 'name' => $item->name,
|
||||
// 'description' => $item->description,
|
||||
// 'duration' => $item->duration,
|
||||
// 'inhale' => $item->inhale,
|
||||
// 'exhale' => $item->exhale,
|
||||
// 'breath_hold' => $item->breath_hold,
|
||||
// 'image_url' => $item->image_url,
|
||||
// 'is_saved' => true,
|
||||
// ]);
|
||||
// }
|
||||
|
||||
// return $baseData;
|
||||
// })->filter();
|
||||
@@ -3,16 +3,18 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Traits\HasSaves;
|
||||
|
||||
class BreathingTemplate extends Model
|
||||
{
|
||||
use HasSaves;
|
||||
protected $fillable = ['user_id', 'name', 'inhale', 'exhale', 'breath_hold' , 'duration' , 'description','image_id' , 'source'];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
protected $appends = ['image_url'];
|
||||
protected $appends = ['image_url', 'is_saved','saved_count'];
|
||||
|
||||
public function getImageUrlAttribute()
|
||||
{
|
||||
|
||||
+11
-7
@@ -5,9 +5,11 @@
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Traits\HasRatings;
|
||||
use App\Traits\HasComments;
|
||||
use App\Traits\HasSaves;
|
||||
|
||||
class Media extends Model
|
||||
{
|
||||
use HasRatings, HasComments;
|
||||
use HasRatings, HasComments,HasSaves;
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'image_id',
|
||||
@@ -21,16 +23,18 @@ class Media extends Model
|
||||
'visibility',
|
||||
'is_premium'
|
||||
];
|
||||
protected $appends = [
|
||||
protected $appends = [
|
||||
'average_rating',
|
||||
'user_rating',
|
||||
'ratings_count',
|
||||
'comments_count',
|
||||
'has_user_commented', // Add this
|
||||
'user_comment', // Add this
|
||||
'user_comment_id', // Add this
|
||||
'has_user_rated' // Add this
|
||||
];
|
||||
'has_user_commented',
|
||||
'user_comment',
|
||||
'user_comment_id',
|
||||
'has_user_rated',
|
||||
'is_saved',
|
||||
'saved_count'
|
||||
];
|
||||
public function image()
|
||||
{
|
||||
return $this->belongsTo(Image::class);
|
||||
|
||||
+11
-3
@@ -10,10 +10,11 @@
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany; // ← Add this
|
||||
use App\Traits\HasRatings;
|
||||
use App\Traits\HasComments;
|
||||
use App\Traits\HasSaves;
|
||||
|
||||
class Music extends Model
|
||||
{
|
||||
use HasFactory, HasRatings, HasComments;
|
||||
use HasFactory, HasRatings, HasComments , HasSaves;
|
||||
protected $table = 'music';
|
||||
|
||||
|
||||
@@ -43,10 +44,17 @@ public function tags(): BelongsToMany
|
||||
return $this->belongsToMany(Tag::class, 'music_tags');
|
||||
}
|
||||
|
||||
protected $appends = ['url', 'image_url' , 'average_rating', 'user_rating', 'comments_count' , 'has_user_commented',
|
||||
protected $appends = ['url',
|
||||
'image_url' ,
|
||||
'average_rating',
|
||||
'user_rating',
|
||||
'comments_count' ,
|
||||
'has_user_commented',
|
||||
'user_comment',
|
||||
'user_comment_id',
|
||||
'has_user_rated'
|
||||
'has_user_rated' ,
|
||||
'is_saved',
|
||||
'saved_count'
|
||||
];
|
||||
|
||||
public function getUrlAttribute()
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
class SavedItem extends Model
|
||||
{
|
||||
protected $table = 'saved_items';
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'saveable_id',
|
||||
'saveable_type',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function saveable(): MorphTo
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
// Helper to get saved items by type
|
||||
public static function getSavedItemsForUser($userId, $type = null)
|
||||
{
|
||||
$query = self::with('saveable')->where('user_id', $userId);
|
||||
|
||||
if ($type) {
|
||||
$query->where('saveable_type', $type);
|
||||
}
|
||||
|
||||
return $query->latest()->get();
|
||||
}
|
||||
|
||||
// Check if user has saved specific item
|
||||
public static function isSavedByUser($userId, $saveableId, $saveableType)
|
||||
{
|
||||
return self::where([
|
||||
'user_id' => $userId,
|
||||
'saveable_id' => $saveableId,
|
||||
'saveable_type' => $saveableType,
|
||||
])->exists();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
// app/Traits/HasSaves.php
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\SavedItem;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
|
||||
trait HasSaves
|
||||
{
|
||||
public function saves()
|
||||
{
|
||||
return $this->morphMany(SavedItem::class, 'saveable');
|
||||
}
|
||||
|
||||
public function getIsSavedAttribute()
|
||||
{
|
||||
if (!auth()->check()) return false;
|
||||
|
||||
return $this->saves()
|
||||
->where('user_id', auth()->id())
|
||||
->exists();
|
||||
}
|
||||
|
||||
public function getSavedCountAttribute()
|
||||
{
|
||||
return $this->saves()->count();
|
||||
}
|
||||
|
||||
public function toggleSaveStatus()
|
||||
{
|
||||
if ($this->getIsSavedAttribute()) {
|
||||
return $this->removeSave();
|
||||
} else {
|
||||
return $this->addSave();
|
||||
}
|
||||
}
|
||||
|
||||
public function addSave()
|
||||
{
|
||||
if ($this->getIsSavedAttribute()) return false;
|
||||
|
||||
return SavedItem::create([
|
||||
'user_id' => auth()->id(),
|
||||
'saveable_id' => $this->id,
|
||||
'saveable_type' => get_class($this),
|
||||
]);
|
||||
}
|
||||
|
||||
public function removeSave()
|
||||
{
|
||||
if (!$this->getIsSavedAttribute()) return false;
|
||||
|
||||
return SavedItem::where([
|
||||
'user_id' => auth()->id(),
|
||||
'saveable_id' => $this->id,
|
||||
'saveable_type' => get_class($this),
|
||||
])->delete();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?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_items', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->onDelete('cascade');
|
||||
$table->morphs('saveable'); // saveable_id + saveable_type
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['user_id', 'saveable_id', 'saveable_type']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('saved_items');
|
||||
}
|
||||
};
|
||||
+11
-1
@@ -20,7 +20,7 @@
|
||||
use App\Http\Controllers\RatingController;
|
||||
use App\Http\Controllers\CommentController;
|
||||
use App\Http\Controllers\MusicSubcategoryController;
|
||||
|
||||
use App\Http\Controllers\SaveController;
|
||||
|
||||
Route::get('/test-hash', function() {
|
||||
$plain = 'amnk1380';
|
||||
@@ -201,4 +201,14 @@
|
||||
Route::delete('{type}/{id}/{commentId}', [CommentController::class, 'deleteComment']);
|
||||
Route::get('most/{type}', [CommentController::class, 'mostCommented']);
|
||||
});
|
||||
|
||||
|
||||
// Save routes (works for all models)
|
||||
Route::prefix('saves')->group(function () {
|
||||
Route::post('/save', [SaveController::class, 'save']);
|
||||
Route::post('/unsave', [SaveController::class, 'unsave']);
|
||||
Route::post('/toggle', [SaveController::class, 'toggleSave']);
|
||||
Route::get('/my-saved', [SaveController::class, 'mySavedItems']);
|
||||
Route::post('/check', [SaveController::class, 'checkSaved']);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user