Merge branch 'main' of https://github.com/AmirmahdiNourkazemi/back-meditation
This commit is contained in:
@@ -22,7 +22,7 @@ public function store(Request $request)
|
|||||||
'category_name' => 'nullable|string|max:255',
|
'category_name' => 'nullable|string|max:255',
|
||||||
'image_id' => 'nullable|exists:images,id',
|
'image_id' => 'nullable|exists:images,id',
|
||||||
'duration' => 'nullable|integer',
|
'duration' => 'nullable|integer',
|
||||||
|
'is_premium' => 'nullable|boolean',
|
||||||
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:512000',
|
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:512000',
|
||||||
'external_url' => 'nullable|string',
|
'external_url' => 'nullable|string',
|
||||||
'visibility' => 'nullable|in:public,private',
|
'visibility' => 'nullable|in:public,private',
|
||||||
@@ -55,6 +55,7 @@ public function store(Request $request)
|
|||||||
'category_id' => $categoryId,
|
'category_id' => $categoryId,
|
||||||
'duration' => $data['duration'] ?? null,
|
'duration' => $data['duration'] ?? null,
|
||||||
'visibility' => $data['visibility'] ?? 'public',
|
'visibility' => $data['visibility'] ?? 'public',
|
||||||
|
'is_premium'=> $data['is_premium'] ?? false
|
||||||
]);
|
]);
|
||||||
if (!empty($data['tags'])) {
|
if (!empty($data['tags'])) {
|
||||||
$tagIds = [];
|
$tagIds = [];
|
||||||
@@ -115,13 +116,32 @@ public function index(Request $request)
|
|||||||
|
|
||||||
public function show($id)
|
public function show($id)
|
||||||
{
|
{
|
||||||
$media = Media::with(['image', 'category', 'myNote' , 'tags'])
|
$media = Media::with(['image', 'category', 'myNote', 'tags'])
|
||||||
->where('id', $id)
|
->where('id', $id)
|
||||||
->where('visibility', 'public')
|
->where(function($query) {
|
||||||
->orWhere('user_id', auth()->id())
|
$query->where('visibility', 'public')
|
||||||
->firstOrFail();
|
->orWhere('user_id', auth()->id());
|
||||||
|
})
|
||||||
|
->firstOrFail();
|
||||||
|
|
||||||
return response()->json($media);
|
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,
|
||||||
|
'is_premium' => $media->is_premium,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// UPDATE media
|
// UPDATE media
|
||||||
@@ -139,7 +159,7 @@ public function update(Request $request, $id)
|
|||||||
// support both: category_id or category_name
|
// support both: category_id or category_name
|
||||||
'category_id' => 'nullable|exists:categories,id',
|
'category_id' => 'nullable|exists:categories,id',
|
||||||
'category_name' => 'nullable|string|max:255',
|
'category_name' => 'nullable|string|max:255',
|
||||||
|
'is_premium' => 'nullable|boolean',
|
||||||
'image_id' => 'nullable|exists:images,id',
|
'image_id' => 'nullable|exists:images,id',
|
||||||
'duration' => 'nullable|integer',
|
'duration' => 'nullable|integer',
|
||||||
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:512000',
|
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:512000',
|
||||||
@@ -171,6 +191,7 @@ public function update(Request $request, $id)
|
|||||||
'caption' => $data['caption'] ?? $media->caption,
|
'caption' => $data['caption'] ?? $media->caption,
|
||||||
'type' => $data['type'] ?? $media->type,
|
'type' => $data['type'] ?? $media->type,
|
||||||
'category_id' => $categoryId,
|
'category_id' => $categoryId,
|
||||||
|
'is_premium' => $data['is_premium'] ?? $media->is_premium,
|
||||||
'duration' => $data['duration'] ?? $media->duration,
|
'duration' => $data['duration'] ?? $media->duration,
|
||||||
'external_url' => $data['external_url'] ?? $media->external_url,
|
'external_url' => $data['external_url'] ?? $media->external_url,
|
||||||
'visibility' => $data['visibility'] ?? $media->visibility,
|
'visibility' => $data['visibility'] ?? $media->visibility,
|
||||||
@@ -221,12 +242,20 @@ public function destroy($id)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SAVE media
|
// SAVE media
|
||||||
public function saveMedia($id)
|
public function toggleSaveMedia($id)
|
||||||
{
|
{
|
||||||
auth()->user()->savedMedia()->syncWithoutDetaching([$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!']);
|
return response()->json(['message' => 'Saved!']);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GET saved
|
// GET saved
|
||||||
public function saved()
|
public function saved()
|
||||||
@@ -234,19 +263,40 @@ public function saved()
|
|||||||
return auth()->user()->savedMedia()->with(['image','category' , 'myNote' , 'tags'])->get();
|
return auth()->user()->savedMedia()->with(['image','category' , 'myNote' , 'tags'])->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function storeNote(Request $request, $mediaId)
|
public function storeNote(Request $request, $mediaId)
|
||||||
{
|
{
|
||||||
$data = $request->validate([
|
|
||||||
'content' => 'required|string'
|
|
||||||
]);
|
|
||||||
|
|
||||||
$media = Media::findOrFail($mediaId);
|
$media = Media::findOrFail($mediaId);
|
||||||
|
$userId = auth()->id();
|
||||||
|
|
||||||
$note = $media->notes()->create([
|
$content = $request->input('content');
|
||||||
'user_id' => auth()->id(),
|
|
||||||
'content' => $data['content'],
|
// Check if note already exists for this user
|
||||||
|
$note = $media->notes()->where('user_id', $userId)->first();
|
||||||
|
|
||||||
|
if (empty($content)) {
|
||||||
|
// If content is empty, delete the note if it exists
|
||||||
|
if ($note) {
|
||||||
|
$note->delete();
|
||||||
|
return response()->json(['message' => 'Note deleted']);
|
||||||
|
}
|
||||||
|
return response()->json(['message' => 'No note to delete']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($note) {
|
||||||
|
// Update existing note
|
||||||
|
$note->update(['content' => $content]);
|
||||||
|
} else {
|
||||||
|
// Create new note
|
||||||
|
$note = $media->notes()->create([
|
||||||
|
'user_id' => $userId,
|
||||||
|
'content' => $content,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Note saved successfully',
|
||||||
|
'note' => $note
|
||||||
]);
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
return response()->json(['message' => 'Note added', 'note' => $note]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -629,7 +629,7 @@ public function googleLogin(Request $request)
|
|||||||
|
|
||||||
public function leaderBoard(){
|
public function leaderBoard(){
|
||||||
$users = User::with(['breathingSessions.template'])->where('xp', '>', 0)
|
$users = User::with(['breathingSessions.template'])->where('xp', '>', 0)
|
||||||
->orderBy('xp', 'desc')
|
->orderBy('xp', 'desc')->limit(20)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
return $users;
|
return $users;
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ class Media extends Model
|
|||||||
'external_url',
|
'external_url',
|
||||||
'duration',
|
'duration',
|
||||||
'visibility',
|
'visibility',
|
||||||
|
'is_premium'
|
||||||
];
|
];
|
||||||
|
|
||||||
public function image()
|
public function image()
|
||||||
@@ -54,4 +55,15 @@ public function getUrlAttribute()
|
|||||||
|
|
||||||
return $this->external_url;
|
return $this->external_url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function getIsSavedAttribute()
|
||||||
|
{
|
||||||
|
if (!auth()->check()) return false;
|
||||||
|
|
||||||
|
return $this->savedBy()
|
||||||
|
->where('user_id', auth()->id())
|
||||||
|
->exists();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?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('media', function (Blueprint $table) {
|
||||||
|
$table->boolean('is_premium')->default(false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('media', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('is_premium');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
+3
-2
@@ -138,10 +138,11 @@
|
|||||||
Route::post('/media', [MediaController::class, 'store']);
|
Route::post('/media', [MediaController::class, 'store']);
|
||||||
Route::get('/media', [MediaController::class, 'index']);
|
Route::get('/media', [MediaController::class, 'index']);
|
||||||
Route::post('/media/{id}', [MediaController::class, 'update']);
|
Route::post('/media/{id}', [MediaController::class, 'update']);
|
||||||
|
Route::get('/media/saved', [MediaController::class, 'saved']);
|
||||||
Route::delete('/media/{id}', [MediaController::class, 'destroy']);
|
Route::delete('/media/{id}', [MediaController::class, 'destroy']);
|
||||||
Route::get('/media/{id}', [MediaController::class, 'show']);
|
Route::get('/media/{id}', [MediaController::class, 'show']);
|
||||||
Route::post('/media/{id}/save', [MediaController::class, 'saveMedia']);
|
Route::post('/media/{id}/save', [MediaController::class, 'toggleSaveMedia']);
|
||||||
Route::get('/media/saved', [MediaController::class, 'saved']);
|
|
||||||
|
|
||||||
Route::post('/media/{id}/note', [MediaController::class, 'storeNote']);
|
Route::post('/media/{id}/note', [MediaController::class, 'storeNote']);
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user