Viewing File: /home/assersoft/public_html/doctor-assistant/app/Controllers/TreatmentsController.php
<?php
namespace App\Controllers;
use App\Models\PatientsModel;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
class TreatmentsController extends ResourceController
{
protected $modelName = "App\Models\TreatmentsModel";
protected $format = "json";
/**
* Return an array of resource objects, themselves in array format.
*
* @return ResponseInterface
*/
public function index()
{
$rules = [
'clinic_id' => 'required|numeric',
'patient_id' => 'required|numeric',
];
if(!$this->validate($rules)){
return $this->fail($this->validator->getErrors());
}
$clinic_id = $this->request->getVar('clinic_id');
$patient_id = $this->request->getVar('patient_id');
$patientModel = new PatientsModel();
$patient = $patientModel->find($patient_id);
if($patient == null){
return $this->failNotFound("Patient not found");
}
$treatments = $this->model->where('clinic_id', $clinic_id)->where('patient_id', $patient_id)->findAll();
return $this->respond($treatments);
}
/**
* Return the properties of a resource object.
*
* @param int|string|null $id
*
* @return ResponseInterface
*/
public function show($id = null)
{
$rules = [
'clinic_id' => 'required|numeric',
'patient_id' => 'required|numeric',
];
if(!$this->validate($rules)){
return $this->fail($this->validator->getErrors());
}
$clinic_id = $this->request->getVar('clinic_id');
$treatment = $this->model->where('clinic_id', $clinic_id)->find($id);
if($treatment == null){
return $this->failNotFound("Treatment not found");
}
return $this->respond($treatment);
}
/**
* Create a new resource object, from "posted" parameters.
*
* @return ResponseInterface
*/
public function create()
{
$rules = [
'clinic_id' => 'required|numeric',
'patient_id' => 'required|numeric',
'treatment_start_date' => 'required|valid_date[Y-m-d]',
'treatment_end_date' => 'required|valid_date[Y-m-d]',
'treatment_name' => 'required|string',
'treatment_cost' => 'required|numeric',
'paid_amount' => 'permit_empty|numeric',
];
if(!$this->validate($rules)){
return $this->fail($this->validator->getErrors());
}
$data = $this->request->getVar();
$data->treatment_status = 'in-progress';
$data->paid_amount = $data->paid_amount ?? 0;
$data->due_amount = $data->treatment_cost - $data->paid_amount;
$treatment = $this->model->insert($data);
if(!$treatment){
return $this->fail("Failed to create treatment");
}
return $this->respondCreated(['treatment_id' => $this->model->getInsertID()]);
}
/**
* Add or update a model resource, from "posted" properties.
*
* @param int|string|null $id
*
* @return ResponseInterface
*/
public function update($id = null)
{
$rules = [
'treatment_start_date' => 'permit_empty|valid_date',
'treatment_end_date' => 'permit_empty|valid_date',
'treatment_name' => 'permit_empty|string',
'treatment_cost' => 'permit_empty|numeric',
'paid_amount' => 'permit_empty|numeric',
];
if(!$this->validate($rules)){
return $this->fail($this->validator->getErrors());
}
$data = $this->request->getVar();
if(property_exists($data, 'treatment_cost') && property_exists($data, 'paid_amount')){
$data->due_amount = $data->treatment_cost - $data->paid_amount;
}
$treatment = $this->model->update($id, $data);
if(!$treatment){
return $this->fail("Failed to update treatment");
}
return $this->respondUpdated(['treatment_id' => $id]);
}
/**
* Delete the designated resource object from the model.
*
* @param int|string|null $id
*
* @return ResponseInterface
*/
public function delete($id = null)
{
$rules = [
'clinic_id' => 'required|numeric',
'patient_id' => 'required|numeric'
];
if(!$this->validate($rules)){
return $this->fail($this->validator->getErrors());
}
$clinic_id = $this->request->getVar('clinic_id');
$patient_id = $this->request->getVar('patient_id');
$treatment = $this->model->where('clinic_id', $clinic_id)->where('patient_id', $patient_id)->find($id);
if($treatment == null){
return $this->failNotFound("Treatment not found");
}
$deleted = $this->model->delete($id);
if(!$deleted){
return $this->fail("Failed to delete treatment");
}
return $this->respondDeleted(['treatment_id' => $id]);
}
}
Back to Directory
File Manager