Working on the API

This commit is contained in:
Hillel Coren 2015-11-03 16:21:17 +02:00
parent 26ec972819
commit ebd63f1805
7 changed files with 135 additions and 14 deletions

View File

@ -10,10 +10,6 @@ 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

@ -9,21 +9,17 @@ class AccountTransformer extends TransformerAbstract
{
protected $defaultIncludes = [
'users',
'account_tokens'
'clients',
];
public function includeAccountTokens($account)
{
$account_token = AccountToken::whereAccountId($account->id)->whereName('ios_api_token')->first();
return $this->Item($account_token, new AccountTokenTransformer);
}
public function includeUsers($account)
{
$users = $account->users;
return $this->collection($account->users, new UserTransformer);
}
return $this->collection($users, new UserTransformer);
public function includeClients($account)
{
return $this->collection($account->clients, new ClientTransformer);
}
public function transform(Account $account)

View File

@ -0,0 +1,34 @@
<?php namespace App\Ninja\Transformers;
use App\Models\Client;
use App\Models\Contact;
use League\Fractal;
use League\Fractal\TransformerAbstract;
class ClientTransformer extends TransformerAbstract
{
protected $defaultIncludes = [
'contacts',
'invoices',
];
public function includeContacts($client)
{
return $this->collection($client->contacts, new ContactTransformer);
}
public function includeInvoices($client)
{
return $this->collection($client->invoices, new InvoiceTransformer);
}
public function transform(Client $client)
{
return [
'id' => (int) $client->public_id,
'name' => $client->name,
'balance' => (float) $client->balance,
'paid_to_date' => (float) $client->paid_to_date,
];
}
}

View File

@ -0,0 +1,18 @@
<?php namespace App\Ninja\Transformers;
use App\Models\Contact;
use League\Fractal;
use League\Fractal\TransformerAbstract;
class ContactTransformer extends TransformerAbstract
{
public function transform(Contact $contact)
{
return [
'id' => (int) $contact->public_id,
'first_name' => $contact->first_name,
'last_name' => $contact->last_name,
'email' => $contact->email,
];
}
}

View File

@ -0,0 +1,16 @@
<?php namespace App\Ninja\Transformers;
use App\Models\InvoiceItem;
use League\Fractal;
use League\Fractal\TransformerAbstract;
class InvoiceItemTransformer extends TransformerAbstract
{
public function transform(InvoiceItem $item)
{
return [
'id' => (int) $item->public_id,
'product_key' => $item->product_key,
];
}
}

View File

@ -0,0 +1,27 @@
<?php namespace App\Ninja\Transformers;
use App\Models\Invoice;
use League\Fractal;
use League\Fractal\TransformerAbstract;
class InvoiceTransformer extends TransformerAbstract
{
protected $defaultIncludes = [
'invoice_items',
];
public function includeInvoiceItems($invoice)
{
return $this->collection($invoice->invoice_items, new InvoiceItemTransformer);
}
public function transform(Invoice $invoice)
{
return [
'id' => (int) $invoice->public_id,
'invoice_number' => $invoice->invoice_number,
'amount' => (float) $invoice->amount,
'balance' => (float) $invoice->balance,
];
}
}

View File

@ -0,0 +1,34 @@
<?php namespace App\Ninja\Transformers;
use App\Models\User;
use League\Fractal;
use League\Fractal\TransformerAbstract;
class UserAccountTransformer extends TransformerAbstract
{
protected $defaultIncludes = [
'account_tokens'
];
public function includeAccountTokens($user)
{
$tokens = $user->account->account_tokens->filter(function($token) use ($user) {
return $token->user_id === $user->id;
});
return $this->collection($tokens, new AccountTokenTransformer);
}
public function transform(User $user)
{
return [
'account_key' => $user->account->account_key,
'name' => $user->account->name,
'user' => [
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
]
];
}
}