clqms-be/app/Controllers/Test/TestDefSite.php

61 lines
2.4 KiB
PHP
Raw Normal View History

2025-10-24 16:41:31 +07:00
<?php
2025-11-10 16:02:52 +07:00
namespace App\Controllers\Test;
2025-10-24 16:41:31 +07:00
use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
2025-11-10 16:02:52 +07:00
use App\Models\Test\TestDefSiteModel;
2025-10-24 16:41:31 +07:00
2025-11-10 16:02:52 +07:00
class TestDefSite extends BaseController {
2025-10-24 16:41:31 +07:00
use ResponseTrait;
protected $db;
protected $rules;
protected $model;
public function __construct() {
$this->db = \Config\Database::connect();
2025-11-10 16:02:52 +07:00
$this->model = new TestDefSiteModel;
2025-11-19 13:38:46 +07:00
$this->rules = [
2025-11-19 14:26:32 +07:00
"TestSiteCode" => "required",
"TestSiteName" => "required",
"Type" => "required",
2025-11-19 13:38:46 +07:00
];
2025-10-24 16:41:31 +07:00
}
public function index() {
2025-11-10 16:02:52 +07:00
$rows = $this->model->findAll();
2025-10-24 16:41:31 +07:00
if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); }
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
}
2025-11-10 16:02:52 +07:00
public function show($id = null) {
$rows = $this->model->where('TestSiteID',$id)->findAll();
2025-10-24 16:41:31 +07:00
if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); }
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
}
public function create() {
$input = $this->request->getJSON(true);
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
try {
2025-11-10 16:02:52 +07:00
$id = $this->model->insert($input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data $id created successfully" ]);
2025-10-24 16:41:31 +07:00
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function update() {
$input = $this->request->getJSON(true);
2025-11-10 16:02:52 +07:00
$id = $input["TestSiteID"];
if (!$id) { return $this->failValidationErrors('TestSiteID is required.'); }
2025-10-24 16:41:31 +07:00
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors( $this->validator->getErrors() ); }
try {
2025-11-10 16:02:52 +07:00
$this->model->update($id,$input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data $id updated successfully" ]);
2025-10-24 16:41:31 +07:00
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
}