1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

API optimizations

This commit is contained in:
Hillel Coren 2016-05-03 23:02:29 +03:00
parent c0a5084282
commit 2d878b6480
19 changed files with 58 additions and 55 deletions

View File

@ -193,26 +193,19 @@ class BaseAPIController extends Controller
protected function getRequestIncludes($data)
{
$data[] = 'user';
$included = Request::get('include');
$included = explode(',', $included);
foreach ($included as $include) {
if ($include == 'invoices') {
$data[] = 'invoices.invoice_items';
$data[] = 'invoices.user';
} elseif ($include == 'client') {
$data[] = 'client.contacts';
$data[] = 'client.user';
} elseif ($include == 'clients') {
$data[] = 'clients.contacts';
$data[] = 'clients.user';
} elseif ($include == 'vendors') {
$data[] = 'vendors.vendorcontacts';
$data[] = 'vendors.user';
}
elseif ($include) {
} elseif ($include) {
$data[] = $include;
}
}

View File

@ -49,7 +49,7 @@ class PaymentApiController extends BaseAPIController
{
$payments = Payment::scope()
->withTrashed()
->with(['invoice'])
->with(['invoice'])
->orderBy('created_at', 'desc');
return $this->listResponse($payments);

View File

@ -19,6 +19,11 @@ class InvoiceItem extends EntityModel
return $this->belongsTo('App\Models\Invoice');
}
public function user()
{
return $this->belongsTo('App\Models\User')->withTrashed();
}
public function product()
{
return $this->belongsTo('App\Models\Product');

View File

@ -26,6 +26,11 @@ class Product extends EntityModel
return Product::scope()->where('product_key', '=', $key)->first();
}
public function user()
{
return $this->belongsTo('App\Models\User')->withTrashed();
}
public function default_tax_rate()
{
return $this->belongsTo('App\Models\TaxRate');

View File

@ -17,4 +17,9 @@ class TaxRate extends EntityModel
{
return ENTITY_TAX_RATE;
}
public function user()
{
return $this->belongsTo('App\Models\User')->withTrashed();
}
}

View File

@ -77,13 +77,11 @@ class ClientTransformer extends EntityTransformer
public function transform(Client $client)
{
return [
return array_merge($this->getDefaults($client), [
'id' => (int) $client->public_id,
'name' => $client->name,
'balance' => (float) $client->balance,
'paid_to_date' => (float) $client->paid_to_date,
'user_id' => (int) $client->user->public_id + 1,
'account_key' => $this->account->account_key,
'updated_at' => $this->getTimestamp($client->updated_at),
'archived_at' => $this->getTimestamp($client->deleted_at),
'address1' => $client->address1,
@ -106,6 +104,6 @@ class ClientTransformer extends EntityTransformer
'currency_id' => (int) $client->currency_id,
'custom_value1' => $client->custom_value1,
'custom_value2' => $client->custom_value2,
];
]);
}
}

View File

@ -8,7 +8,7 @@ class ContactTransformer extends EntityTransformer
{
public function transform(Contact $contact)
{
return [
return array_merge($this->getDefaults($contact), [
'id' => (int) $contact->public_id,
'first_name' => $contact->first_name,
'last_name' => $contact->last_name,
@ -18,8 +18,7 @@ class ContactTransformer extends EntityTransformer
'is_primary' => (bool) $contact->is_primary,
'phone' => $contact->phone,
'last_login' => $contact->last_login,
'account_key' => $this->account->account_key,
'send_invoice' => (bool) $contact->send_invoice,
];
]);
}
}

View File

@ -8,17 +8,16 @@ class CreditTransformer extends EntityTransformer
{
public function transform(Credit $credit)
{
return [
return array_merge($this->getDefaults($credit), [
'id' => (int) $credit->public_id,
'amount' => (float) $credit->amount,
'balance' => (float) $credit->balance,
'updated_at' => $this->getTimestamp($credit->updated_at),
'archived_at' => $this->getTimestamp($credit->deleted_at),
'is_deleted' => (bool) $credit->is_deleted,
'account_key' => $this->account->account_key,
'credit_date' => $credit->credit_date,
'credit_number' => $credit->credit_number,
'private_notes' => $credit->private_notes,
];
]);
}
}

