feat: add breath colors

This commit is contained in:
2026-06-16 18:52:25 +03:30
parent 81ac4a896f
commit 42f3808343
9 changed files with 206 additions and 8 deletions
@@ -0,0 +1,68 @@
<?php
namespace App\Http\Controllers;
use App\Models\BreathingColor;
use Illuminate\Http\Request;
class BreathingColorController extends Controller
{
// Palette the app shows when creating a breathing template.
public function index(Request $request)
{
$query = BreathingColor::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(BreathingColor::findOrFail($id));
}
public function store(Request $request)
{
$data = $this->validateData($request, true);
$color = BreathingColor::create($data);
return response()->json([
'message' => 'Color created successfully',
'color' => $color,
], 201);
}
public function update(Request $request, $id)
{
$color = BreathingColor::findOrFail($id);
$color->update($this->validateData($request, false));
return response()->json([
'message' => 'Color updated successfully',
'color' => $color,
]);
}
public function destroy($id)
{
BreathingColor::findOrFail($id)->delete();
return response()->json(['message' => 'Color deleted successfully']);
}
private function validateData(Request $request, bool $creating): array
{
return $request->validate([
'name' => 'nullable|string|max:255',
'colors' => ($creating ? 'required' : 'sometimes') . '|array|min:1',
'colors.*' => ['string', 'regex:/^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/'],
'order' => 'nullable|integer',
'is_active' => 'nullable|boolean',
]);
}
}
@@ -19,7 +19,9 @@ public function createTemplate(Request $request)
'duration' => 'sometimes|integer|min:0',
'description' => 'sometimes|string|nullable',
'image_id' => 'nullable|exists:images,id',
'source' => 'nullable|string'
'source' => 'nullable|string',
// The chosen color from the /breathing-colors palette.
'breathing_color_id' => 'nullable|exists:breathing_colors,id',
]);
$template = BreathingTemplate::create([
@@ -31,10 +33,11 @@ public function createTemplate(Request $request)
'duration'=> $data['duration'] ?? 60,
'description' => $data['description'] ?? null,
'image_id' => $data['image_id'] ?? null,
'source' => 'nullable|string'
'source' => $data['source'] ?? null,
'breathing_color_id' => $data['breathing_color_id'] ?? null,
]);
return response()->json(['message' => 'Template created', 'template' => $template->load('image')]);
return response()->json(['message' => 'Template created', 'template' => $template->load('image', 'breathingColor')]);
}
public function updateTemplate(Request $request, $id)
@@ -51,12 +54,13 @@ public function updateTemplate(Request $request, $id)
'duration' => 'sometimes|integer|min:0',
'description' => 'sometimes|string|nullable',
'image_id' => 'nullable|exists:images,id',
'source' => 'nullable|string'
'source' => 'nullable|string',
'breathing_color_id' => 'nullable|exists:breathing_colors,id',
]);
$template->update($data);
return response()->json(['message' => 'Template updated', 'template' => $template->load('image')]);
return response()->json(['message' => 'Template updated', 'template' => $template->load('image', 'breathingColor')]);
}
public function deleteTemplate($id)
{
@@ -75,7 +79,7 @@ public function getTemplates()
$templates = BreathingTemplate::whereNull('user_id')
->orWhere('user_id', $userId)
->with('image') // eager load image
->with(['image', 'breathingColor']) // eager load image + color
->get()
->map(function ($template) {
if ($template->image) {
@@ -92,7 +96,7 @@ public function getTemplates()
public function getUserTemplates()
{
$templates = BreathingTemplate::where('user_id', auth()->id())
->with('image') // eager load image
->with(['image', 'breathingColor']) // eager load image + color
->get()
->map(function ($template) {
if ($template->image) {
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class BreathingColor extends Model
{
protected $fillable = ['name', 'colors', 'order', 'is_active'];
protected $casts = [
'colors' => 'array',
'is_active' => 'boolean',
'order' => 'integer',
];
}
+6 -1
View File
@@ -8,12 +8,17 @@
class BreathingTemplate extends Model
{
use HasSaves;
protected $fillable = ['user_id', 'name', 'inhale', 'exhale', 'breath_hold' , 'duration' , 'description','image_id' , 'source'];
protected $fillable = ['user_id', 'name', 'inhale', 'exhale', 'breath_hold' , 'duration' , 'description','image_id' , 'source', 'breathing_color_id'];
public function user()
{
return $this->belongsTo(User::class);
}
public function breathingColor()
{
return $this->belongsTo(BreathingColor::class);
}
protected $appends = ['image_url', 'is_saved','saved_count'];
public function getImageUrlAttribute()
@@ -0,0 +1,23 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('breathing_templates', function (Blueprint $table) {
// One or more hex colors: a single solid color or a gradient.
$table->json('colors')->nullable()->after('image_id');
});
}
public function down(): void
{
Schema::table('breathing_templates', function (Blueprint $table) {
$table->dropColumn('colors');
});
}
};
@@ -0,0 +1,25 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('breathing_colors', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->json('colors'); // one hex (solid) or several (gradient)
$table->integer('order')->default(0);
$table->boolean('is_active')->default(true);
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('breathing_colors');
}
};
@@ -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
{
public function up(): void
{
Schema::table('breathing_templates', function (Blueprint $table) {
if (Schema::hasColumn('breathing_templates', 'colors')) {
$table->dropColumn('colors');
}
$table->foreignId('breathing_color_id')->nullable()->after('image_id')
->constrained('breathing_colors')->nullOnDelete();
});
}
public function down(): void
{
Schema::table('breathing_templates', function (Blueprint $table) {
$table->dropForeign(['breathing_color_id']);
$table->dropColumn('breathing_color_id');
$table->json('colors')->nullable();
});
}
};
+25
View File
@@ -0,0 +1,25 @@
<?php
namespace Database\Seeders;
use App\Models\BreathingColor;
use Illuminate\Database\Seeder;
class BreathingColorSeeder extends Seeder
{
public function run(): void
{
$colors = [
['name' => 'آبی', 'order' => 1, 'colors' => ['#3ABDEB', '#553AEB']],
['name' => 'صورتی', 'order' => 2, 'colors' => ['#CE4299', '#8628FF']],
['name' => 'سبز', 'order' => 3, 'colors' => ['#5CC65C', '#00A489']],
['name' => 'نارنجی', 'order' => 4, 'colors' => ['#E2990A', '#CC4B1C']],
['name' => 'بنفش', 'order' => 5, 'colors' => ['#6829DD', '#1A50CC']],
['name' => 'تک‌رنگ', 'order' => 6, 'colors' => ['#5360FC']],
];
foreach ($colors as $c) {
BreathingColor::firstOrCreate(['name' => $c['name']], $c);
}
}
}
+4
View File
@@ -4,6 +4,7 @@
use Illuminate\Support\Facades\Route;
use Laravel\Socialite\Facades\Socialite;
use App\Http\Controllers\BreathingExerciseController;
use App\Http\Controllers\BreathingColorController;
use App\Http\Controllers\PackageNameController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\BreathingTemplate;
@@ -109,6 +110,9 @@
Route::get('user-templates', [BreathingExerciseController::class, 'getUserTemplates']);
Route::get('breathing-sessions', [BreathingExerciseController::class, 'getSessions']);
// Breathing color palette (app reads it to pick a template color; admin manages it).
Route::apiResource('breathing-colors', BreathingColorController::class);
/// worry box feature
Route::prefix('worries')->controller(WorryController::class)->group(function () {
Route::post('/', 'store'); // Create worry + note