get('/entries/:account_id/:year/:month', 'get_entries'); $app->get('/accounts', 'get_accounts'); $app->get('/accounts/:account_id/months', 'get_months'); $app->delete('/entries/:id', 'remove_entry'); $app->post('/entries/add', 'add_entry'); $app->put('/entries/save/:id', 'save_entry'); $app->run(); function get_connection() { $db=new PDO("pgsql:host=localhost;dbname=accountant", "accountant", "accountant"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $db; } // Return the entries function get_entries($account_id, $year, $month) { $day=$year."-".$month."-01"; $connection=get_connection(); $sql = <<prepare($sql); $statement->bindParam("day", $day); $statement->bindParam("account_id", $account_id); $return=$statement->execute(); echo(json_encode($statement->fetchAll(PDO::FETCH_ASSOC))); } // Add an entry function add_entry() { $request = \Slim\Slim::getInstance()->request(); $entry = json_decode($request->getBody(), true); $connection=get_connection(); $statement=$connection->prepare("insert into entry (value_date, operation_date, label, value, account_id, category) values (:value_date, :operation_date, :label, :value, :account_id, :category)"); $statement->bindParam("value_date", $entry['value_date']); $statement->bindParam("operation_date", array_key_exists("operation_date", $entry) ? $entry['operation_date'] : null); $statement->bindParam("label", $entry['label']); $statement->bindParam("value", $entry['value']); $statement->bindParam("account_id", $entry['account_id']); $statement->bindParam("category", array_key_exists("category", $entry) ? $entry["category"] : null); $return=$statement->execute(); echo("Entry saved."); } // Saves an entry function save_entry($id) { $request = \Slim\Slim::getInstance()->request(); $entry = json_decode($request->getBody(), true); $connection=get_connection(); $statement=$connection->prepare("update entry set value_date=:value_date, operation_date=:operation_date, label=:label, value=:value, account_id=:account_id, category=:category where id=:id"); $statement->bindParam("value_date", $entry['value_date']); $statement->bindParam("operation_date", array_key_exists("operation_date", $entry) ? $entry['operation_date'] : null); $statement->bindParam("label", $entry['label']); $statement->bindParam("value", $entry['value']); $statement->bindParam("account_id", $entry['account_id']); $statement->bindParam("id", $entry['id']); $statement->bindParam("category", array_key_exists("category", $entry) ? $entry["category"] : null); $return=$statement->execute(); echo($entry['id'] . " saved."); } // Remove an entry function remove_entry($id) { $connection=get_connection(); $statement=$connection->prepare("delete from entry where id=:id"); $statement->bindParam("id", $id); $return=$statement->execute(); echo("Entry #" . $id . " removed."); } // Return the accounts with their solds. function get_accounts() { $connection=get_connection(); $sql = <<prepare($sql); $return=$statement->execute(); echo(json_encode($statement->fetchAll(PDO::FETCH_ASSOC))); } // Returns the months for an account. function get_months($account_id) { $connection=get_connection(); $sql = <<prepare($sql); $statement->bindParam("account_id", $account_id); $return=$statement->execute(); echo(json_encode($statement->fetchAll(PDO::FETCH_ASSOC))); } ?>