clqms-be/app/Controllers/Specimen/ContainerDef.php

65 lines
2.3 KiB
PHP
Raw Normal View History

2025-10-14 10:48:20 +07:00
<?php
namespace App\Controllers\Specimen;
use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
use App\Models\Specimen\ContainerDefModel;
class ContainerDef extends BaseController {
use ResponseTrait;
protected $db;
protected $model;
protected $rules;
public function __construct() {
$this->db = \Config\Database::connect();
$this->model = new ContainerDefModel();
$this->rules = [
'ConCode' => 'required|max_length[50]',
'ConName' => 'required|max_length[50]'
];
}
public function index() {
try {
$rows = $this->model->findAll();
return $this->respond([ 'status' => 'success', 'message'=> "data fetched successfully", 'data' => $rows ], 200);
} catch (\Exception $e) {
return $this->failServerError('Exception : '.$e->getMessage());
}
}
public function show($ConDefID) {
try {
2025-10-15 13:09:00 +07:00
$rows = $this->model->getContainer($ConDefID);
2025-10-14 10:48:20 +07:00
return $this->respond([ 'status' => 'success', 'message'=> "data fetched successfully", 'data' => $rows ], 200);
} catch (\Exception $e) {
return $this->failServerError('Exception : '.$e->getMessage());
}
}
public function create() {
$input = $this->request->getJSON(true);
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
try {
$ConDefID = $this->model->insert($input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data $ConDefID created successfully" ]);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function update() {
$input = $this->request->getJSON(true);
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
try {
$ConDefID = $this->model->update($input['ConDefID'], $input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data $ConDefID updated successfully" ]);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
}