View File

@ -8,14 +8,12 @@ class DocumentTransformer extends EntityTransformer
{
public function transform(Document $document)
{
return [
return array_merge($this->getDefaults($document), [
'id' => (int) $document->public_id,
'name' => $document->name,
'account_key' => $this->account->account_key,
'type' => $document->type,
'invoice_id' => isset($document->invoice->public_id) ? (int) $document->invoice->public_id : null,
'expense_id' => isset($document->expense->public_id) ? (int) $document->expense->public_id : null,
];
]);
}
}

View File

@ -1,5 +1,6 @@
<?php namespace App\Ninja\Transformers;
use Auth;
use App\Models\Account;
use App\Models\Client;
use League\Fractal\TransformerAbstract;
@ -42,4 +43,18 @@ class EntityTransformer extends TransformerAbstract
{
return $this->defaultIncludes;
}
protected function getDefaults($entity)
{
$data = [
'account_key' => $this->account->account_key,
'is_owner' => (bool) Auth::user()->owns($entity),
];
if ($entity->relationLoaded('user')) {
$data['user_id'] = (int) $entity->user->public_id + 1;
}
return $data;
}
}

View File

@ -15,7 +15,7 @@ class ExpenseTransformer extends EntityTransformer
public function transform(Expense $expense)
{
return [
return array_merge($this->getDefaults($expense), [
'id' => (int) $expense->public_id,
'private_notes' => $expense->private_notes,
'public_notes' => $expense->public_notes,
@ -25,7 +25,6 @@ class ExpenseTransformer extends EntityTransformer
'transaction_id' => $expense->transaction_id,
'bank_id' => $expense->bank_id,
'expense_currency_id' => (int) $expense->expense_currency_id,
'account_key' => $this->account->account_key,
'amount' => (float) $expense->amount,
'expense_date' => $expense->expense_date,
'exchange_rate' => (float) $expense->exchange_rate,
@ -34,6 +33,6 @@ class ExpenseTransformer extends EntityTransformer
'client_id' => $this->client ? $this->client->public_id : (isset($expense->client->public_id) ? (int) $expense->client->public_id : null),
'invoice_id' => isset($expense->invoice->public_id) ? (int) $expense->invoice->public_id : null,
'vendor_id' => isset($expense->vendor->public_id) ? (int) $expense->vendor->public_id : null,
];
]);
}
}

View File

@ -8,11 +8,9 @@ class InvoiceItemTransformer extends EntityTransformer
{
public function transform(InvoiceItem $item)
{
return [
return array_merge($this->getDefaults($item), [
'id' => (int) $item->public_id,
'product_key' => $item->product_key,
'account_key' => $this->account->account_key,
'user_id' => (int) $item->user_id,
'updated_at' => $this->getTimestamp($item->updated_at),
'archived_at' => $this->getTimestamp($item->deleted_at),
'product_key' => $item->product_key,
@ -23,6 +21,6 @@ class InvoiceItemTransformer extends EntityTransformer
'tax_rate1' => (float) $item->tax_rate1,
'tax_name2' => $item->tax_name2 ? $item->tax_name1 : '',
'tax_rate2' => (float) $item->tax_rate2,
];
]);
}
}

View File

@ -71,7 +71,7 @@ class InvoiceTransformer extends EntityTransformer
public function transform(Invoice $invoice)
{
return [
return array_merge($this->getDefaults($invoice), [
'id' => (int) $invoice->public_id,
'amount' => (float) $invoice->amount,
'balance' => (float) $invoice->balance,
@ -105,8 +105,6 @@ class InvoiceTransformer extends EntityTransformer
'partial' => (float) $invoice->partial,
'has_tasks' => (bool) $invoice->has_tasks,
'auto_bill' => (bool) $invoice->auto_bill,
'account_key' => $this->account->account_key,
'user_id' => (int) $invoice->user->public_id + 1,
'custom_value1' => (float) $invoice->custom_value1,
'custom_value2' => (float) $invoice->custom_value2,
'custom_taxes1' => (bool) $invoice->custom_taxes1,
@ -115,6 +113,6 @@ class InvoiceTransformer extends EntityTransformer
'quote_invoice_id' => (int) $invoice->quote_invoice_id,
'custom_text_value1' => $invoice->custom_text_value1,
'custom_text_value2' => $invoice->custom_text_value2,
];
]);
}
}

