From 8ca4085b861719c2720f7a240f23dd32b2e63839 Mon Sep 17 00:00:00 2001 From: AmirmahdiNourkazemi Date: Fri, 26 Sep 2025 12:13:38 +0330 Subject: [PATCH] feat: add image_id for template --- .../BreathingExerciseController.php | 59 +++++++++++++------ app/Models/BreathingTemplate.php | 7 ++- ..._image_id_to_breathing_templates_table.php | 30 ++++++++++ routes/api.php | 3 - 4 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 database/migrations/2025_09_26_081749_add_image_id_to_breathing_templates_table.php diff --git a/app/Http/Controllers/BreathingExerciseController.php b/app/Http/Controllers/BreathingExerciseController.php index 4cb24eb..f4d0037 100644 --- a/app/Http/Controllers/BreathingExerciseController.php +++ b/app/Http/Controllers/BreathingExerciseController.php @@ -17,7 +17,8 @@ public function createTemplate(Request $request) 'exhale' => 'required|integer|min:0', 'breath_hold' => 'required|integer|min:0', 'duration' => 'sometimes|integer|min:0', - 'description' => 'sometimes|string|nullable' + 'description' => 'sometimes|string|nullable', + 'image_id' => 'nullable|exists:images,id', ]); $template = BreathingTemplate::create([ @@ -27,10 +28,11 @@ public function createTemplate(Request $request) 'exhale' => $data['exhale'], 'breath_hold'=> $data['breath_hold'] ?? 0, 'duration'=> $data['duration'] ?? 60, - 'description' => $data['description'] ?? null + 'description' => $data['description'] ?? null, + 'image_id' => $data['image_id'] ?? null, ]); - return response()->json(['message' => 'Template created', 'template' => $template]); + return response()->json(['message' => 'Template created', 'template' => $template->load('image')]); } public function updateTemplate(Request $request, $id) @@ -45,12 +47,13 @@ public function updateTemplate(Request $request, $id) 'exhale' => 'sometimes|required|integer|min:0', 'breath_hold' => 'sometimes|integer|min:0', 'duration' => 'sometimes|integer|min:0', - 'description' => 'sometimes|string|nullable' + 'description' => 'sometimes|string|nullable', + 'image_id' => 'nullable|exists:images,id', ]); $template->update($data); - return response()->json(['message' => 'Template updated', 'template' => $template]); + return response()->json(['message' => 'Template updated', 'template' => $template->load('image')]); } public function deleteTemplate($id) { @@ -63,21 +66,43 @@ public function deleteTemplate($id) return response()->json(['message' => 'Template deleted']); } - public function getUserTemplates() - { - $templates = BreathingTemplate::where('user_id', auth()->id())->get(); - return response()->json($templates); - } + public function getTemplates() +{ + $userId = auth()->id(); + $templates = BreathingTemplate::whereNull('user_id') + ->orWhere('user_id', $userId) + ->with('image') // eager load image + ->get() + ->map(function ($template) { + if ($template->image) { + $template->image_url = asset('storage/' . $template->image->path); + } else { + $template->image_url = null; + } + return $template; + }); - public function getTemplates() - { - $templates = BreathingTemplate::whereNull('user_id') - ->orWhere('user_id', auth()->id()) - ->get(); + return response()->json($templates); +} + +public function getUserTemplates() +{ + $templates = BreathingTemplate::where('user_id', auth()->id()) + ->with('image') // eager load image + ->get() + ->map(function ($template) { + if ($template->image) { + $template->image_url = asset('storage/' . $template->image->path); + } else { + $template->image_url = null; + } + return $template; + }); + + return response()->json($templates); +} - return response()->json($templates); - } // public function store(Request $request) // { // $data = $request->validate([ diff --git a/app/Models/BreathingTemplate.php b/app/Models/BreathingTemplate.php index bc2c578..5526649 100644 --- a/app/Models/BreathingTemplate.php +++ b/app/Models/BreathingTemplate.php @@ -6,7 +6,7 @@ class BreathingTemplate extends Model { - protected $fillable = ['user_id', 'name', 'inhale', 'exhale', 'breath_hold' , 'duration' , 'description']; + protected $fillable = ['user_id', 'name', 'inhale', 'exhale', 'breath_hold' , 'duration' , 'description','image_id']; public function user() { @@ -17,4 +17,9 @@ public function sessions() { return $this->hasMany(UserBreathingSession::class, 'template_id'); } + + public function image() + { + return $this->belongsTo(Image::class); + } } diff --git a/database/migrations/2025_09_26_081749_add_image_id_to_breathing_templates_table.php b/database/migrations/2025_09_26_081749_add_image_id_to_breathing_templates_table.php new file mode 100644 index 0000000..d919a08 --- /dev/null +++ b/database/migrations/2025_09_26_081749_add_image_id_to_breathing_templates_table.php @@ -0,0 +1,30 @@ +unsignedBigInteger('image_id')->nullable()->after('description'); + $table->foreign('image_id')->references('id')->on('images')->onDelete('set null'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('breathing_templates', function (Blueprint $table) { + $table->dropForeign(['image_id']); + $table->dropColumn('image_id'); + }); + } +}; diff --git a/routes/api.php b/routes/api.php index 1007087..c5524d6 100644 --- a/routes/api.php +++ b/routes/api.php @@ -109,7 +109,6 @@ Route::get('/slider', [SliderController::class, 'index']); /// images - Route::post('/images', [ImageController::class, 'store']); Route::get('/images/private', [ImageController::class, 'userImages']); Route::get('/images/all', [ImageController::class, 'allImages']); @@ -119,6 +118,4 @@ - - }); \ No newline at end of file