clqms-be/app/Controllers/Test/TestMapController.php
mahdahar 707d548db0 feat: implement test mapping functionality with TestMapController and model
- Add TestMapController for managing test-to-analyzer mappings
- Create TestMapModel with database operations for test mappings
- Update TestsController to support test mapping operations
- Add testmap API routes to Routes.php
- Create migration updates for test definitions table
- Add OpenAPI specification for test mapping endpoints
- Include unit tests for TestDef models
- Update bundled API documentation
2026-02-23 16:49:39 +07:00

128 lines
5.3 KiB
PHP

<?php
namespace App\Controllers\Test;
use App\Traits\ResponseTrait;
use App\Controllers\BaseController;
use App\Libraries\ValueSet;
use App\Models\Test\TestMapModel;
class TestMapController extends BaseController {
use ResponseTrait;
protected $db;
protected $rules;
protected $model;
public function __construct() {
$this->db = \Config\Database::connect();
$this->model = new TestMapModel;
}
public function index() {
$rows = $this->model->findAll();
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 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);
}
public function create() {
$input = $this->request->getJSON(true);
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
try {
$id = $this->model->insert($input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data created successfully", 'data' => $id ]);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function update() {
$input = $this->request->getJSON(true);
$id = $input["TestMapID"];
if (!$id) { return $this->failValidationErrors('TestMapID is required.'); }
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors( $this->validator->getErrors() ); }
try {
$this->model->update($id,$input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data updated successfully", 'data' => $id ]);
} 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);
}
}