Skip to content

Instantly share code, notes, and snippets.

@icflorescu
Created July 24, 2012 17:34
Show Gist options
  • Select an option

  • Save icflorescu/3171376 to your computer and use it in GitHub Desktop.

Select an option

Save icflorescu/3171376 to your computer and use it in GitHub Desktop.
RESTful API - Accounts Controller
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* Accounts RESTful API controller.
*
* @package Nomisma
* @category Controllers
* @author Ionut-Cristian Florescu
*/
class Controller_Api_Accounts extends Controller_Api {
/**
* GET /api/accounts(/id)
*/
public function action_index()
{
if ($this->request->param('id'))
{
try
{
Database::instance()->begin();
$account = ORM::factory('Account')
->where('id', '=', $this->request->param('id'))
->and_where('profile_id', '=', Auth::instance()->profile_id)
->find();
if ( ! $account->loaded())
throw new Exception_DataNotFound;
$this->_response_payload = $account->as_array();
$ob_transaction = DB::select('t.date', array('"t.sign" * "t.amount"', 'balance'))
->from(array('transactions', 't'))
->join(array('transaction_items', 'ti'))->on('ti.transaction_id', '=', 't.id')
->where('t.account_id', '=', $this->request->param('id'))
->and_where('ti.category_id', '=', Kohana::config('nomisma.transaction_category_id.opening_balance'))
->execute()->current();
$this->_response_payload['opening_date'] = $ob_transaction['date'];
$this->_response_payload['opening_balance'] = $ob_transaction['balance'];
$this->_response_payload['current_balance'] = DB::select(array('SUM("sign" * "amount")', 'balance'))
->from('transactions')
->where('account_id', '=', $this->request->param('id'))
->execute()->get('balance');
Database::instance()->commit();
}
catch (Exception $e)
{
Database::instance()->rollback();
throw $e;
}
}
else
{
$this->_response_payload = DB::select(
'a.id', 'a.name', 'a.type', 'a.currency_code', 'a.is_active',
array('SUM("t.sign" * "t.amount")', 'current_balance')
)
->from(array('accounts', 'a'))
->join(array('transactions', 't'))->on('t.account_id', '=', 'a.id')
->where('a.profile_id', '=', Auth::instance()->profile_id)
->and_where('a.type', 'IN', array('c', 'b'))
->group_by('a.id')
->execute()->as_array();
}
}
/**
* POST /api/accounts
*/
public function action_create()
{
$expected_values = array('name', 'type', 'currency_code', 'is_active', 'description');
$this->_response_payload = ORM::factory('Account')
->values($this->_request_payload, $expected_values)
->set('profile_id', Auth::instance()->profile_id)
->sign()->save()->as_array();
}
/**
* PUT /api/contacts
*/
public function action_update()
{
$expected_values = array('name', 'email_address', 'telephone_number', 'description');
try
{
Database::instance()->begin();
$this->_response_payload = ORM::factory('Contact')
->where('id', '=', $this->request->param('id'))
->and_where('profile_id', '=', Auth::instance()->profile_id)
->find()
->values($this->_request_payload, $expected_values)
->sign()->save()->as_array();
Database::instance()->commit();
}
catch (Exception $e)
{
Database::instance()->rollback();
throw $e;
}
}
/**
* DELETE /api/contacts
*/
public function action_delete()
{
try
{
Database::instance()->begin();
ORM::factory('Contact')
->where('id', '=', $this->request->param('id'))
->and_where('profile_id', '=', Auth::instance()->profile_id)
->find()
->delete();
$this->_response_payload = array('success' => TRUE);
Database::instance()->commit();
}
catch (Exception $e)
{
Database::instance()->rollback();
throw $e;
}
}
} // End Controller_Api_Accounts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment