From 68479e8844b74aa687ce544830f0e5571051518d Mon Sep 17 00:00:00 2001 From: Alfa Ramadhan <127267285+ALFARAMADHAN@users.noreply.github.com> Date: Thu, 4 Sep 2025 13:34:29 +0700 Subject: [PATCH] Template --- .../Http/Controllers/TemplateController.php | 63 +++++++++++++++++++ backend/app/Models/Template.php | 34 ++++++++++ .../database/factories/TemplateFactory.php | 23 +++++++ ...25_09_03_042108_create_templates_table.php | 35 +++++++++++ backend/routes/api.php | 3 + 5 files changed, 158 insertions(+) create mode 100644 backend/app/Http/Controllers/TemplateController.php create mode 100644 backend/app/Models/Template.php create mode 100644 backend/database/factories/TemplateFactory.php create mode 100644 backend/database/migrations/2025_09_03_042108_create_templates_table.php diff --git a/backend/app/Http/Controllers/TemplateController.php b/backend/app/Http/Controllers/TemplateController.php new file mode 100644 index 0000000..1af4558 --- /dev/null +++ b/backend/app/Http/Controllers/TemplateController.php @@ -0,0 +1,63 @@ +get(); + } + + // Store template + public function store(Request $request) + { + $validated = $request->validate([ + 'kategori_id' => 'required|exists:kategoris,id', + 'nama_template' => 'required|string|max:255', + 'fitur' => 'nullable|string', + 'foto' => 'nullable|string', + ]); + + + $template = Template::create($validated); + + return response()->json($template, 201); + } + + // Show template by id + public function show($id) + { + return Template::with('kategori')->findOrFail($id); + } + + // Update template + public function update(Request $request, $id) + { + $template = Template::findOrFail($id); + + $validated = $request->validate([ + 'id_kategori' => 'required|exists:kategori,id_kategori', + 'nama_template' => 'required|string|max:255', + 'fitur' => 'nullable|string', + 'foto' => 'nullable|string', + ]); + + $template->update($validated); + + return response()->json($template); + } + + // Delete template + public function destroy($id) + { + $template = Template::findOrFail($id); + $template->delete(); + + return response()->json(null, 204); + } +} diff --git a/backend/app/Models/Template.php b/backend/app/Models/Template.php new file mode 100644 index 0000000..9a2f5f6 --- /dev/null +++ b/backend/app/Models/Template.php @@ -0,0 +1,34 @@ +belongsTo(Kategori::class, 'kategori_id'); + } + + // Relasi ke pemesanan + public function pemesanan() + { + return $this->hasMany(Pemesanan::class, 'id_template', 'id_template'); + } +} diff --git a/backend/database/factories/TemplateFactory.php b/backend/database/factories/TemplateFactory.php new file mode 100644 index 0000000..abca374 --- /dev/null +++ b/backend/database/factories/TemplateFactory.php @@ -0,0 +1,23 @@ + + */ +class TemplateFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/backend/database/migrations/2025_09_03_042108_create_templates_table.php b/backend/database/migrations/2025_09_03_042108_create_templates_table.php new file mode 100644 index 0000000..0162652 --- /dev/null +++ b/backend/database/migrations/2025_09_03_042108_create_templates_table.php @@ -0,0 +1,35 @@ +id('id_template'); // Primary Key + $table->unsignedBigInteger('id_kategori'); // Foreign Key ke kategori + $table->string('nama_template'); + $table->text('fitur')->nullable(); + $table->string('foto')->nullable(); + $table->timestamps(); + + // Relasi ke tabel kategori + $table->unsignedBigInteger('kategori_id'); + $table->foreign('kategori_id')->references('id')->on('kategoris')->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('templates'); + } +}; diff --git a/backend/routes/api.php b/backend/routes/api.php index bb8314e..3fbf9fe 100644 --- a/backend/routes/api.php +++ b/backend/routes/api.php @@ -3,6 +3,8 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; use App\Http\Controllers\Api\ReviewController; +use App\Http\Controllers\TemplateController; + /* |-------------------------------------------------------------------------- @@ -16,3 +18,4 @@ use App\Http\Controllers\Api\ReviewController; */ Route::apiResource('reviews', ReviewController::class); +Route::apiResource('templates', TemplateController::class);