diff --git a/backend/app/Http/Controllers/Api/TemplateController.php b/backend/app/Http/Controllers/Api/TemplateController.php new file mode 100644 index 0000000..ea29625 --- /dev/null +++ b/backend/app/Http/Controllers/Api/TemplateController.php @@ -0,0 +1,101 @@ +get(); + return response()->json($templates); + } + + /** + * Menyimpan template baru. + */ + public function store(Request $request) + { + try { + $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); + } catch (ValidationException $e) { + return response()->json([ + 'message' => 'Validation failed', + 'errors' => $e->errors() + ], 422); + } + } + + /** + * Mengambil detail satu template berdasarkan ID. + */ + public function show($id) + { + $template = Template::with('kategori')->find($id); + + if (!$template) { + return response()->json(['message' => 'Template not found'], 404); + } + + return response()->json($template); + } + + /** + * Memperbarui template yang sudah ada. + */ + public function update(Request $request, $id) + { + $template = Template::find($id); + + if (!$template) { + return response()->json(['message' => 'Template not found'], 404); + } + + try { + $validated = $request->validate([ + 'kategori_id' => 'required|exists:kategoris,id', + 'nama_template' => 'required|string|max:255', + 'fitur' => 'nullable|string', + 'foto' => 'nullable|string', + ]); + + $template->update($validated); + return response()->json($template); + } catch (ValidationException $e) { + return response()->json([ + 'message' => 'Validation failed', + 'errors' => $e->errors() + ], 422); + } + } + + /** + * Menghapus template. + */ + public function destroy($id) + { + $template = Template::find($id); + + if (!$template) { + return response()->json(['message' => 'Template not found'], 404); + } + + $template->delete(); + return response()->json(null, 204); + } +} \ No newline at end of file diff --git a/backend/app/Http/Controllers/TemplateController.php b/backend/app/Http/Controllers/TemplateController.php deleted file mode 100644 index 1af4558..0000000 --- a/backend/app/Http/Controllers/TemplateController.php +++ /dev/null @@ -1,63 +0,0 @@ -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 index 9a2f5f6..f5e0514 100644 --- a/backend/app/Models/Template.php +++ b/backend/app/Models/Template.php @@ -13,22 +13,19 @@ class Template extends Model protected $primaryKey = 'id_template'; protected $fillable = [ - 'kategori_id', + 'kategori_id', // Sesuaikan dengan nama kolom di database yang sudah benar 'nama_template', 'fitur', 'foto', ]; - - // Relasi ke kategori public function kategori() { return $this->belongsTo(Kategori::class, 'kategori_id'); } - // Relasi ke pemesanan public function pemesanan() { return $this->hasMany(Pemesanan::class, 'id_template', 'id_template'); } -} +} \ No newline at end of file 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 index 0162652..d0e7a44 100644 --- a/backend/database/migrations/2025_09_03_042108_create_templates_table.php +++ b/backend/database/migrations/2025_09_03_042108_create_templates_table.php @@ -13,14 +13,13 @@ return new class extends Migration { Schema::create('templates', function (Blueprint $table) { $table->id('id_template'); // Primary Key - $table->unsignedBigInteger('id_kategori'); // Foreign Key ke kategori + $table->unsignedBigInteger('kategori_id'); // 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'); }); } diff --git a/backend/routes/api.php b/backend/routes/api.php index 3fbf9fe..d9a8f4b 100644 --- a/backend/routes/api.php +++ b/backend/routes/api.php @@ -3,8 +3,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; use App\Http\Controllers\Api\ReviewController; -use App\Http\Controllers\TemplateController; - +use App\Http\Controllers\Api\TemplateController; /* |--------------------------------------------------------------------------