[Update FormApi]

This commit is contained in:
Arief Dwi Wicaksono 2025-09-19 09:28:50 +07:00
parent 80c1fdda8b
commit d4f5e5ef76
5 changed files with 303 additions and 220 deletions

View File

@ -0,0 +1,79 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Pelanggan;
use App\Models\PelangganDetail;
use App\Models\Template;
use Illuminate\Http\Request;
class FormApiController extends Controller
{
public function store(Request $request)
{
// ✅ Validasi dasar
$rules = [
'template_id' => 'required|exists:templates,id',
'nama_pemesan' => 'required|string|max:255',
'no_hp' => 'required|string|max:20',
'email' => 'required|email',
];
// ✅ Ambil template & fiturnya
$template = Template::with(['fiturs', 'kategori'])->findOrFail($request->template_id);
// ✅ Loop fitur → generate aturan validasi dinamis
foreach ($template->fiturs as $fitur) {
$field = str_replace(' ', '_', strtolower($fitur->deskripsi)); // contoh: "Nama Lengkap Pria" → "nama_lengkap_pria"
// Aturan default: required string max 255
$rules[$field] = 'nullable|string|max:255';
// Kalau fitur ada kata "tanggal"
if (str_contains(strtolower($fitur->deskripsi), 'tanggal')) {
$rules[$field] = 'nullable|date';
}
// Kalau fitur ada kata "foto" atau "galeri"
if (str_contains(strtolower($fitur->deskripsi), 'galeri')) {
$rules['galeri'] = 'nullable|array|max:10';
$rules['galeri.*'] = 'image|mimes:jpeg,png,jpg,gif|max:2048';
}
}
// ✅ Jalankan validasi
$data = $request->validate($rules);
// --- PROSES UPLOAD GALERI ---
$galleryPaths = [];
if ($request->hasFile('galeri')) {
foreach ($request->file('galeri') as $file) {
$galleryPaths[] = $file->store('gallery', 'public');
}
}
$data['galeri'] = $galleryPaths;
// ✅ Simpan ke tabel pelanggan
$pelanggan = Pelanggan::create([
'nama_pemesan' => $data['nama_pemesan'],
'nama_template' => $template->nama_template,
'kategori' => $template->kategori->nama ?? '-',
'email' => $data['email'],
'no_tlpn' => $data['no_hp'],
'harga' => $template->harga,
]);
// ✅ Simpan detail form (dinamis)
PelangganDetail::create([
'pelanggan_id' => $pelanggan->id,
'detail_form' => $data,
]);
return response()->json([
'success' => true,
'message' => 'Form berhasil dikirim sesuai fitur template',
'data' => $pelanggan->load('details')
], 201);
}
}

View File

