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',
];
}