Compare commits
2 Commits
7cba5c3ebd
...
2b945a5243
Author | SHA1 | Date | |
---|---|---|---|
|
2b945a5243 | ||
|
68479e8844 |
63
backend/app/Http/Controllers/TemplateController.php
Normal file
63
backend/app/Http/Controllers/TemplateController.php
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
34
backend/app/Models/Template.php
Normal file
34
backend/app/Models/Template.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Template extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $table = 'templates';
|
||||||
|
protected $primaryKey = 'id_template';
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'kategori_id',
|
||||||
|
'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');
|
||||||
|
}
|
||||||
|
}
|
23
backend/database/factories/TemplateFactory.php
Normal file
23
backend/database/factories/TemplateFactory.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Template>
|
||||||
|
*/
|
||||||
|
class TemplateFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
<?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::create('templates', function (Blueprint $table) {
|
||||||
|
$table->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');
|
||||||
|
}
|
||||||
|
};
|
@ -3,6 +3,8 @@
|
|||||||
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;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
@ -16,3 +18,4 @@ use App\Http\Controllers\Api\ReviewController;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Route::apiResource('reviews', ReviewController::class);
|
Route::apiResource('reviews', ReviewController::class);
|
||||||
|
Route::apiResource('templates', TemplateController::class);
|
||||||
|
Loading…
Reference in New Issue
Block a user