API template

This commit is contained in:
Alfa Ramadhan 2025-09-08 14:23:18 +07:00
parent 2b945a5243
commit bfbc8db1fc
5 changed files with 105 additions and 72 deletions

View File

@ -0,0 +1,101 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Template;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class TemplateController extends Controller
{
/**
* Mengambil daftar semua template beserta kategorinya.
*/
public function index()
{
$templates = Template::with('kategori')->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);
}
}

View File

@ -1,63 +0,0 @@
<?php
namespace App\Http\Controllers;
use App\Models\Template;
use Illuminate\Http\Request;
class TemplateController extends Controller
{
// Get all templates
public function index()
{
return Template::with('kategori')->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);
}
}

View File

@ -13,22 +13,19 @@ class Template extends Model
protected $primaryKey = 'id_template'; protected $primaryKey = 'id_template';
protected $fillable = [ protected $fillable = [
'kategori_id', 'kategori_id', // Sesuaikan dengan nama kolom di database yang sudah benar
'nama_template', 'nama_template',
'fitur', 'fitur',
'foto', 'foto',
]; ];
// Relasi ke kategori
public function kategori() public function kategori()
{ {
return $this->belongsTo(Kategori::class, 'kategori_id'); return $this->belongsTo(Kategori::class, 'kategori_id');
} }
// Relasi ke pemesanan
public function pemesanan() public function pemesanan()
{ {
return $this->hasMany(Pemesanan::class, 'id_template', 'id_template'); return $this->hasMany(Pemesanan::class, 'id_template', 'id_template');
} }
} }

View File

@ -13,14 +13,13 @@ return new class extends Migration
{ {
Schema::create('templates', function (Blueprint $table) { Schema::create('templates', function (Blueprint $table) {
$table->id('id_template'); // Primary Key $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->string('nama_template');
$table->text('fitur')->nullable(); $table->text('fitur')->nullable();
$table->string('foto')->nullable(); $table->string('foto')->nullable();
$table->timestamps(); $table->timestamps();
// Relasi ke tabel kategori // Relasi ke tabel kategori
$table->unsignedBigInteger('kategori_id');
$table->foreign('kategori_id')->references('id')->on('kategoris')->onDelete('cascade'); $table->foreign('kategori_id')->references('id')->on('kategoris')->onDelete('cascade');
}); });
} }

View File

@ -3,8 +3,7 @@
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\ReviewController; use App\Http\Controllers\Api\ReviewController;
use App\Http\Controllers\TemplateController; use App\Http\Controllers\Api\TemplateController;
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------