clqms-be/app/Controllers/Location.php

75 lines
2.8 KiB
PHP
Raw Normal View History

2025-09-10 16:54:33 +07:00
<?php
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
use App\Models\Location\LocationModel;
2025-09-10 16:54:33 +07:00
class Location extends BaseController {
2025-09-10 16:54:33 +07:00
use ResponseTrait;
protected $model;
2025-09-26 16:33:54 +07:00
protected $rules;
2025-09-10 16:54:33 +07:00
public function __construct() {
$this->model = new LocationModel();
$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');
$rows = $this->model->getLocations($LocCode,$LocName);
2025-09-11 11:09:04 +07:00
if (empty($rows)) {
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200);
2025-09-11 11:09:04 +07:00
}
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200);
2025-09-11 11:09:04 +07:00
}
public function show($LocationID = null) {
$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
}
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);
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
2025-09-22 15:22:07 +07:00
try {
2025-12-01 16:47:52 +07:00
$id = $this->model->saveLocation($input);
return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $id ], 201);
2025-09-11 11:09:04 +07:00
} catch (\Throwable $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function update() {
$input = $this->request->getJSON(true);
2025-09-11 11:09:04 +07:00
try {
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors( $this->validator->getErrors()); }
2025-12-01 16:47:52 +07:00
$id = $this->model->saveLocation($input, true);
return $this->respondCreated([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $id ], 201);
2025-09-11 11:09:04 +07:00
} catch (\Throwable $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function delete() {
$input = $this->request->getJSON(true);
try {
$LocationID = $input["LocationID"];
$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());
}
}
2025-09-10 16:54:33 +07:00
}