feat: add insight timer

This commit is contained in:
2026-06-03 00:55:40 +03:30
parent 82164f691f
commit 6f01c783fd
13 changed files with 599 additions and 0 deletions
@@ -0,0 +1,21 @@
<?php
namespace App\Http\Controllers;
use App\Models\BackgroundSound;
class BackgroundSoundController extends CatalogController
{
protected function modelClass(): string
{
return BackgroundSound::class;
}
protected function fileFields(): array
{
return [
'sound' => ['column' => 'sound_path', 'folder' => 'background-sounds/sounds', 'rules' => 'nullable|mimes:mp3,wav,ogg,flac|max:102400'],
'image' => ['column' => 'image_path', 'folder' => 'background-sounds/images', 'rules' => 'nullable|image|max:8192'],
];
}
}
@@ -0,0 +1,21 @@
<?php
namespace App\Http\Controllers;
use App\Models\BellSound;
class BellSoundController extends CatalogController
{
protected function modelClass(): string
{
return BellSound::class;
}
protected function fileFields(): array
{
return [
'sound' => ['column' => 'sound_path', 'folder' => 'bells/sounds', 'rules' => 'nullable|mimes:mp3,wav,ogg,flac|max:51200'],
'image' => ['column' => 'image_path', 'folder' => 'bells/images', 'rules' => 'nullable|image|max:8192'],
];
}
}
+118
View File
@@ -0,0 +1,118 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
/**
* Base controller for simple admin-managed catalogs that hold a name plus
* one or more uploaded files (image/sound). Subclasses declare the model
* and the file fields.
*/
abstract class CatalogController extends Controller
{
/** @return class-string<\Illuminate\Database\Eloquent\Model> */
abstract protected function modelClass(): string;
/**
* Map of request file field => ['column' => db column, 'folder' => storage folder, 'rules' => validation].
*/
abstract protected function fileFields(): array;
public function index(Request $request)
{
$query = ($this->modelClass())::query();
if (!$request->boolean('include_inactive')) {
$query->where('is_active', true);
}
return response()->json($query->orderBy('order')->get());
}
public function show($id)
{
return response()->json(($this->modelClass())::findOrFail($id));
}
public function store(Request $request)
{
$data = $request->validate(array_merge([
'name' => 'required|string|max:255',
'order' => 'nullable|integer',
'is_active' => 'nullable|boolean',
], $this->fileRules()));
$attributes = [
'name' => $data['name'],
'order' => $data['order'] ?? 0,
'is_active' => $data['is_active'] ?? true,
];
foreach ($this->fileFields() as $field => $config) {
$attributes[$config['column']] = $request->hasFile($field)
? $request->file($field)->store($config['folder'], 'public')
: null;
}
$item = ($this->modelClass())::create($attributes);
return response()->json(['message' => 'Created successfully', 'item' => $item], 201);
}
public function update(Request $request, $id)
{
$item = ($this->modelClass())::findOrFail($id);
$data = $request->validate(array_merge([
'name' => 'sometimes|string|max:255',
'order' => 'nullable|integer',
'is_active' => 'nullable|boolean',
], $this->fileRules()));
foreach (['name', 'order', 'is_active'] as $field) {
if ($request->has($field)) {
$item->$field = $data[$field];
}
}
foreach ($this->fileFields() as $field => $config) {
if ($request->hasFile($field)) {
if ($item->{$config['column']}) {
Storage::disk('public')->delete($item->{$config['column']});
}
$item->{$config['column']} = $request->file($field)->store($config['folder'], 'public');
}
}
$item->save();
return response()->json(['message' => 'Updated successfully', 'item' => $item]);
}
public function destroy($id)
{
$item = ($this->modelClass())::findOrFail($id);
foreach ($this->fileFields() as $config) {
if ($item->{$config['column']}) {
Storage::disk('public')->delete($item->{$config['column']});
}
}
$item->delete();
return response()->json(['message' => 'Deleted successfully']);
}
private function fileRules(): array
{
$rules = [];
foreach ($this->fileFields() as $field => $config) {
$rules[$field] = $config['rules'];
}
return $rules;
}
}
@@ -0,0 +1,94 @@
<?php
namespace App\Http\Controllers;
use App\Models\BackgroundSound;
use App\Models\BellSound;
use App\Models\Image;
use App\Models\TimerPreset;
use Illuminate\Http\Request;
class TimerPresetController extends Controller
{
// All catalogs needed to build a timer (pickers), in one call.
public function options()
{
return response()->json([
'bell_sounds' => BellSound::where('is_active', true)->orderBy('order')->get(),
'background_sounds' => BackgroundSound::where('is_active', true)->orderBy('order')->get(),
'background_images' => Image::where('type', 'public')->get(),
]);
}
// USER: their saved timers (ذخیره‌شده‌های من).
public function index()
{
$presets = TimerPreset::with(TimerPreset::RELATIONS)
->where('user_id', auth()->id())
->latest()
->get();
return response()->json($presets);
}
public function show($id)
{
$preset = TimerPreset::with(TimerPreset::RELATIONS)
->where('user_id', auth()->id())
->findOrFail($id);
return response()->json($preset);
}
public function store(Request $request)
{
$data = $this->validatePreset($request);
$preset = TimerPreset::create($data + ['user_id' => auth()->id()]);
return response()->json([
'message' => 'Timer saved successfully',
'preset' => $preset->load(TimerPreset::RELATIONS),
], 201);
}
public function update(Request $request, $id)
{
$preset = TimerPreset::where('user_id', auth()->id())->findOrFail($id);
$data = $this->validatePreset($request, false);
$preset->fill($data)->save();
return response()->json([
'message' => 'Timer updated successfully',
'preset' => $preset->load(TimerPreset::RELATIONS),
]);
}
public function destroy($id)
{
$preset = TimerPreset::where('user_id', auth()->id())->findOrFail($id);
$preset->delete();
return response()->json(['message' => 'Timer deleted successfully']);
}
private function validatePreset(Request $request, bool $creating = true): array
{
$required = $creating ? 'required' : 'sometimes';
return $request->validate([
'name' => "$required|string|max:255",
'duration_seconds' => "$required|integer|min:1",
'start_bell_id' => 'nullable|exists:bell_sounds,id',
'end_bell_id' => 'nullable|exists:bell_sounds,id',
'interval_bell_id' => 'nullable|exists:bell_sounds,id',
'interval_seconds' => 'nullable|integer|min:1',
'interval_repeat' => 'nullable|integer|min:1',
'background_sound_id' => 'nullable|exists:background_sounds,id',
'background_image_id' => 'nullable|exists:images,id',
'volume' => 'nullable|integer|min:0|max:100',
]);
}
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class BackgroundSound extends Model
{
protected $fillable = ['name', 'sound_path', 'image_path', 'order', 'is_active'];
protected $casts = [
'is_active' => 'boolean',
'order' => 'integer',
];
protected $appends = ['sound_url', 'image_url'];
public function getSoundUrlAttribute(): ?string
{
return $this->sound_path ? asset('storage/' . $this->sound_path) : null;
}
public function getImageUrlAttribute(): ?string
{
return $this->image_path ? asset('storage/' . $this->image_path) : null;
}
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class BellSound extends Model
{
protected $fillable = ['name', 'sound_path', 'image_path', 'order', 'is_active'];
protected $casts = [
'is_active' => 'boolean',
'order' => 'integer',
];
protected $appends = ['sound_url', 'image_url'];
public function getSoundUrlAttribute(): ?string
{
return $this->sound_path ? asset('storage/' . $this->sound_path) : null;
}
public function getImageUrlAttribute(): ?string
{
return $this->image_path ? asset('storage/' . $this->image_path) : null;
}
}
+69
View File
@@ -0,0 +1,69 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class TimerPreset extends Model
{
protected $fillable = [
'user_id',
'name',
'duration_seconds',
'start_bell_id',
'end_bell_id',
'interval_bell_id',
'interval_seconds',
'interval_repeat',
'background_sound_id',
'background_image_id',
'volume',
];
protected $casts = [
'duration_seconds' => 'integer',
'interval_seconds' => 'integer',
'interval_repeat' => 'integer',
'volume' => 'integer',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function startBell(): BelongsTo
{
return $this->belongsTo(BellSound::class, 'start_bell_id');
}
public function endBell(): BelongsTo
{
return $this->belongsTo(BellSound::class, 'end_bell_id');
}
public function intervalBell(): BelongsTo
{
return $this->belongsTo(BellSound::class, 'interval_bell_id');
}
public function backgroundSound(): BelongsTo
{
return $this->belongsTo(BackgroundSound::class, 'background_sound_id');
}
public function backgroundImage(): BelongsTo
{
return $this->belongsTo(Image::class, 'background_image_id');
}
// Eager-load set for returning a fully-resolved preset to the front.
public const RELATIONS = [
'startBell',
'endBell',
'intervalBell',
'backgroundSound',
'backgroundImage',
];
}
@@ -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
{
// Bell sounds used for start / end / interval bells (زنگ شروع/پایان/بین‌راهی).
Schema::create('bell_sounds', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('sound_path')->nullable();
$table->string('image_path')->nullable();
$table->integer('order')->default(0);
$table->boolean('is_active')->default(true);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('bell_sounds');
}
};
@@ -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
{
// Ambient background sounds (صدای پس‌زمینه).
Schema::create('background_sounds', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('sound_path')->nullable();
$table->string('image_path')->nullable();
$table->integer('order')->default(0);
$table->boolean('is_active')->default(true);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('background_sounds');
}
};
@@ -0,0 +1,32 @@
<?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
{
// Background images (تصویر پس‌زمینه).
Schema::create('background_images', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('image_path')->nullable();
$table->integer('order')->default(0);
$table->boolean('is_active')->default(true);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('background_images');
}
};
@@ -0,0 +1,46 @@
<?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
{
// A saved meditation timer (ذخیره‌شده‌های من) configured by a user.
Schema::create('timer_presets', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
$table->string('name');
$table->unsignedInteger('duration_seconds')->default(0); // مدت زمان
// Bells
$table->foreignId('start_bell_id')->nullable()->constrained('bell_sounds')->nullOnDelete();
$table->foreignId('end_bell_id')->nullable()->constrained('bell_sounds')->nullOnDelete();
$table->foreignId('interval_bell_id')->nullable()->constrained('bell_sounds')->nullOnDelete();
$table->unsignedInteger('interval_seconds')->nullable(); // هر چند ثانیه یک‌بار
$table->unsignedInteger('interval_repeat')->nullable(); // تکرار چند بار
// Ambience
$table->foreignId('background_sound_id')->nullable()->constrained('background_sounds')->nullOnDelete();
$table->foreignId('background_image_id')->nullable()->constrained('background_images')->nullOnDelete();
$table->unsignedInteger('volume')->default(100); // صدای دستگاه (0-100)
$table->timestamps();
$table->index('user_id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('timer_presets');
}
};
@@ -0,0 +1,48 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Repoint timer background image to the shared `images` table and drop the
* dedicated background_images catalog.
*/
public function up(): void
{
Schema::table('timer_presets', function (Blueprint $table) {
$table->dropForeign(['background_image_id']);
});
Schema::dropIfExists('background_images');
Schema::table('timer_presets', function (Blueprint $table) {
$table->foreign('background_image_id')->references('id')->on('images')->nullOnDelete();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('timer_presets', function (Blueprint $table) {
$table->dropForeign(['background_image_id']);
});
Schema::create('background_images', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('image_path')->nullable();
$table->integer('order')->default(0);
$table->boolean('is_active')->default(true);
$table->timestamps();
});
Schema::table('timer_presets', function (Blueprint $table) {
$table->foreign('background_image_id')->references('id')->on('background_images')->nullOnDelete();
});
}
};
+30
View File
@@ -14,6 +14,9 @@
use App\Http\Controllers\SurveyQuestionController; use App\Http\Controllers\SurveyQuestionController;
use App\Http\Controllers\SliderController; use App\Http\Controllers\SliderController;
use App\Http\Controllers\SceneController; use App\Http\Controllers\SceneController;
use App\Http\Controllers\BellSoundController;
use App\Http\Controllers\BackgroundSoundController;
use App\Http\Controllers\TimerPresetController;
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; use App\Http\Controllers\MediaController;
@@ -170,6 +173,33 @@
Route::delete('/scenes/{id}', [SceneController::class, 'destroy']); Route::delete('/scenes/{id}', [SceneController::class, 'destroy']);
/// insight timer (زمان‌سنج)
// Builder catalogs (bells / background sounds / background images) in one call.
Route::get('/timer/options', [TimerPresetController::class, 'options']);
// Saved timers per user (ذخیره‌شده‌های من).
Route::get('/timer-presets', [TimerPresetController::class, 'index']);
Route::post('/timer-presets', [TimerPresetController::class, 'store']);
Route::get('/timer-presets/{id}', [TimerPresetController::class, 'show']);
Route::put('/timer-presets/{id}', [TimerPresetController::class, 'update']);
Route::delete('/timer-presets/{id}', [TimerPresetController::class, 'destroy']);
// Timer sound/image catalogs (POST update for multipart uploads).
Route::get('/bell-sounds', [BellSoundController::class, 'index']);
Route::post('/bell-sounds', [BellSoundController::class, 'store']);
Route::get('/bell-sounds/{id}', [BellSoundController::class, 'show']);
Route::post('/bell-sounds/{id}', [BellSoundController::class, 'update']);
Route::delete('/bell-sounds/{id}', [BellSoundController::class, 'destroy']);
Route::get('/background-sounds', [BackgroundSoundController::class, 'index']);
Route::post('/background-sounds', [BackgroundSoundController::class, 'store']);
Route::get('/background-sounds/{id}', [BackgroundSoundController::class, 'show']);
Route::post('/background-sounds/{id}', [BackgroundSoundController::class, 'update']);
Route::delete('/background-sounds/{id}', [BackgroundSoundController::class, 'destroy']);
// Background images for timers reuse the shared images catalog (see /images routes).
///media ///media
Route::get('/media/filters', [MediaController::class, 'filters']); Route::get('/media/filters', [MediaController::class, 'filters']);