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

128 lines
5.3 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 App\Traits\ResponseTrait;
2025-10-24 16:41:31 +07:00
use App\Controllers\BaseController;
use App\Libraries\ValueSet;
2025-11-10 16:02:52 +07:00
use App\Models\Test\TestMapModel;
2025-10-24 16:41:31 +07:00
class TestMapController 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 TestMapModel;
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); }
$rows = ValueSet::transformLabels($rows, [
'HostType' => 'entity_type',
'ClientType' => 'entity_type',
]);
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
2025-10-24 16:41:31 +07:00
}
2025-11-10 16:02:52 +07:00
public function show($id = null) {
$row = $this->model->where('TestMapID',$id)->first();
if (empty($row)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200); }
$row = ValueSet::transformLabels([$row], [
'HostType' => 'entity_type',
'ClientType' => 'entity_type',
])[0];
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $row ], 200);
2025-10-24 16:41:31 +07:00
}
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);
2025-11-19 15:35:32 +07:00
return $this->respondCreated([ 'status' => 'success', 'message' => "data created successfully", 'data' => $id ]);
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["TestMapID"];
if (!$id) { return $this->failValidationErrors('TestMapID 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);
2025-11-19 15:35:32 +07:00
return $this->respondCreated([ 'status' => 'success', 'message' => "data updated successfully", 'data' => $id ]);
2025-10-24 16:41:31 +07:00
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function delete() {
$input = $this->request->getJSON(true);
$id = $input["TestMapID"] ?? null;
if (!$id) { return $this->failValidationErrors('TestMapID is required.'); }
try {
$row = $this->model->where('TestMapID', $id)->where('EndDate IS NULL')->first();
if (empty($row)) { return $this->respond([ 'status' => 'failed', 'message' => "Data not found or already deleted.", 'data' => null ], 404); }
$this->model->update($id, ['EndDate' => date('Y-m-d H:i:s')]);
return $this->respond([ 'status' => 'success', 'message' => "data deleted successfully", 'data' => $id ], 200);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function showByTestSite($testSiteID = null) {
if (!$testSiteID) { return $this->failValidationErrors('TestSiteID is required.'); }
$rows = $this->model->getMappingsByTestSite($testSiteID);
if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); }
$rows = ValueSet::transformLabels($rows, [
'HostType' => 'entity_type',
'ClientType' => 'entity_type',
]);
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
}
public function showByHost($hostType = null, $hostID = null) {
if (!$hostType || !$hostID) { return $this->failValidationErrors('HostType and HostID are required.'); }
$rows = $this->model->getMappingsByHost($hostType, $hostID);
if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); }
$rows = ValueSet::transformLabels($rows, [
'HostType' => 'entity_type',
'ClientType' => 'entity_type',
]);
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
}
public function showByClient($clientType = null, $clientID = null) {
if (!$clientType || !$clientID) { return $this->failValidationErrors('ClientType and ClientID are required.'); }
$rows = $this->model->getMappingsByClient($clientType, $clientID);
if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); }
$rows = ValueSet::transformLabels($rows, [
'HostType' => 'entity_type',
'ClientType' => 'entity_type',
]);
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
}
}