@ -1,80 +1,80 @@
<?php <?php
namespace App\Http\Controllers\Api; // namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; // use App\Http\Controllers\Controller;
use App\Models\Pelanggan; // use App\Models\Pelanggan;
use App\Models\PelangganDetail; // use App\Models\PelangganDetail;
use App\Models\Template; // ✅ tambahkan ini // use App\Models\Template; // ✅ tambahkan ini
use Illuminate\Http\Request; // use Illuminate\Http\Request;
use Illuminate\Support\Arr; // use Illuminate\Support\Arr;
class KhitanApiController extends Controller // class KhitanApiController extends Controller
{ // {
public function store(Request $request) // public function store(Request $request)
{ // {
$data = $request->validate([ // $data = $request->validate([
'template_id' => 'required|exists:templates,id', // 'template_id' => 'required|exists:templates,id',
'nama_pemesan' => 'required|string|max:255', // 'nama_pemesan' => 'required|string|max:255',
'no_hp' => 'required|string|max:20', // 'no_hp' => 'required|string|max:20',
'email' => 'required|email', // 'email' => 'required|email',
// Anak // // Anak
'nama_lengkap_anak' => 'required|string|max:255', // 'nama_lengkap_anak' => 'required|string|max:255',
'nama_panggilan_anak' => 'required|string|max:255', // 'nama_panggilan_anak' => 'required|string|max:255',
'bapak_anak' => 'nullable|string|max:255', // 'bapak_anak' => 'nullable|string|max:255',
'ibu_anak' => 'nullable|string|max:255', // 'ibu_anak' => 'nullable|string|max:255',
// Jadwal // // Jadwal
'hari_tanggal_acara' => 'nullable|date', // 'hari_tanggal_acara' => 'nullable|date',
'waktu_acara' => 'nullable|string', // 'waktu_acara' => 'nullable|string',
'alamat_acara' => 'nullable|string', // 'alamat_acara' => 'nullable|string',
'maps_acara' => 'nullable|string', // 'maps_acara' => 'nullable|string',
// Tambahan // // Tambahan
'no_rekening1' => 'nullable|string', // 'no_rekening1' => 'nullable|string',
'no_rekening2' => 'nullable|string', // 'no_rekening2' => 'nullable|string',
'link_musik' => 'nullable|string', // 'link_musik' => 'nullable|string',
'galeri' => 'nullable|array|max:5', // 'galeri' => 'nullable|array|max:5',
'galeri.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048', // 'galeri.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
]); // ]);
// --- PROSES UPLOAD GAMBAR --- // // --- PROSES UPLOAD GAMBAR ---
$galleryPaths = []; // $galleryPaths = [];
if ($request->hasFile('galeri')) { // if ($request->hasFile('galeri')) {
foreach ($request->file('galeri') as $file) { // foreach ($request->file('galeri') as $file) {
// Simpan file ke storage/app/public/gallery dan dapatkan path-nya // // Simpan file ke storage/app/public/gallery dan dapatkan path-nya
$path = $file->store('gallery', 'public'); // $path = $file->store('gallery', 'public');
$galleryPaths[] = $path; // $galleryPaths[] = $path;
} // }
} // }
// Tambahkan path gambar ke dalam data yang akan disimpan // // Tambahkan path gambar ke dalam data yang akan disimpan
$data['galeri'] = $galleryPaths; // $data['galeri'] = $galleryPaths;
// ✅ Ambil template dari database // // ✅ Ambil template dari database
$template = Template::with('kategori')->findOrFail($data['template_id']); // $template = Template::with('kategori')->findOrFail($data['template_id']);
// ✅ Simpan ke tabel pelanggan // // ✅ Simpan ke tabel pelanggan
$pelanggan = Pelanggan::create([ // $pelanggan = Pelanggan::create([
'nama_pemesan' => $data['nama_pemesan'], // 'nama_pemesan' => $data['nama_pemesan'],
'nama_template' => $template->nama_template, // 'nama_template' => $template->nama_template,
'kategori' => $template->kategori->nama ?? 'khitan', // 'kategori' => $template->kategori->nama ?? 'khitan',
'email' => $data['email'], // 'email' => $data['email'],
'no_tlpn' => $data['no_hp'], // 'no_tlpn' => $data['no_hp'],
'harga' => $template->harga, // 'harga' => $template->harga,
]); // ]);
// ✅ Simpan detail form ke tabel pelanggan_details // // ✅ Simpan detail form ke tabel pelanggan_details
PelangganDetail::create([ // PelangganDetail::create([
'pelanggan_id' => $pelanggan->id, // 'pelanggan_id' => $pelanggan->id,
'detail_form' => $data, // 'detail_form' => $data,
]); // ]);
return response()->json([ // return response()->json([
'success' => true, // 'success' => true,
'message' => 'Form khitan berhasil dikirim', // 'message' => 'Form khitan berhasil dikirim',
'data' => $pelanggan->load('details') // 'data' => $pelanggan->load('details')
], 201); // ], 201);
} // }
} // }

View File

