model = new LocationModel(); $this->rules = [ 'LocCode' => 'required|max_length[6]', 'LocFull' => 'required', ]; } public function index() { $LocName = $this->request->getVar('LocName'); $LocCode = $this->request->getVar('LocCode'); $rows = $this->model->getLocations($LocCode,$LocName); if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); } return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200); } public function show($LocationID = null) { $rows = $this->model->getLocation($LocationID); if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); } return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200); } public function create() { $input = $this->request->getJSON(true); if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); } try { $data = $this->model->saveLocation($input); return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $data['LocationID'] ], 201); } catch (\Throwable $e) { return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } public function update() { $input = $this->request->getJSON(true); try { if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors( $this->validator->getErrors()); } $this->model->saveLocation($input); return $this->respondCreated([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $input['LocationID'] ], 201); } catch (\Throwable $e) { return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } public function delete() { $input = $this->request->getJSON(true); try { $LocationID = $input["LocationID"]; $this->model->deleteLocation($LocationID); return $this->respondDeleted([ 'status' => 'success', 'message' => "Location with {$LocationID} deleted successfully." ]); } catch (\Throwable $e) { return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } }