213 lines
8.2 KiB
PHP
213 lines
8.2 KiB
PHP
<?php
|
|
namespace App\Controllers\Test;
|
|
|
|
use App\Traits\ResponseTrait;
|
|
use App\Controllers\BaseController;
|
|
use App\Libraries\ValueSet;
|
|
use App\Models\Test\TestMapModel;
|
|
use App\Models\Test\TestMapDetailModel;
|
|
|
|
class TestMapController extends BaseController {
|
|
use ResponseTrait;
|
|
|
|
protected $db;
|
|
protected $rules;
|
|
protected $model;
|
|
protected $modelDetail;
|
|
|
|
public function __construct() {
|
|
$this->db = \Config\Database::connect();
|
|
$this->model = new TestMapModel;
|
|
$this->modelDetail = new TestMapDetailModel;
|
|
$this->rules = [
|
|
'TestCode' => 'required',
|
|
'HostID' => 'required|integer',
|
|
'ClientID' => 'required|integer',
|
|
];
|
|
}
|
|
|
|
public function index() {
|
|
$rows = $this->model->getUniqueGroupings();
|
|
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)->where('EndDate IS NULL')->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];
|
|
|
|
// Include testmapdetail records
|
|
$row['details'] = $this->modelDetail->getDetailsByTestMap($id);
|
|
|
|
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->db->transStart();
|
|
|
|
// Soft delete the testmap
|
|
$this->model->update($id, ['EndDate' => date('Y-m-d H:i:s')]);
|
|
|
|
// Soft delete all related details
|
|
$this->modelDetail->where('TestMapID', $id)
|
|
->where('EndDate IS NULL')
|
|
->set('EndDate', date('Y-m-d H:i:s'))
|
|
->update();
|
|
|
|
$this->db->transComplete();
|
|
|
|
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 showByTestCode($testCode = null) {
|
|
if (!$testCode) { return $this->failValidationErrors('TestCode is required.'); }
|
|
|
|
$rows = $this->model->getMappingsByTestCode($testCode);
|
|
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 batchCreate() {
|
|
$items = $this->request->getJSON(true);
|
|
if (!is_array($items)) { return $this->failValidationErrors('Expected array of items'); }
|
|
|
|
$results = ['success' => [], 'failed' => []];
|
|
$this->db->transStart();
|
|
|
|
foreach ($items as $index => $item) {
|
|
if (!$this->validateData($item, $this->rules)) {
|
|
$results['failed'][] = ['index' => $index, 'errors' => $this->validator->getErrors()];
|
|
continue;
|
|
}
|
|
try {
|
|
$id = $this->model->insert($item);
|
|
$results['success'][] = ['index' => $index, 'TestMapID' => $id];
|
|
} catch (\Exception $e) {
|
|
$results['failed'][] = ['index' => $index, 'error' => $e->getMessage()];
|
|
}
|
|
}
|
|
|
|
$this->db->transComplete();
|
|
return $this->respond([
|
|
'status' => empty($results['failed']) ? 'success' : 'partial',
|
|
'message' => 'Batch create completed',
|
|
'data' => $results
|
|
], 200);
|
|
}
|
|
|
|
public function batchUpdate() {
|
|
$items = $this->request->getJSON(true);
|
|
if (!is_array($items)) { return $this->failValidationErrors('Expected array of items'); }
|
|
|
|
$results = ['success' => [], 'failed' => []];
|
|
$this->db->transStart();
|
|
|
|
foreach ($items as $index => $item) {
|
|
$id = $item['TestMapID'] ?? null;
|
|
if (!$id) {
|
|
$results['failed'][] = ['index' => $index, 'error' => 'TestMapID required'];
|
|
continue;
|
|
}
|
|
if (!$this->validateData($item, $this->rules)) {
|
|
$results['failed'][] = ['index' => $index, 'errors' => $this->validator->getErrors()];
|
|
continue;
|
|
}
|
|
try {
|
|
$this->model->update($id, $item);
|
|
$results['success'][] = ['index' => $index, 'TestMapID' => $id];
|
|
} catch (\Exception $e) {
|
|
$results['failed'][] = ['index' => $index, 'error' => $e->getMessage()];
|
|
}
|
|
}
|
|
|
|
$this->db->transComplete();
|
|
return $this->respond([
|
|
'status' => empty($results['failed']) ? 'success' : 'partial',
|
|
'message' => 'Batch update completed',
|
|
'data' => $results
|
|
], 200);
|
|
}
|
|
|
|
public function batchDelete() {
|
|
$ids = $this->request->getJSON(true);
|
|
if (!is_array($ids)) { return $this->failValidationErrors('Expected array of TestMapIDs'); }
|
|
|
|
$results = ['success' => [], 'failed' => []];
|
|
$this->db->transStart();
|
|
|
|
foreach ($ids as $id) {
|
|
try {
|
|
$row = $this->model->where('TestMapID', $id)->where('EndDate IS NULL')->first();
|
|
if (empty($row)) {
|
|
$results['failed'][] = ['TestMapID' => $id, 'error' => 'Not found or already deleted'];
|
|
continue;
|
|
}
|
|
$this->model->update($id, ['EndDate' => date('Y-m-d H:i:s')]);
|
|
$results['success'][] = $id;
|
|
} catch (\Exception $e) {
|
|
$results['failed'][] = ['TestMapID' => $id, 'error' => $e->getMessage()];
|
|
}
|
|
}
|
|
|
|
$this->db->transComplete();
|
|
return $this->respond([
|
|
'status' => empty($results['failed']) ? 'success' : 'partial',
|
|
'message' => 'Batch delete completed',
|
|
'data' => $results
|
|
], 200);
|
|
}
|
|
|
|
}
|