@ -1,101 +1,101 @@
<?php <?php
namespace App\Http\Controllers\Api; // namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; // use App\Http\Controllers\Controller;
use App\Models\Pelanggan; // use App\Models\Pelanggan;
use App\Models\PelangganDetail; // use App\Models\PelangganDetail;
use App\Models\Template; // ✅ tambahkan ini // use App\Models\Template; // ✅ tambahkan ini
use Illuminate\Http\Request; // use Illuminate\Http\Request;
use Illuminate\Support\Arr; // use Illuminate\Support\Arr;
class PernikahanApiController extends Controller // class PernikahanApiController extends Controller
{ // {
public function store(Request $request) // public function store(Request $request)
{ // {
$data = $request->validate([ // $data = $request->validate([
'template_id' => 'required|exists:templates,id', // 'template_id' => 'required|exists:templates,id',
'nama_pemesan' => 'required|string|max:255', // 'nama_pemesan' => 'required|string|max:255',
'no_hp' => 'required|string|max:20', // 'no_hp' => 'required|string|max:20',
'email' => 'required|email', // 'email' => 'required|email',
// Pria // // Pria
'nama_lengkap_pria' => 'required|string|max:255', // 'nama_lengkap_pria' => 'required|string|max:255',
'nama_panggilan_pria' => 'required|string|max:255', // 'nama_panggilan_pria' => 'required|string|max:255',
'bapak_pria' => 'nullable|string|max:255', // 'bapak_pria' => 'nullable|string|max:255',
'ibu_pria' => 'nullable|string|max:255', // 'ibu_pria' => 'nullable|string|max:255',
'instagram_pria' => 'nullable|string', // 'instagram_pria' => 'nullable|string',
'facebook_pria' => 'nullable|string', // 'facebook_pria' => 'nullable|string',
'twitter_pria' => 'nullable|string', // 'twitter_pria' => 'nullable|string',
// Wanita // // Wanita
'nama_lengkap_wanita' => 'required|string|max:255', // 'nama_lengkap_wanita' => 'required|string|max:255',
'nama_panggilan_wanita' => 'required|string|max:255', // 'nama_panggilan_wanita' => 'required|string|max:255',
'bapak_wanita' => 'nullable|string|max:255', // 'bapak_wanita' => 'nullable|string|max:255',
'ibu_wanita' => 'nullable|string|max:255', // 'ibu_wanita' => 'nullable|string|max:255',
'instagram_wanita' => 'nullable|string', // 'instagram_wanita' => 'nullable|string',
'facebook_wanita' => 'nullable|string', // 'facebook_wanita' => 'nullable|string',
'twitter_wanita' => 'nullable|string', // 'twitter_wanita' => 'nullable|string',
// Cerita // // Cerita
'cerita_kita' => 'nullable|string', // 'cerita_kita' => 'nullable|string',
// Akad // // Akad
'hari_tanggal_akad' => 'nullable|date', // 'hari_tanggal_akad' => 'nullable|date',
'waktu_akad' => 'nullable|string', // 'waktu_akad' => 'nullable|string',
'alamat_akad' => 'nullable|string', // 'alamat_akad' => 'nullable|string',
'maps_akad' => 'nullable|string', // 'maps_akad' => 'nullable|string',
// Resepsi // // Resepsi
'hari_tanggal_resepsi' => 'nullable|date', // 'hari_tanggal_resepsi' => 'nullable|date',
'waktu_resepsi' => 'nullable|string', // 'waktu_resepsi' => 'nullable|string',
'alamat_resepsi' => 'nullable|string', // 'alamat_resepsi' => 'nullable|string',
'maps_resepsi' => 'nullable|string', // 'maps_resepsi' => 'nullable|string',
// Tambahan // // Tambahan
'no_rekening1' => 'nullable|string', // 'no_rekening1' => 'nullable|string',
'no_rekening2' => 'nullable|string', // 'no_rekening2' => 'nullable|string',
'link_musik' => 'nullable|string', // 'link_musik' => 'nullable|string',
'galeri' => 'nullable|array|max:10', // 'galeri' => 'nullable|array|max:10',
'galeri.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048', // 'galeri.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
]); // ]);
// --- PROSES UPLOAD GAMBAR --- // // --- PROSES UPLOAD GAMBAR ---
$galleryPaths = []; // $galleryPaths = [];
if ($request->hasFile('galeri')) { // if ($request->hasFile('galeri')) {
foreach ($request->file('galeri') as $file) { // foreach ($request->file('galeri') as $file) {
// Simpan file ke storage/app/public/gallery dan dapatkan path-nya // // Simpan file ke storage/app/public/gallery dan dapatkan path-nya
$path = $file->store('gallery', 'public'); // $path = $file->store('gallery', 'public');
$galleryPaths[] = $path; // $galleryPaths[] = $path;
} // }
} // }
// Ganti 'galeri' di $data dengan array path yang sudah disimpan // // Ganti 'galeri' di $data dengan array path yang sudah disimpan
$data['galeri'] = $galleryPaths; // $data['galeri'] = $galleryPaths;
// ✅ Ambil template berdasarkan template_id // // ✅ Ambil template berdasarkan template_id
$template = Template::with('kategori')->findOrFail($data['template_id']); // $template = Template::with('kategori')->findOrFail($data['template_id']);
// ✅ Simpan ke tabel pelanggan // // ✅ Simpan ke tabel pelanggan
$pelanggan = Pelanggan::create([ // $pelanggan = Pelanggan::create([
'nama_pemesan' => $data['nama_pemesan'], // 'nama_pemesan' => $data['nama_pemesan'],
'nama_template' => $template->nama_template, // 'nama_template' => $template->nama_template,
'kategori' => $template->kategori->nama ?? '-', // 'kategori' => $template->kategori->nama ?? '-',
'email' => $data['email'], // 'email' => $data['email'],
'no_tlpn' => $data['no_hp'], // 'no_tlpn' => $data['no_hp'],
'harga' => $template->harga, // 'harga' => $template->harga,
]); // ]);
// ✅ Simpan detail form ke tabel pelanggan_details // // ✅ Simpan detail form ke tabel pelanggan_details
PelangganDetail::create([ // PelangganDetail::create([
'pelanggan_id' => $pelanggan->id, // 'pelanggan_id' => $pelanggan->id,
'detail_form' => $data, // 'detail_form' => $data,
]); // ]);
return response()->json([ // return response()->json([
'success' => true, // 'success' => true,
'message' => 'Form pernikahan berhasil dikirim', // 'message' => 'Form pernikahan berhasil dikirim',
'data' => $pelanggan->load('details') // 'data' => $pelanggan->load('details')
], 201); // ], 201);
} // }
} // }

