175 lines
6.3 KiB
PHP
175 lines
6.3 KiB
PHP
<?php
|
|
namespace App\Controllers;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
use App\Controllers\BaseController;
|
|
|
|
class Tests extends BaseController {
|
|
use ResponseTrait;
|
|
|
|
|
|
protected $db;
|
|
protected $rules;
|
|
protected $model;
|
|
protected $modelCal;
|
|
protected $modelTech;
|
|
protected $modelGrp;
|
|
protected $modelValueSet;
|
|
|
|
public function __construct() {
|
|
$this->db = \Config\Database::connect();
|
|
$this->model = new \App\Models\Test\TestDefSiteModel;
|
|
$this->modelCal = new \App\Models\Test\TestDefCalModel;
|
|
$this->modelTech = new \App\Models\Test\TestDefTechModel;
|
|
$this->modelGrp = new \App\Models\Test\TestGrpModel;
|
|
$this->modelValueSet = new \App\Models\ValueSet\ValueSetModel;
|
|
}
|
|
|
|
public function index() {
|
|
$rows = $this->model->getTests();
|
|
if (empty($rows)) { return $this->failNotFound('Data not found'); }
|
|
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
|
|
}
|
|
|
|
public function show($id = null) {
|
|
$rows = $this->model->getTest($id);
|
|
if (empty($rows)) { return $this->failNotFound('Data not found'); }
|
|
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)) {
|
|
// If rules are empty, validateData returns true (or false depending on CI version, but here we assume checks passed if no rules)
|
|
// If you have rules, uncomment the line below
|
|
// return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$this->db->transStart();
|
|
|
|
try {
|
|
// 1. Insert into Main Table
|
|
$id = $this->model->insert($input);
|
|
$input['TestSiteID'] = $id;
|
|
|
|
// 2. Determine Type
|
|
$testTypeID = $input['TestType'] ?? null;
|
|
if ($testTypeID) {
|
|
// Fetch TypeCode to decide where to insert
|
|
$vs = $this->modelValueSet->find($testTypeID);
|
|
$typeCode = $vs['VValue'] ?? '';
|
|
|
|
if ($typeCode === 'Calculated') {
|
|
$this->modelCal->insert($input);
|
|
} elseif ($typeCode === 'GROUP') {
|
|
if (isset($input['Members']) && is_array($input['Members'])) {
|
|
foreach ($input['Members'] as $memberID) {
|
|
$this->modelGrp->insert([
|
|
'TestSiteID' => $id,
|
|
'Member' => $memberID
|
|
]);
|
|
}
|
|
}
|
|
} else {
|
|
// Default to Technical for others
|
|
$this->modelTech->insert($input);
|
|
}
|
|
}
|
|
|
|
$this->db->transComplete();
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
return $this->failServerError('Transaction failed');
|
|
}
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => "data created successfully", 'data'=> $id ]);
|
|
} catch (\Exception $e) {
|
|
$this->db->transRollback();
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function update($id = null) {
|
|
$input = $this->request->getJSON(true);
|
|
// Sometimes ID comes in payload, sometimes in URL.
|
|
// Route typically is PUT /tests/(:num) -> update($1)
|
|
// If $id is null, check input
|
|
if (!$id && isset($input["TestID"])) { $id = $input["TestID"]; } // TestDefSite uses TestSiteID as PK but getting as TestID in other code maybe? Model says TestSiteID.
|
|
// Let's use TestSiteID to be consistent with model
|
|
if (!$id && isset($input["TestSiteID"])) { $id = $input["TestSiteID"]; }
|
|
|
|
if (!$id) { return $this->failValidationErrors('TestSiteID is required.'); }
|
|
|
|
$this->db->transStart();
|
|
|
|
try {
|
|
// 1. Update Main Table
|
|
$this->model->update($id, $input);
|
|
|
|
// 2. Determine Type (we probably need to fetch the existing record to know the type if it's not in payload,
|
|
// but if we are updating, we might be changing type? Unlikely.
|
|
// Let's assume Type is in input OR we fetch it.)
|
|
|
|
$testTypeID = $input['TestType'] ?? null;
|
|
|
|
// If not in input, fetch from DB
|
|
if (!$testTypeID) {
|
|
$existing = $this->model->find($id);
|
|
$testTypeID = $existing['TestType'] ?? null;
|
|
}
|
|
|
|
if ($testTypeID) {
|
|
$vs = $this->modelValueSet->find($testTypeID);
|
|
$typeCode = $vs['VValue'] ?? '';
|
|
|
|
// For update, we need to locate the record in sub-table.
|
|
// Sub-tables have their own PKs (TestCalID, TestTechID) but also foreign key TestSiteID.
|
|
// We should update based on TestSiteID.
|
|
|
|
if ($typeCode === 'Calculated') {
|
|
// Check if exists
|
|
$exists = $this->modelCal->where('TestSiteID', $id)->first();
|
|
if ($exists) {
|
|
$this->modelCal->update($exists['TestCalID'], $input);
|
|
} else {
|
|
// Create if missing?
|
|
$input['TestSiteID'] = $id;
|
|
$this->modelCal->insert($input);
|
|
}
|
|
} elseif ($typeCode === 'GROUP') {
|
|
if (isset($input['Members']) && is_array($input['Members'])) {
|
|
// Delete existing members for this group to avoid duplicates or stale data
|
|
// If we had a primary key on the relation we could be smarter, but full replace is safer here
|
|
$this->modelGrp->where('TestSiteID', $id)->delete();
|
|
|
|
foreach ($input['Members'] as $memberID) {
|
|
$this->modelGrp->insert([
|
|
'TestSiteID' => $id,
|
|
'Member' => $memberID
|
|
]);
|
|
}
|
|
}
|
|
} else {
|
|
$exists = $this->modelTech->where('TestSiteID', $id)->first();
|
|
if ($exists) {
|
|
$this->modelTech->update($exists['TestTechID'], $input);
|
|
} else {
|
|
$input['TestSiteID'] = $id;
|
|
$this->modelTech->insert($input);
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->db->transComplete();
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
return $this->failServerError('Transaction failed');
|
|
}
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => "data updated successfully", 'data'=> $id ]);
|
|
} catch (\Exception $e) {
|
|
$this->db->transRollback();
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
} |