1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00

API optimizations

This commit is contained in:
Hillel Coren 2016-05-03 09:46:24 +03:00
parent 95fdda6e1b
commit 6cd4ff025e
10 changed files with 38 additions and 17 deletions

View File

@ -3,6 +3,7 @@
use Session;
use Utils;
use Auth;
use Log;
use Input;
use Response;
use Request;
@ -66,6 +67,10 @@ class BaseAPIController extends Controller
} else {
$this->manager->setSerializer(new ArraySerializer());
}
if (Utils::isNinjaDev()) {
\DB::enableQueryLog();
}
}
protected function handleAction($request)
@ -82,7 +87,8 @@ class BaseAPIController extends Controller
protected function listResponse($query)
{
//\DB::enableQueryLog();
$query->with($this->getIncluded());
if ($clientPublicId = Input::get('client_id')) {
$filter = function($query) use ($clientPublicId) {
$query->where('public_id', '=', $clientPublicId);
@ -103,7 +109,6 @@ class BaseAPIController extends Controller
$data = $this->createCollection($query, $transformer, $this->entityType);
//return \DB::getQueryLog();
return $this->response($data);
}
@ -146,6 +151,11 @@ class BaseAPIController extends Controller
protected function response($response)
{
if (Utils::isNinjaDev()) {
$count = count(\DB::getQueryLog());
Log::info(Request::method() . ' - ' . Request::url() . ": $count queries");
}
$index = Request::get('index') ?: 'data';
if ($index == 'none') {

View File

@ -44,7 +44,6 @@ class ClientApiController extends BaseAPIController
public function index()
{
$clients = Client::scope()
->with($this->getIncluded())
->orderBy('created_at', 'desc')
->withTrashed();

View File

@ -60,7 +60,7 @@ class InvoiceApiController extends BaseAPIController
{
$invoices = Invoice::scope()
->withTrashed()
->with(array_merge(['invoice_items'], $this->getIncluded()))
->with('invoice_items')
->orderBy('created_at', 'desc');
return $this->listResponse($invoices);

View File

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

View File

@ -42,7 +42,6 @@ class TaskApiController extends BaseAPIController
{
$payments = Task::scope()
->withTrashed()
->with($this->getIncluded())
->orderBy('created_at', 'desc');
return $this->listResponse($payments);

View File

@ -49,7 +49,6 @@ class VendorApiController extends BaseAPIController
public function index()
{
$vendors = Vendor::scope()
->with($this->getIncluded())
->withTrashed()
->orderBy('created_at', 'desc');

View File

@ -58,7 +58,7 @@ class ClientTransformer extends EntityTransformer
public function includeInvoices(Client $client)
{
$transformer = new InvoiceTransformer($this->account, $this->serializer);
$transformer = new InvoiceTransformer($this->account, $this->serializer, $client);
return $this->includeCollection($client->invoices, $transformer, ENTITY_INVOICE);
}

View File

@ -6,9 +6,15 @@ use League\Fractal;
class ExpenseTransformer extends EntityTransformer
{
public function __construct($account = null, $serializer = null, $client = null)
{
parent::__construct($account, $serializer);
$this->client = $client;
}
public function transform(Expense $expense)
{
return [
'id' => (int) $expense->public_id,
'private_notes' => $expense->private_notes,
@ -25,7 +31,7 @@ class ExpenseTransformer extends EntityTransformer
'exchange_rate' => (float) $expense->exchange_rate,
'invoice_currency_id' => (int) $expense->invoice_currency_id,
'is_deleted' => (bool) $expense->is_deleted,
'client_id' => isset($expense->client->public_id) ? (int) $expense->client->public_id : null,
'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

@ -31,6 +31,13 @@ class InvoiceTransformer extends EntityTransformer
'expenses',
];
public function __construct($account = null, $serializer = null, $client = null)
{
parent::__construct($account, $serializer);
$this->client = $client;
}
public function includeInvoiceItems(Invoice $invoice)
{
$transformer = new InvoiceItemTransformer($this->account, $this->serializer);
@ -45,7 +52,7 @@ class InvoiceTransformer extends EntityTransformer
public function includePayments(Invoice $invoice)
{
$transformer = new PaymentTransformer($this->account, $this->serializer);
$transformer = new PaymentTransformer($this->account, $this->serializer, $invoice);
return $this->includeCollection($invoice->payments, $transformer, ENTITY_PAYMENT);
}
@ -68,7 +75,7 @@ class InvoiceTransformer extends EntityTransformer
'id' => (int) $invoice->public_id,
'amount' => (float) $invoice->amount,
'balance' => (float) $invoice->balance,
'client_id' => (int) $invoice->client->public_id,
'client_id' => (int) ($this->client ? $this->client->public_id : $invoice->client->public_id),
'invoice_status_id' => (int) $invoice->invoice_status_id,
'updated_at' => $this->getTimestamp($invoice->updated_at),
'archived_at' => $this->getTimestamp($invoice->deleted_at),

View File

@ -26,10 +26,11 @@ class PaymentTransformer extends EntityTransformer
];
public function __construct(Account $account)
public function __construct($account = null, $serializer = null, $invoice = null)
{
parent::__construct($account);
parent::__construct($account, $serializer);
$this->invoice = $invoice;
}
public function includeInvoice(Payment $payment)
@ -57,7 +58,7 @@ class PaymentTransformer extends EntityTransformer
'archived_at' => $this->getTimestamp($payment->deleted_at),
'is_deleted' => (bool) $payment->is_deleted,
'payment_type_id' => (int) $payment->payment_type_id,
'invoice_id' => (int) $payment->invoice->public_id,
'invoice_id' => (int) ($this->invoice ? $this->invoice->public_id : $payment->invoice->public_id),
];
}
}