View File

@ -1,75 +1,75 @@
<?php <?php
namespace App\Http\Controllers\Api; // namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; // use App\Http\Controllers\Controller;
use App\Models\Pelanggan; // use App\Models\Pelanggan;
use App\Models\PelangganDetail; // use App\Models\PelangganDetail;
use App\Models\Template; // ✅ tambahkan ini // use App\Models\Template; // ✅ tambahkan ini
use Illuminate\Http\Request; // use Illuminate\Http\Request;
class UlangTahunApiController extends Controller // class UlangTahunApiController extends Controller
{ // {
public function store(Request $request) // public function store(Request $request)
{ // {
$data = $request->validate([ // $data = $request->validate([
'template_id' => 'required|exists:templates,id', // 'template_id' => 'required|exists:templates,id',
'nama_pemesan' => 'required|string|max:255', // 'nama_pemesan' => 'required|string|max:255',
'no_hp' => 'required|string|max:20', // 'no_hp' => 'required|string|max:20',
'email' => 'required|email', // 'email' => 'required|email',
// Data Anak // // Data Anak
'nama_lengkap_anak' => 'required|string|max:255', // 'nama_lengkap_anak' => 'required|string|max:255',
'nama_panggilan_anak' => 'required|string|max:100', // 'nama_panggilan_anak' => 'required|string|max:100',
'bapak_anak' => 'required|string|max:255', // 'bapak_anak' => 'required|string|max:255',
'ibu_anak' => 'required|string|max:255', // 'ibu_anak' => 'required|string|max:255',
'umur_dirayakan' => 'required|string|max:10', // 'umur_dirayakan' => 'required|string|max:10',
'anak_ke' => 'required|string|max:5', // 'anak_ke' => 'required|string|max:5',
// Jadwal // // Jadwal
'hari_tanggal_acara' => 'required|date', // 'hari_tanggal_acara' => 'required|date',
'waktu_acara' => 'required|string|max:50', // 'waktu_acara' => 'required|string|max:50',
'alamat_acara' => 'required|string', // 'alamat_acara' => 'required|string',
'maps_acara' => 'nullable|string', // 'maps_acara' => 'nullable|string',
'link_musik' => 'nullable|string', // 'link_musik' => 'nullable|string',
// --- PERBAIKAN VALIDASI GALERI --- // // --- PERBAIKAN VALIDASI GALERI ---
'galeri' => 'nullable|array|max:5', // 'galeri' => 'nullable|array|max:5',
'galeri.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048', // 'galeri.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
]); // ]);
// --- PROSES UPLOAD GAMBAR --- // // --- PROSES UPLOAD GAMBAR ---
$galleryPaths = []; // $galleryPaths = [];
if ($request->hasFile('galeri')) { // if ($request->hasFile('galeri')) {
foreach ($request->file('galeri') as $file) { // foreach ($request->file('galeri') as $file) {
$path = $file->store('gallery', 'public'); // $path = $file->store('gallery', 'public');
$galleryPaths[] = $path; // $galleryPaths[] = $path;
} // }
} // }
$data['galeri'] = $galleryPaths; // $data['galeri'] = $galleryPaths;
// ✅ Ambil template berdasarkan template_id // // ✅ Ambil template berdasarkan template_id
$template = Template::with('kategori')->findOrFail($data['template_id']); // $template = Template::with('kategori')->findOrFail($data['template_id']);
// ✅ Simpan ke tabel pelanggan // // ✅ Simpan ke tabel pelanggan
$pelanggan = Pelanggan::create([ // $pelanggan = Pelanggan::create([
'nama_pemesan' => $data['nama_pemesan'], // 'nama_pemesan' => $data['nama_pemesan'],
'nama_template' => $template->nama_template, // 'nama_template' => $template->nama_template,
'kategori' => $template->kategori->nama ?? 'ulang_tahun', // 'kategori' => $template->kategori->nama ?? 'ulang_tahun',
'email' => $data['email'], // 'email' => $data['email'],
'no_tlpn' => $data['no_hp'], // 'no_tlpn' => $data['no_hp'],
'harga' => $template->harga, // 'harga' => $template->harga,
]); // ]);
// ✅ Simpan detail form ke tabel pelanggan_details // // ✅ Simpan detail form ke tabel pelanggan_details
PelangganDetail::create([ // PelangganDetail::create([
'pelanggan_id' => $pelanggan->id, // 'pelanggan_id' => $pelanggan->id,
'detail_form' => $data, // 'detail_form' => $data,
]); // ]);
return response()->json([ // return response()->json([
'success' => true, // 'success' => true,
'message' => 'Form ulang tahun berhasil dikirim', // 'message' => 'Form ulang tahun berhasil dikirim',
'data' => $pelanggan->load('details') // 'data' => $pelanggan->load('details')
], 201); // ], 201);
} // }
} // }

