Feature: crud reklame
This commit is contained in:
108
app/Http/Controllers/Api/AdvertisementController.php
Normal file
108
app/Http/Controllers/Api/AdvertisementController.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Models\Advertisement;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Requests\AdvertisementRequest;
|
||||
use Illuminate\Http\Response;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\AdvertisementResource;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AdvertisementController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$perPage = $request->input('per_page', 15); // Default 15 jika tidak dikirim oleh client
|
||||
$advertisements = Advertisement::paginate($perPage);
|
||||
|
||||
$advertisements->getCollection()->transform(function ($advertisement) {
|
||||
$village = DB::table('villages')->where('village_code', $advertisement->village_code)->first();
|
||||
$advertisement->village_name = $village ? $village->village_name : null;
|
||||
|
||||
$district = DB::table('districts')->where('district_code', $advertisement->district_code)->first();
|
||||
$advertisement->district_name = $district ? $district->district_name : null;
|
||||
return $advertisement;
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'data' => AdvertisementResource::collection($advertisements),
|
||||
'meta' => [
|
||||
'total' => $advertisements->total(),
|
||||
'per_page' => $advertisements->perPage(),
|
||||
'current_page' => $advertisements->currentPage(),
|
||||
'last_page' => $advertisements->lastPage(),
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(AdvertisementRequest $request): Advertisement
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
// Transformasi village_name dan district_name menjadi village_code dan district_code
|
||||
if (isset($data['village_name'])) {
|
||||
// Ambil village_code berdasarkan village_name dari database
|
||||
$data['village_code'] = DB::table('villages')->where('village_name', $data['village_name'])->value('village_code');
|
||||
unset($data['village_name']); // Hapus village_name jika sudah tidak diperlukan
|
||||
}
|
||||
|
||||
if (isset($data['district_name'])) {
|
||||
// Ambil district_code berdasarkan district_name dari database
|
||||
$data['district_code'] = DB::table('districts')->where('district_name', $data['district_name'])->value('district_code');
|
||||
unset($data['district_name']); // Hapus district_name jika sudah tidak diperlukan
|
||||
}
|
||||
|
||||
// Log data setelah transformasi
|
||||
info($data);
|
||||
return Advertisement::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Advertisement $advertisement): Advertisement
|
||||
{
|
||||
return $advertisement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(AdvertisementRequest $request, Advertisement $advertisement): Advertisement
|
||||
{
|
||||
$data = $request->validated();
|
||||
// Transformasi village_name dan district_name menjadi village_code dan district_code
|
||||
if (isset($data['village_name'])) {
|
||||
// Ambil village_code berdasarkan village_name dari database
|
||||
$data['village_code'] = DB::table('villages')->where('village_name', $data['village_name'])->value('village_code');
|
||||
unset($data['village_name']); // Hapus village_name jika sudah tidak diperlukan
|
||||
}
|
||||
|
||||
if (isset($data['district_name'])) {
|
||||
// Ambil district_code berdasarkan district_name dari database
|
||||
$data['district_code'] = DB::table('districts')->where('district_name', $data['district_name'])->value('district_code');
|
||||
unset($data['district_name']); // Hapus district_name jika sudah tidak diperlukan
|
||||
}
|
||||
|
||||
// Log data setelah transformasi
|
||||
info($data);
|
||||
$advertisement->update($data);
|
||||
|
||||
return $advertisement;
|
||||
}
|
||||
|
||||
public function destroy(Advertisement $advertisement): Response
|
||||
{
|
||||
$advertisement->delete();
|
||||
|
||||
return response()->noContent();
|
||||
}
|
||||
}
|
||||
126
app/Http/Controllers/Data/AdvertisementController.php
Normal file
126
app/Http/Controllers/Data/AdvertisementController.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Data;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Advertisement;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AdvertisementController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('data.advertisements.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$title = 'Advertisement';
|
||||
$subtitle = 'Create Data';
|
||||
|
||||
// Mengambil data untuk dropdown
|
||||
$dropdownOptions = [
|
||||
'village_name' => DB::table('villages')->orderBy('village_name')->pluck('village_name', 'village_code'),
|
||||
'district_name' => DB::table('districts')->orderBy('district_name')->pluck('district_name', 'district_code'),
|
||||
];
|
||||
|
||||
$fields = $this->getFields();
|
||||
$fieldTypes = $this->getFieldTypes();
|
||||
|
||||
$apiUrl = url('/api/advertisements');
|
||||
|
||||
// $route = 'advertisements.create';
|
||||
// info("AdvertisementController@edit diakses dengan ID: $title");
|
||||
return view('form-create-update.form', compact('title', 'subtitle', 'fields', 'fieldTypes', 'apiUrl', 'dropdownOptions'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
info("AdvertisementController@edit diakses dengan ID: $id");
|
||||
$title = 'Advertisement';
|
||||
$subtitle = 'Update Data';
|
||||
$modelInstance = Advertisement::find($id);
|
||||
// Pastikan model ditemukan
|
||||
if (!$modelInstance) {
|
||||
info("AdvertisementController@edit: Model tidak ditemukan.");
|
||||
return redirect()->route('advertisements.index')->with('error', 'Advertisement not found');
|
||||
}
|
||||
|
||||
// Mengambil dan memetakan village_name dan district_name
|
||||
$village = DB::table('villages')->where('village_code', $modelInstance->village_code)->first();
|
||||
$modelInstance->village_name = $village ? $village->village_name : null;
|
||||
|
||||
$district = DB::table('districts')->where('district_code', $modelInstance->district_code)->first();
|
||||
$modelInstance->district_name = $district ? $district->district_name : null;
|
||||
|
||||
// Mengambil data untuk dropdown
|
||||
$dropdownOptions = [
|
||||
'village_name' => DB::table('villages')->orderBy('village_name')->pluck('village_name', 'village_code'),
|
||||
'district_name' => DB::table('districts')->orderBy('district_name')->pluck('district_name', 'district_code'),
|
||||
];
|
||||
|
||||
info("AdvertisementController@edit diakses dengan Model Instance: $modelInstance");
|
||||
$fields = $this->getFields();
|
||||
$fieldTypes = $this->getFieldTypes();
|
||||
|
||||
$apiUrl = url('/api/advertisements');
|
||||
|
||||
// $route = 'advertisements.update'; // Menggunakan route update untuk form edit
|
||||
// info("AdvertisementController@edit diakses dengan route: $route");
|
||||
return view('form-create-update.form', compact('title', 'subtitle', 'modelInstance', 'fields', 'fieldTypes', 'apiUrl', 'dropdownOptions'));
|
||||
}
|
||||
|
||||
private function getFields()
|
||||
{
|
||||
return [
|
||||
"no" => "No",
|
||||
"business_name" => "Nama Wajib Pajak",
|
||||
"npwpd" => "NPWPD",
|
||||
"advertisement_type" => "Jenis Reklame",
|
||||
"advertisement_content" => "Isi Reklame",
|
||||
"business_address" => "Alamat Wajib Pajak",
|
||||
"advertisement_location" => "Lokasi Reklame",
|
||||
"village_name" => "Desa",
|
||||
"district_name" => "Kecamatan",
|
||||
"length" => "Panjang",
|
||||
"width" => "Lebar",
|
||||
"viewing_angle" => "Sudut Pandang",
|
||||
"face" => "Muka",
|
||||
"area" => "Luas",
|
||||
"angle" => "Sudut",
|
||||
"contact" => "Kontak",
|
||||
];
|
||||
}
|
||||
|
||||
private function getFieldTypes()
|
||||
{
|
||||
return [
|
||||
"no" => "text",
|
||||
"business_name" => "text",
|
||||
"npwpd" => "text",
|
||||
"advertisement_type" => "text",
|
||||
"advertisement_content" => "textarea",
|
||||
"business_address" => "text",
|
||||
"advertisement_location" => "text",
|
||||
"village_name" => "combobox",
|
||||
"district_name" => "combobox",
|
||||
"length" => "text",
|
||||
"width" => "text",
|
||||
"viewing_angle" => "text",
|
||||
"face" => "text",
|
||||
"area" => "text",
|
||||
"angle" => "text",
|
||||
"contact" => "text",
|
||||
];
|
||||
}
|
||||
}
|
||||
43
app/Http/Requests/AdvertisementRequest.php
Normal file
43
app/Http/Requests/AdvertisementRequest.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class AdvertisementRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'no' => 'required',
|
||||
'business_name' => 'required|string',
|
||||
'npwpd' => 'required|string',
|
||||
'advertisement_type' => 'required|string',
|
||||
'advertisement_content' => 'required|string',
|
||||
'business_address' => 'required|string',
|
||||
'advertisement_location' => 'required|string',
|
||||
'village_name' => 'required',
|
||||
'district_name' => 'required',
|
||||
'length' => 'required',
|
||||
'width' => 'required',
|
||||
'viewing_angle' => 'required|string',
|
||||
'face' => 'required|string',
|
||||
'area' => 'required|string',
|
||||
'angle' => 'required|string',
|
||||
'contact' => 'required|string',
|
||||
];
|
||||
}
|
||||
}
|
||||
19
app/Http/Resources/AdvertisementResource.php
Normal file
19
app/Http/Resources/AdvertisementResource.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class AdvertisementResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return parent::toArray($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user