tstat_be/app/Controllers/TravellerController.php

148 lines
4.0 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use App\Models\TravellerModel;
class TravellerController extends ResourceController
{
protected $format = 'json';
protected $myLogger;
protected $travellerModel;
public function __construct()
{
$this->myLogger = \Config\Services::mylogger();
$this->travellerModel = new TravellerModel();
}
// Get all travellers
public function index()
{
$orgId = $this->request->getGet('org_id');
$for = $this->request->getVar('for');
if (isset($for) && $for === 'table_view')
{
$users = $this->travellerModel->where('org_id',$orgId)->findAll();
} else {
$users = $this->travellerModel->where('org_id',$orgId)->where('is_active',1)->findAll();
}
if (!$users) {
return $this->failNotFound('No Traveller found');
}
return $this->respond([
'status' => 200,
'message' => 'Travellers retrieved successfully',
'data' => $users
], 200);
}
// Create Traveller
public function create()
{
$data = $this->request->getJSON(true);
if ($this->travellerModel->insert($data)) {
$travellerId = $this->travellerModel->insertID(); // Get the last inserted ID
$traveller = $this->travellerModel->find($travellerId); // Fetch the inserted record
return $this->respond([
'status' => 200,
'message' => 'Traveller added successfully',
'data' => $traveller // Returning inserted traveller details
], 200);
} else {
return $this->failServerError('Failed to add traveller');
}
}
//Find Traveller
public function find()
{
$id = $this->request->getGet('traveller_id');
$data = $this->travellerModel->where('traveller_id',$id)->find();
if (!$data) {
return $this->failNotFound('No data found');
}
return $this->respond(['status' => 200,'message' => 'success','data' => $data]);
}
// Update Traveller
public function update($id = null)
{
if (!$id) {
return $this->failNotFound('Traveller ID is required');
}
$data = $this->request->getJSON(true);
if ($this->travellerModel->update($id, $data)) {
return $this->respond([
'status' => 200,
'message' => 'Traveller updated successfully'
], 200);
} else {
return $this->failNotFound('Traveller not found or update failed');
}
}
// Find by Name (LIKE Search)
public function findByName($name)
{
$travellers = $this->travellerModel->like('name', $name)->findAll();
if ($travellers) {
return $this->respond([
'status' => 200,
'message' => 'Traveller(s) found',
'data' => $travellers
], 200);
} else {
return $this->failNotFound('No traveller found with this name');
}
}
// Find by Email
public function findByEmail($email)
{
$traveller = $this->travellerModel->where('email', $email)->first();
if ($traveller) {
return $this->respond([
'status' => 200,
'message' => 'Traveller found',
'data' => $traveller
], 200);
} else {
return $this->failNotFound('No traveller found with this email');
}
}
// Find by Mobile
public function findByMobile($mobile)
{
$traveller = $this->travellerModel->where('mobile', $mobile)->first();
if ($traveller) {
return $this->respond([
'status' => 200,
'message' => 'Traveller found',
'data' => $traveller
], 200);
} else {
return $this->failNotFound('No traveller found with this mobile number');
}
}
}