Viewing File: /home/assersoft/public_html/doctor-assistant/app/Controllers/PatientsController.php

<?php

namespace App\Controllers;

use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;

class PatientsController extends ResourceController
{

    protected $modelName = 'App\Models\PatientsModel';
    protected $format    = 'json';

    /**
     * Return an array of resource objects, themselves in array format.
     *
     * @return ResponseInterface
     */
    public function index()
    {
        $rules = [
            'clinic_id' => 'required|numeric',
            'perPage' => 'numeric|permit_empty',
            'page' => 'numeric|permit_empty',
        ];

        if(!$this->validate($rules)){
            return $this->fail($this->validator->getErrors());
        }

        $page = $this->request->getVar('page') ?? 1;
        $perPage = $this->request->getVar('perPage') ?? 10;

        $data = $this->model->where('clinic_id', $this->request->getVar('clinic_id'))->orderBy('patient_id', 'asc')->paginate($perPage, 'default', $page);

        $pager = service('pager');
        $pagination = [
            'current_page' => $page,
            'perPage' => $perPage,
            'total_pages' => $pager->getPageCount(),
            'total_items' => $pager->getTotal('default'),
        ];

        return $this->respond([
            'data' => $data,
            'pagination' => $pagination
        ]);
    }

    /**
     * Return the properties of a resource object.
     *
     * @param int|string|null $id
     *
     * @return ResponseInterface
     */
    public function show($id = null)
    {
        $data = $this->model->find($id);
        if(!$data) {
            return $this->failNotFound('No data found');
        }

        return $this->respond($data);
    }

    /**
     * Create a new resource object, from "posted" parameters.
     *
     * @return ResponseInterface
     */
    public function create()
    {
        
        $rules = [
            'patient_name' => 'required|min_length[3]|max_length[50]',
            'phone' => 'required|is_unique[Patients.phone]',
            'clinic_id' => 'required|numeric',
        ];

        if(!$this->validate($rules)){
            return $this->fail($this->validator->getErrors());
        }

        $data = $this->request->getVar();

        if(!$this->model->insert($data)) {
            return $this->fail($this->model->errors());
        }

        return $this->respondCreated(['patient_id' => $this->model->getInsertID(), 'message' => 'Patient created successfully']);
    }

    /**
     * Add or update a model resource, from "posted" properties.
     *
     * @param int|string|null $id
     *
     * @return ResponseInterface
     */
    public function update($id = null)
    {
        $rules = [
            'patient_name' => 'permit_empty|min_length[3]|max_length[50]',
            'phone' => 'permit_empty|is_unique[Patients.phone,phone,'.$id.',patient_id]',
        ];

        if(!$this->validate($rules)){
            return $this->fail($this->validator->getErrors());
        }

        $data = $this->request->getVar();

        if(!$this->model->update($id, $data)) {
            return $this->fail($this->model->errors());
        }

        return $this->respondUpdated(['patient_id' => $id]);
    }

    /**
     * Delete the designated resource object from the model.
     *
     * @param int|string|null $id
     *
     * @return ResponseInterface
     */
    public function delete($id = null)
    {
        $data = $this->model->find($id);
        if(!$data) {
            return $this->failNotFound('No data found');
        }

        if(!$this->model->delete($id)) {
            return $this->fail($this->model->errors());
        }

        return $this->respondDeleted(['patient_id' => $id]);
    }

    public function search() {
        $rules = [
            'clinic_id' => 'required|numeric'
        ];

        if(!$this->validate($rules)){
            return $this->fail($this->validator->getErrors());
        }

        $phone = $this->request->getVar('phone');
        $data = $this->model->where('clinic_id', $this->request->getVar('clinic_id'))->where('phone', $phone)->first();
        return $this->respond($data);
    }
}
Back to Directory File Manager