View File

@ -47,11 +47,9 @@ class PaymentTransformer extends EntityTransformer
public function transform(Payment $payment)
{
return [
return array_merge($this->getDefaults($payment), [
'id' => (int) $payment->public_id,
'amount' => (float) $payment->amount,
'account_key' => $this->account->account_key,
'user_id' => (int) $payment->user->public_id + 1,
'transaction_reference' => $payment->transaction_reference,
'payment_date' => $payment->payment_date,
'updated_at' => $this->getTimestamp($payment->updated_at),
@ -59,6 +57,6 @@ class PaymentTransformer extends EntityTransformer
'is_deleted' => (bool) $payment->is_deleted,
'payment_type_id' => (int) $payment->payment_type_id,
'invoice_id' => (int) ($this->invoice ? $this->invoice->public_id : $payment->invoice->public_id),
];
]);
}
}

View File

@ -7,16 +7,15 @@ class ProductTransformer extends EntityTransformer
{
public function transform(Product $product)
{
return [
return array_merge($this->getDefaults(), [
'id' => (int) $product->public_id,
'product_key' => $product->product_key,
'notes' => $product->notes,
'cost' => $product->cost,
'qty' => $product->qty,
'account_key' =>$this->account->account_key,
'default_tax_rate_id' =>$product->default_tax_rate_id,
'updated_at' =>$this->getTimestamp($product->updated_at),
'archived_at' => $this->getTimestamp($product->deleted_at),
];
]);
}
}

View File

@ -39,12 +39,10 @@ class TaskTransformer extends EntityTransformer
public function transform(Task $task)
{
return [
return array_merge($this->getDefaults($task), [
'id' => (int) $task->public_id,
'account_key' => $this->account->account_key,
'user_id' => (int) $task->user->public_id + 1,
'description' => $task->description,
'duration' => $task->getDuration()
];
]);
}
}

View File

@ -21,13 +21,12 @@ class TaxRateTransformer extends EntityTransformer
public function transform(TaxRate $taxRate)
{
return [
return array_merge($this->getDefaults($taxRate), [
'id' => (int) $taxRate->public_id,
'name' => $taxRate->name,
'rate' => (float) $taxRate->rate,
'updated_at' => $this->getTimestamp($taxRate->updated_at),
'archived_at' => $this->getTimestamp($taxRate->deleted_at),
'account_key' => $this->account->account_key,
];
]);
}
}

View File

@ -8,7 +8,7 @@ class VendorContactTransformer extends EntityTransformer
{
public function transform(VendorContact $contact)
{
return [
return array_merge($this->getDefaults($contact), [
'id' => (int) $contact->public_id,
'first_name' => $contact->first_name,
'last_name' => $contact->last_name,
@ -17,7 +17,6 @@ class VendorContactTransformer extends EntityTransformer
'archived_at' => $this->getTimestamp($contact->deleted_at),
'is_primary' => (bool) $contact->is_primary,
'phone' => $contact->phone,
'account_key' => $this->account->account_key,
];
]);
}
}

View File

@ -61,13 +61,11 @@ class VendorTransformer extends EntityTransformer
public function transform(Vendor $vendor)
{
return [
return array_merge($this->getDefaults($vendor), [
'id' => (int) $vendor->public_id,
'name' => $vendor->name,
'balance' => (float) $vendor->balance,
'paid_to_date' => (float) $vendor->paid_to_date,
'user_id' => (int) $vendor->user->public_id + 1,
'account_key' => $this->account->account_key,
'updated_at' => $this->getTimestamp($vendor->updated_at),
'archived_at' => $this->getTimestamp($vendor->deleted_at),
'address1' => $vendor->address1,
@ -84,6 +82,6 @@ class VendorTransformer extends EntityTransformer
'vat_number' => $vendor->vat_number,
'id_number' => $vendor->id_number,
'currency_id' => (int) $vendor->currency_id
];
]);
}
}