1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

working on api

This commit is contained in:
David Bomba 2015-11-03 13:02:15 +11:00
parent c3066c824f
commit 38c3f14081
4 changed files with 54 additions and 2 deletions

View File

@ -6,6 +6,7 @@ use Response;
use Input; use Input;
use App\Models\Client; use App\Models\Client;
use App\Models\Account; use App\Models\Account;
use App\Models\AccountToken;
use App\Ninja\Repositories\AccountRepository; use App\Ninja\Repositories\AccountRepository;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use League\Fractal; use League\Fractal;
@ -54,7 +55,25 @@ class AccountApiController extends Controller
private function processLogin(Request $request) private function processLogin(Request $request)
{ {
\Log::info('authed user = '.Auth::user()->email);
//Create a new token only if one does not already exist
if(!AccountToken::where('user_id', '=', Auth::user()->id)->firstOrFail())
{
$account_token = $this->accountRepo->createToken($request->token_name);
}
$manager = new Manager();
$manager->setSerializer(new ArraySerializer());
$account = Auth::user()->account->load('users','tokens');
$resource = new Item($account, new AccountTransformer, 'account');
$response = $manager->createData($resource)->toArray();
$response = json_encode($response, JSON_PRETTY_PRINT);
$headers = Utils::getApiHeaders();
return Response::make($response, 200, $headers);
} }

View File

@ -41,6 +41,10 @@ class Account extends Eloquent
'invoice_settings' => 'object', 'invoice_settings' => 'object',
]; ];
*/ */
public function tokens()
{
return $this->hasMany('App\Models\AccountToken');
}
public function users() public function users()
{ {

View File

@ -0,0 +1,21 @@
<?php namespace App\Ninja\Transformers;
use App\Models\AccountToken;
use League\Fractal;
use League\Fractal\TransformerAbstract;
class AccountTokenTransformer extends TransformerAbstract
{
public function transform(AccountToken $account_token)
{
return [
'id' => (int) $account_token->id,
'account_id' =>(int) $account_token->account_id,
'user_id' => (int) $account_token->user_id,
'public_id' => (int) $account_token->public_id,
'name' => $account_token->name,
'token' => $account_token->token
];
}
}

View File

@ -7,9 +7,17 @@ use League\Fractal\TransformerAbstract;
class AccountTransformer extends TransformerAbstract class AccountTransformer extends TransformerAbstract
{ {
protected $defaultIncludes = [ protected $defaultIncludes = [
'users' 'users',
'account_tokens'
]; ];
public function includeAccountTokens($account)
{
$account_tokens = $account->tokens;
return $this->collection($account_tokens, new AccountTokenTransformer);
}
public function includeUsers($account) public function includeUsers($account)
{ {
$users = $account->users; $users = $account->users;