get(); return response()->json([ "success" => true, "data" => $data, ]); } /** * Ambil satu data berdasarkan kode (dipakai saat buka form edit). */ public function show(string $kode): JsonResponse { $item = MasterStasiunKerja::find($kode); if (!$item) { return response()->json( [ "success" => false, "message" => "Data stasiun kerja tidak ditemukan.", ], 404, ); } return response()->json([ "success" => true, "data" => $item, ]); } /** * Simpan data stasiun kerja baru. */ public function store(Request $request): JsonResponse { $validator = Validator::make($request->all(), [ "kode" => ["required", "string", "max:50", Rule::unique("stasiunkerja", "kode")], "nama" => ["required", "string", "max:255"], "margin" => ["nullable", "integer"], "spv" => ["nullable", "string", "max:20"], "target" => ["nullable", "integer"], "id_lokasi" => ["nullable", "string", "max:10"], "lantai" => ["nullable", "string", "max:4"], "status" => ["nullable", "integer"], "alamat" => ["nullable", "string", "max:255"], "bank" => ["nullable", "string", "max:64"], "telp" => ["nullable", "string", "max:32"], "kode_akses" => ["nullable", "string", "max:5"], ]); if ($validator->fails()) { return response()->json( [ "success" => false, "message" => "Validasi gagal.", "errors" => $validator->errors(), ], 422, ); } $item = MasterStasiunKerja::create($validator->validated()); return response()->json( [ "success" => true, "message" => "Data stasiun kerja berhasil ditambahkan.", "data" => $item, ], 201, ); } /** * Perbarui data stasiun kerja berdasarkan kode. */ public function update(Request $request, string $kode): JsonResponse { $item = MasterStasiunKerja::find($kode); if (!$item) { return response()->json( [ "success" => false, "message" => "Data stasiun kerja tidak ditemukan.", ], 404, ); } $validator = Validator::make($request->all(), [ "nama" => ["required", "string", "max:255"], "margin" => ["nullable", "integer"], "spv" => ["nullable", "string", "max:20"], "target" => ["nullable", "integer"], "id_lokasi" => ["nullable", "string", "max:10"], "lantai" => ["nullable", "string", "max:4"], "status" => ["nullable", "integer"], "alamat" => ["nullable", "string", "max:255"], "bank" => ["nullable", "string", "max:64"], "telp" => ["nullable", "string", "max:32"], "kode_akses" => ["nullable", "string", "max:5"], ]); if ($validator->fails()) { return response()->json( [ "success" => false, "message" => "Validasi gagal.", "errors" => $validator->errors(), ], 422, ); } $item->update($validator->validated()); return response()->json([ "success" => true, "message" => "Data stasiun kerja berhasil diperbarui.", "data" => $item, ]); } /** * Hapus data stasiun kerja berdasarkan kode. */ public function destroy(string $kode): JsonResponse { $item = MasterStasiunKerja::find($kode); if (!$item) { return response()->json( [ "success" => false, "message" => "Data stasiun kerja tidak ditemukan.", ], 404, ); } $item->delete(); return response()->json([ "success" => true, "message" => "Data stasiun kerja berhasil dihapus.", ]); } }