feat: add breath_hold & duration

This commit is contained in:
2025-09-13 16:54:17 +03:30
parent 0b73e9d095
commit c8bf4011e2
4 changed files with 164 additions and 38 deletions
@@ -15,7 +15,8 @@ public function createTemplate(Request $request)
'name' => 'required|string', 'name' => 'required|string',
'inhale' => 'required|integer|min:1', 'inhale' => 'required|integer|min:1',
'exhale' => 'required|integer|min:1', 'exhale' => 'required|integer|min:1',
'repeat' => 'required|integer|min:1', 'breath_hold' => 'required|integer|min:0',
'duration' => 'sometimes|integer|min:10'
]); ]);
$template = BreathingTemplate::create([ $template = BreathingTemplate::create([
@@ -23,7 +24,8 @@ public function createTemplate(Request $request)
'name' => $data['name'], 'name' => $data['name'],
'inhale' => $data['inhale'], 'inhale' => $data['inhale'],
'exhale' => $data['exhale'], 'exhale' => $data['exhale'],
'repeat' => $data['repeat'], 'breath_hold'=> $data['breath_hold'] ?? 0,
'duration'=> $data['duration'] ?? 60,
]); ]);
return response()->json(['message' => 'Template created', 'template' => $template]); return response()->json(['message' => 'Template created', 'template' => $template]);
@@ -39,7 +41,8 @@ public function updateTemplate(Request $request, $id)
'name' => 'sometimes|required|string', 'name' => 'sometimes|required|string',
'inhale' => 'sometimes|required|integer|min:1', 'inhale' => 'sometimes|required|integer|min:1',
'exhale' => 'sometimes|required|integer|min:1', 'exhale' => 'sometimes|required|integer|min:1',
'repeat' => 'sometimes|required|integer|min:1', 'breath_hold' => 'sometimes|integer|min:0',
'duration' => 'sometimes|integer|min:10',
]); ]);
$template->update($data); $template->update($data);
@@ -72,44 +75,46 @@ public function getTemplates()
return response()->json($templates); return response()->json($templates);
} }
public function store(Request $request) // public function store(Request $request)
{ // {
$data = $request->validate([ // $data = $request->validate([
'subject' => 'required|string', // 'subject' => 'required|string',
'duration' => 'required|integer', // 'duration' => 'required|integer',
'created_at' => 'nullable|date', // 'created_at' => 'nullable|date',
]); // ]);
$user = auth()->user(); // $user = auth()->user();
$exercise = BreathingExercise::create([ // $exercise = BreathingExercise::create([
'user_id' => $user->id, // 'user_id' => $user->id,
'subject' => $data['subject'], // 'subject' => $data['subject'],
'duration' => $data['duration'], // 'duration' => $data['duration'],
'created_at' => $data['created_at'] ?? now(), // 'created_at' => $data['created_at'] ?? now(),
]); // ]);
// Give XP (e.g., +10 per session) // // Give XP (e.g., +10 per session)
if ($data['created_at'] == null) { // if ($data['created_at'] == null) {
$user->increment('xp', 10); // $user->increment('xp', 10);
} // }
return response()->json([ // return response()->json([
'message' => 'Exercise saved', // 'message' => 'Exercise saved',
'exercise' => $exercise, // 'exercise' => $exercise,
'xp' => $user->xp // 'xp' => $user->xp
]); // ]);
} // }
public function completeSession(Request $request) public function completeSession(Request $request)
{ {
$data = $request->validate([ $data = $request->validate([
'template_id' => 'required|exists:breathing_templates,id', 'template_id' => 'required|exists:breathing_templates,id',
'duration' => 'sometimes|integer|min:10'
]); ]);
$session = UserBreathingSession::create([ $session = UserBreathingSession::create([
'user_id' => auth()->id(), 'user_id' => auth()->id(),
'template_id' => $data['template_id'], 'template_id' => $data['template_id'],
'duration' => $data['duration'] ?? $template->duration,
]); ]);
// Increase XP // Increase XP
@@ -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()
{
Schema::table('breathing_templates', function (Blueprint $table) {
$table->integer('breath_hold')->default(0)->after('exhale');
$table->integer('duration')->default(60)->after('breath_hold');
$table->dropColumn('repeat'); // only if you are sure you no longer need it
});
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::table('breathing_templates', function (Blueprint $table) {
$table->dropColumn(['breath_hold', 'duration']);
$table->integer('repeat')->default(1); // rollback safety
});
}
};
@@ -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('user_breathing_sessions', function (Blueprint $table) {
$table ->integer('duration')->default(60)->after('template_id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('user_breathing_sessions', function (Blueprint $table) {
$table->dropColumn('duration');
});
}
};
+73 -11
View File
@@ -4,22 +4,84 @@
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use App\Models\BreathingTemplate; use App\Models\BreathingTemplate;
use Illuminate\Support\Facades\DB;
class BreathingTemplateSeeder extends Seeder class BreathingTemplateSeeder extends Seeder
{ {
public function run(): void public function run(): void
{ {
BreathingTemplate::query()->delete();
DB::statement('ALTER TABLE breathing_templates AUTO_INCREMENT = 1');
$templates = [ $templates = [
['name' => 'نفس آرام', 'inhale' => 4, 'exhale' => 4, 'repeat' => 5], [
['name' => 'نفس تمرکزی', 'inhale' => 3, 'exhale' => 3, 'repeat' => 8], 'name' => '۴-۷-۸ تنفس آرام',
['name' => 'نفس تسکین دهنده', 'inhale' => 6, 'exhale' => 6, 'repeat' => 10], 'inhale' => 4,
['name' => 'نفس انرژی بخش', 'inhale' => 2, 'exhale' => 2, 'repeat' => 12], 'exhale' => 8,
['name' => 'نفس عمیق', 'inhale' => 5, 'exhale' => 5, 'repeat' => 7], 'breath_hold' => 7,
['name' => 'نفس آرامش بخش', 'inhale' => 4, 'exhale' => 6, 'repeat' => 6], 'duration' => 60
['name' => 'نفس پاکسازی', 'inhale' => 3, 'exhale' => 5, 'repeat' => 8], ],
['name' => 'نفس ذهنی', 'inhale' => 4, 'exhale' => 4, 'repeat' => 9], [
['name' => 'نفس کنترل استرس', 'inhale' => 5, 'exhale' => 7, 'repeat' => 6], 'name' => 'تنفس جعبه‌ای (Box Breathing)',
['name' => 'نفس تمرینی', 'inhale' => 3, 'exhale' => 4, 'repeat' => 10], 'inhale' => 4,
'exhale' => 4,
'breath_hold' => 4,
'duration' => 60
],
[
'name' => 'تنفس هماهنگ (Coherent Breathing)',
'inhale' => 5,
'exhale' => 5,
'breath_hold' => 0,
'duration' => 60
],
[
'name' => 'تنفس آرام عمیق',
'inhale' => 6,
'exhale' => 6,
'breath_hold' => 0,
'duration' => 90
],
[
'name' => 'تنفس تمرکزی ساده',
'inhale' => 4,
'exhale' => 4,
'breath_hold' => 0,
'duration' => 60
],
[
'name' => 'تنفس برای کاهش استرس',
'inhale' => 5,
'exhale' => 7,
'breath_hold' => 0,
'duration' => 120
],
[
'name' => 'تنفس پاکسازی',
'inhale' => 3,
'exhale' => 5,
'breath_hold' => 0,
'duration' => 60
],
[
'name' => 'تنفس ذهنی کوتاه',
'inhale' => 4,
'exhale' => 4,
'breath_hold' => 0,
'duration' => 30
],
[
'name' => 'تنفس خواب‌آور ۴-۷-۸',
'inhale' => 4,
'exhale' => 8,
'breath_hold' => 7,
'duration' => 120
],
[
'name' => 'تنفس تجدید انرژی',
'inhale' => 2,
'exhale' => 4,
'breath_hold' => 0,
'duration' => 45
],
]; ];
foreach ($templates as $template) { foreach ($templates as $template) {