diff --git a/src/html/api/index.php b/src/html/api/index.php index 5356d62..df01b10 100644 --- a/src/html/api/index.php +++ b/src/html/api/index.php @@ -4,16 +4,19 @@ require_once('Slim/Slim.php'); $app=new \Slim\Slim(); -$app->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->get('/entries/:account_id/:year/:month', 'getEntries'); +$app->get('/accounts', 'getAccounts'); +$app->get('/accounts/:account_id/months', 'getMonths'); +$app->delete('/entries/:id', 'removeEntry'); +$app->post('/entries/add', 'addEntry'); +$app->put('/entries/save/:id', 'saveEntry'); +$app->post('/accounts/add','addAccount'); +$app->put('/accounts/save/:id','saveAccount'); +$app->delete('/accounts/:id', 'removeAccount'); $app->run(); -function get_connection() { +function getConnection() { $db=new PDO("pgsql:host=localhost;dbname=accountant", "accountant", "accountant"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); @@ -21,14 +24,14 @@ function get_connection() { } // Return the entries -function get_entries($account_id, $year, $month) { +function getEntries($account_id, $year, $month) { $day=$year."-".$month."-01"; - $connection=get_connection(); + $connection=getConnection(); $sql = <<request(); $entry = json_decode($request->getBody(), true); - $connection=get_connection(); + $connection=getConnection(); $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)"); @@ -77,15 +80,15 @@ function add_entry() { $return=$statement->execute(); - echo("Entry saved."); + echo("Entry saved."); } // Saves an entry -function save_entry($id) { +function saveEntry($id) { $request = \Slim\Slim::getInstance()->request(); $entry = json_decode($request->getBody(), true); - $connection=get_connection(); + $connection=getConnection(); $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"); @@ -99,12 +102,12 @@ function save_entry($id) { $return=$statement->execute(); - echo($entry['id'] . " saved."); + echo($entry['id'] . " saved."); } // Remove an entry -function remove_entry($id) { - $connection=get_connection(); +function removeEntry($id) { + $connection=getConnection(); $statement=$connection->prepare("delete from entry where id=:id"); $statement->bindParam("id", $id); @@ -115,8 +118,8 @@ function remove_entry($id) { } // Return the accounts with their solds. -function get_accounts() { - $connection=get_connection(); +function getAccounts() { + $connection=getConnection(); $sql = <<fetchAll(PDO::FETCH_ASSOC))); } + +function addAccount() { + $request = \Slim\Slim::getInstance()->request(); + $account = json_decode($request->getBody(), true); + + $connection=getConnection(); + + $statement=$connection->prepare("insert into account (name) values (:name)"); + + $statement->bindParam("name", $account['name']); + + $return=$statement->execute(); + + echo("Account saved."); +} + +function saveAccount($id) { + $request = \Slim\Slim::getInstance()->request(); + $account = json_decode($request->getBody(), true); + + $connection=getConnection(); + + $statement=$connection->prepare("update account set name=:name where id=:id"); + + $statement->bindParam("name", $account['name']); + $statement->bindParam("id", $id); + + $return=$statement->execute(); + + echo("Account #$id saved."); +} + +// Remove an account +function removeAccount($id) { + $connection=getConnection(); + + $statement=$connection->prepare("delete from account where id=:id"); + $statement->bindParam("id", $id); + + $return=$statement->execute(); + + echo("Account #$id removed."); +} + ?> diff --git a/src/html/index.html b/src/html/index.html index 3421ffa..a7e180e 100644 --- a/src/html/index.html +++ b/src/html/index.html @@ -22,8 +22,8 @@ @@ -101,6 +101,39 @@ + + + +