2025-09-10 16:54:33 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
2025-10-15 11:01:52 +07:00
|
|
|
use App\Controllers\BaseController;
|
|
|
|
|
use App\Models\Location\LocationModel;
|
2025-09-10 16:54:33 +07:00
|
|
|
|
2026-01-05 16:55:34 +07:00
|
|
|
class LocationController extends BaseController {
|
2025-09-10 16:54:33 +07:00
|
|
|
use ResponseTrait;
|
|
|
|
|
|
2025-10-15 11:01:52 +07:00
|
|
|
protected $model;
|
2025-09-26 16:33:54 +07:00
|
|
|
protected $rules;
|
|
|
|
|
|
2025-09-10 16:54:33 +07:00
|
|
|
public function __construct() {
|
2025-10-15 11:01:52 +07:00
|
|
|
$this->model = new LocationModel();
|
2025-09-16 15:33:22 +07:00
|
|
|
$this->rules = [
|
|
|
|
|
'LocCode' => 'required|max_length[6]',
|
|
|
|
|
'LocFull' => 'required',
|
|
|
|
|
];
|
2025-09-10 16:54:33 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index() {
|
2025-09-26 16:33:54 +07:00
|
|
|
$LocName = $this->request->getVar('LocName');
|
|
|
|
|
$LocCode = $this->request->getVar('LocCode');
|
2025-10-15 11:01:52 +07:00
|
|
|
$rows = $this->model->getLocations($LocCode,$LocName);
|
2025-09-11 11:09:04 +07:00
|
|
|
if (empty($rows)) {
|
2025-10-15 11:01:52 +07:00
|
|
|
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200);
|
2025-09-11 11:09:04 +07:00
|
|
|
}
|
|
|
|
|
|
2025-10-15 11:01:52 +07:00
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200);
|
2025-09-11 11:09:04 +07:00
|
|
|
}
|
|
|
|
|
|
2025-10-15 11:01:52 +07:00
|
|
|
public function show($LocationID = null) {
|
2025-12-29 12:55:31 +07:00
|
|
|
$row = $this->model->getLocation($LocationID);
|
|
|
|
|
if (empty($row)) {
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200);
|
2025-09-11 11:09:04 +07:00
|
|
|
}
|
|
|
|
|
|
2025-12-29 12:55:31 +07:00
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $row ], 200);
|
2025-09-11 11:09:04 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create() {
|
2025-09-22 15:22:07 +07:00
|
|
|
$input = $this->request->getJSON(true);
|
2025-10-15 11:01:52 +07:00
|
|
|
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
|
2025-09-22 15:22:07 +07:00
|
|
|
try {
|
2026-01-06 17:00:33 +07:00
|
|
|
$result = $this->model->saveLocation($input);
|
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $result ], 201);
|
2025-09-11 11:09:04 +07:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 15:33:22 +07:00
|
|
|
public function update() {
|
2025-10-15 11:01:52 +07:00
|
|
|
$input = $this->request->getJSON(true);
|
2025-09-11 11:09:04 +07:00
|
|
|
try {
|
2025-10-15 11:01:52 +07:00
|
|
|
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors( $this->validator->getErrors()); }
|
2026-01-06 17:00:33 +07:00
|
|
|
$result = $this->model->saveLocation($input, true);
|
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $result ], 201);
|
2025-09-11 11:09:04 +07:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 15:33:22 +07:00
|
|
|
public function delete() {
|
2025-10-15 11:01:52 +07:00
|
|
|
$input = $this->request->getJSON(true);
|
2025-09-16 15:33:22 +07:00
|
|
|
try {
|
|
|
|
|
$LocationID = $input["LocationID"];
|
2025-10-15 11:01:52 +07:00
|
|
|
$this->model->deleteLocation($LocationID);
|
|
|
|
|
return $this->respondDeleted([ 'status' => 'success', 'message' => "Location with {$LocationID} deleted successfully." ]);
|
2025-09-11 11:09:04 +07:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-05 16:55:34 +07:00
|
|
|
}
|