add crud for test
This commit is contained in:
parent
6b83f332ea
commit
600f672831
@ -3,54 +3,173 @@ namespace App\Controllers;
|
||||
|
||||
use CodeIgniter\API\ResponseTrait;
|
||||
use App\Controllers\BaseController;
|
||||
use App\Models\Test\TestDefSiteModel;
|
||||
|
||||
class Tests extends BaseController {
|
||||
use ResponseTrait;
|
||||
use ResponseTrait;
|
||||
|
||||
protected $db;
|
||||
protected $rules;
|
||||
protected $model;
|
||||
|
||||
public function __construct() {
|
||||
$this->db = \Config\Database::connect();
|
||||
$this->model = new TestDefSiteModel;
|
||||
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());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
$this->db->transStart();
|
||||
|
||||
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);
|
||||
}
|
||||
try {
|
||||
// 1. Insert into Main Table
|
||||
$id = $this->model->insert($input);
|
||||
$input['TestSiteID'] = $id;
|
||||
|
||||
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());
|
||||
// 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();
|
||||
|
||||
public function update() {
|
||||
$input = $this->request->getJSON(true);
|
||||
$id = $input["TestID"];
|
||||
if (!$id) { return $this->failValidationErrors('TestID 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());
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -24,11 +24,20 @@ class TestDefSiteModel extends BaseModel {
|
||||
}
|
||||
|
||||
public function getTest($TestSiteID) {
|
||||
$row = $this->join("valueset", "valueset.VID=testdefsite.TestType", "left")
|
||||
$row = $this->select("testdefsite.*, valueset.VValue as TypeCode, valueset.VDesc as TypeName")
|
||||
->join("valueset", "valueset.VID=testdefsite.TestType", "left")
|
||||
->where("testdefsite.TestSiteID", $TestSiteID)
|
||||
->find($TestSiteID);
|
||||
|
||||
$row['testdeftech'] = $this->db->query("select * from testdeftech where TestSiteID='$TestSiteID'")->getResultArray();
|
||||
if (!$row) return null;
|
||||
|
||||
if ($row['TypeCode'] == 'Calculated') {
|
||||
$row['testdefcal'] = $this->db->query("select * from testdefcal where TestSiteID='$TestSiteID'")->getResultArray();
|
||||
} elseif ($row['TypeCode'] == 'GROUP') {
|
||||
$row['testgrp'] = $this->db->query("select * from testgrp where TestSiteID='$TestSiteID'")->getResultArray();
|
||||
} else {
|
||||
$row['testdeftech'] = $this->db->query("select * from testdeftech where TestSiteID='$TestSiteID'")->getResultArray();
|
||||
}
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user