View File

@ -8,11 +8,15 @@ use App\Http\Controllers\Api\PernikahanApiController;
use App\Http\Controllers\Api\UlangTahunApiController; use App\Http\Controllers\Api\UlangTahunApiController;
use App\Http\Controllers\Api\KhitanApiController; use App\Http\Controllers\Api\KhitanApiController;
use App\Http\Controllers\Api\TemplateApiController; use App\Http\Controllers\Api\TemplateApiController;
use App\Http\Controllers\Api\FormApiController;
// Form API (user) // Form API (universal, dinamis berdasarkan template_id)
Route::post('form/pernikahan', [PernikahanApiController::class, 'store']); Route::post('form', [FormApiController::class, 'store']);
Route::post('form/ulang-tahun', [UlangTahunApiController::class, 'store']);
Route::post('form/khitan', [KhitanApiController::class, 'store']); // // Form API (user)
// Route::post('form/pernikahan', [PernikahanApiController::class, 'store']);
// Route::post('form/ulang-tahun', [UlangTahunApiController::class, 'store']);
// Route::post('form/khitan', [KhitanApiController::class, 'store']);
// API Kategori hanya read-only // API Kategori hanya read-only
Route::get('kategoris', [KategoriApiController::class, 'index']); Route::get('kategoris', [KategoriApiController::class, 'index']);