stock = new Stock(); $this->db = Config::connect(); } public function index() { try { $data = $this->stock->findAll(); if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200); return $this->respond(['status' => 200, 'message' => 'No stock records found'], 200); } catch (\Exception $e) { return $this->respond(['status' => 500, 'message' => 'Server error'], 500); } } public function show($id = null) { try { $item = $this->stock->find($id); if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200); return $this->respond(['status' => 404, 'message' => 'Stock not found'], 404); } catch (\Exception $e) { return $this->respond(['status' => 500, 'message' => 'Server error'], 500); } } public function create() { try { $payload = $this->request->getJSON(true); $id = $this->stock->insert($payload); if ($id) return $this->respond(['status' => 201, 'message' => 'Stock created', 'result' => ['id' => $id]], 201); return $this->respond(['status' => 400, 'message' => 'Failed to create Stock'], 400); } catch (\Exception $e) { return $this->respond(['status' => 500, 'message' => 'Server error'], 500); } } public function update($id = null) { try { if (!$this->stock->find($id)) return $this->respond(['status' => 404, 'message' => 'Stock not found'], 404); $payload = $this->request->getJSON(true); if ($this->stock->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Stock updated'], 200); return $this->respond(['status' => 400, 'message' => 'Failed to update Stock'], 400); } catch (\Exception $e) { return $this->respond(['status' => 500, 'message' => 'Server error'], 500); } } public function delete($id = null) { try { if (!$this->stock->find($id)) return $this->respond(['status' => 404, 'message' => 'Stock not found'], 404); if ($this->stock->delete($id)) return $this->respond(['status' => 200, 'message' => 'Stock deleted'], 200); return $this->respond(['status' => 400, 'message' => 'Failed to delete Stock'], 400); } catch (\Exception $e) { return $this->respond(['status' => 500, 'message' => 'Server error'], 500); } } }