1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 20:52:56 +01:00

Refactored API index methods

This commit is contained in:
Hillel Coren 2016-05-01 23:55:13 +03:00
parent e7296cf513
commit 4db5a19885
13 changed files with 186 additions and 228 deletions

View File

@ -2,6 +2,8 @@
use Session;
use Utils;
use Auth;
use Input;
use Response;
use Request;
use League\Fractal;
@ -9,8 +11,10 @@ use League\Fractal\Manager;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Collection;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use App\Models\EntityModel;
use App\Ninja\Serializers\ArraySerializer;
use League\Fractal\Serializer\JsonApiSerializer;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @SWG\Swagger(
@ -64,6 +68,23 @@ class BaseAPIController extends Controller
}
}
protected function returnList($query)
{
if ($clientPublicId = Input::get('client_id')) {
$filter = function($query) use ($clientPublicId) {
$query->where('public_id', '=', $clientPublicId);
};
$query->whereHas('client', $filter);
}
$transformerClass = EntityModel::getTransformerName($this->entityType);
$transformer = new $transformerClass(Auth::user()->account, Input::get('serializer'));
$data = $this->createCollection($query, $transformer, $this->entityType);
return $this->response($data);
}
protected function createItem($data, $transformer, $entityType)
{
if ($this->serializer && $this->serializer != API_SERIALIZER_JSON) {
@ -74,18 +95,19 @@ class BaseAPIController extends Controller
return $this->manager->createData($resource)->toArray();
}
protected function createCollection($data, $transformer, $entityType, $paginator = false)
protected function createCollection($query, $transformer, $entityType)
{
if ($this->serializer && $this->serializer != API_SERIALIZER_JSON) {
$entityType = null;
}
$resource = new Collection($data, $transformer, $entityType);
if ($paginator) {
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
if ($query instanceof LengthAwarePaginator) {
$resource = new Collection($query, $transformer, $entityType);
} else {
$resource = new Collection($query->get(), $transformer, $entityType);
$resource->setPaginator(new IlluminatePaginatorAdapter($query->paginate()));
}
return $this->manager->createData($resource)->toArray();
}

View File

@ -18,6 +18,8 @@ class ClientApiController extends BaseAPIController
protected $clientRepo;
protected $clientService;
protected $entityType = ENTITY_CLIENT;
public function __construct(ClientRepository $clientRepo, ClientService $clientService)
{
parent::__construct();
@ -53,26 +55,18 @@ class ClientApiController extends BaseAPIController
{
$clients = Client::scope()
->with($this->getIncluded())
->orderBy('created_at', 'desc')->withTrashed();
->orderBy('created_at', 'desc')
->withTrashed();
// Filter by email
if (Input::has('email')) {
$email = Input::get('email');
$clients = $clients->whereHas('contacts', function ($query) use ($email) {
$query->where('email', $email);
});
}
$clients = $clients->paginate();
$transformer = new ClientTransformer(Auth::user()->account, Input::get('serializer'));
$paginator = Client::scope()->withTrashed()->paginate();
$data = $this->createCollection($clients, $transformer, ENTITY_CLIENT, $paginator);
return $this->response($data);
return $this->returnList($clients);
}
/**

View File

@ -1,5 +1,5 @@
<?php namespace App\Http\Controllers;
// vendor
use App\Models\Expense;
use app\Ninja\Repositories\ExpenseRepository;
use App\Ninja\Transformers\ExpenseTransformer;
@ -16,6 +16,8 @@ class ExpenseApiController extends BaseAPIController
protected $expenseRepo;
protected $expenseService;
protected $entityType = ENTITY_EXPENSE;
public function __construct(ExpenseRepository $expenseRepo, ExpenseService $expenseService)
{
parent::__construct();
@ -26,20 +28,11 @@ class ExpenseApiController extends BaseAPIController
public function index()
{
$expenses = Expense::scope()
->withTrashed()
->orderBy('created_at','desc');
$expenses = $expenses->paginate();
$transformer = new ExpenseTransformer(Auth::user()->account, Input::get('serializer'));
$paginator = Expense::scope()->withTrashed()->paginate();
$data = $this->createCollection($expenses, $transformer, ENTITY_EXPENSE, $paginator);
return $this->response($data);
return $this->returnList($expenses);
}
public function update()

View File

@ -26,6 +26,8 @@ class InvoiceApiController extends BaseAPIController
{
protected $invoiceRepo;
protected $entityType = ENTITY_INVOICE;
public function __construct(InvoiceService $invoiceService, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, PaymentRepository $paymentRepo, Mailer $mailer)
{
parent::__construct();
@ -55,36 +57,12 @@ class InvoiceApiController extends BaseAPIController
*/
public function index()
{
$paginator = Invoice::scope()->withTrashed();
$invoices = Invoice::scope()->withTrashed()
->with(array_merge(['invoice_items'], $this->getIncluded()));
$invoices = Invoice::scope()
->withTrashed()
->with(array_merge(['invoice_items'], $this->getIncluded()))
->orderBy('created_at', 'desc');
if ($clientPublicId = Input::get('client_id')) {
$filter = function($query) use ($clientPublicId) {
$query->where('public_id', '=', $clientPublicId);
};
$invoices->whereHas('client', $filter);
$paginator->whereHas('client', $filter);
}
$invoices = $invoices->orderBy('created_at', 'desc')->paginate();
/*
// Add the first invitation link to the data
foreach ($invoices as $key => $invoice) {
foreach ($invoice->invitations as $subKey => $invitation) {
$invoices[$key]['link'] = $invitation->getLink();
}
unset($invoice['invitations']);
}
*/
$transformer = new InvoiceTransformer(Auth::user()->account, Input::get('serializer'));
$paginator = $paginator->paginate();
$data = $this->createCollection($invoices, $transformer, 'invoices', $paginator);
return $this->response($data);
return $this->returnList($invoices);
}
/**
@ -106,7 +84,6 @@ class InvoiceApiController extends BaseAPIController
public function show($publicId)
{
$invoice = Invoice::scope($publicId)->withTrashed()->first();
if(!$invoice)

View File

@ -546,13 +546,13 @@ class InvoiceController extends BaseController
}
}
public function convertQuote(InvoiceRequest $request, $publicId)
public function convertQuote(InvoiceRequest $request)
{
$invoice = Invoice::with('invoice_items')->scope($publicId)->firstOrFail();
$clone = $this->invoiceService->convertQuote($invoice);
$clone = $this->invoiceService->convertQuote($request->entity());
Session::flash('message', trans('texts.converted_to_invoice'));
return Redirect::to('invoices/'.$clone->public_id);
return Redirect::to('invoices/' . $clone->public_id);
}
public function cloneInvoice(InvoiceRequest $request, $publicId)

View File

@ -17,13 +17,14 @@ class PaymentApiController extends BaseAPIController
{
protected $paymentRepo;
protected $entityType = ENTITY_PAYMENT;
public function __construct(PaymentRepository $paymentRepo, ContactMailer $contactMailer)
{
parent::__construct();
$this->paymentRepo = $paymentRepo;
$this->contactMailer = $contactMailer;
}
/**
@ -44,85 +45,71 @@ class PaymentApiController extends BaseAPIController
*/
public function index()
{
$paginator = Payment::scope();
$payments = Payment::scope()
->with('client.contacts', 'invitation', 'user', 'invoice')->withTrashed();
->withTrashed()
->with(array_merge(['client.contacts', 'invitation', 'user', 'invoice'], $this->getIncluded()))
->orderBy('created_at', 'desc');
if ($clientPublicId = Input::get('client_id')) {
$filter = function($query) use ($clientPublicId) {
$query->where('public_id', '=', $clientPublicId);
};
$payments->whereHas('client', $filter);
$paginator->whereHas('client', $filter);
}
$payments = $payments->orderBy('created_at', 'desc')->paginate();
$paginator = $paginator->paginate();
$transformer = new PaymentTransformer(Auth::user()->account, Input::get('serializer'));
$data = $this->createCollection($payments, $transformer, 'payments', $paginator);
return $this->response($data);
return $this->returnList($payments);
}
/**
* @SWG\Put(
* path="/payments/{payment_id",
* summary="Update a payment",
* tags={"payment"},
* @SWG\Parameter(
* in="body",
* name="body",
* @SWG\Schema(ref="#/definitions/Payment")
* ),
* @SWG\Response(
* response=200,
* description="Update payment",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Payment"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
/**
* @SWG\Put(
* path="/payments/{payment_id",
* summary="Update a payment",
* tags={"payment"},
* @SWG\Parameter(
* in="body",
* name="body",
* @SWG\Schema(ref="#/definitions/Payment")
* ),
* @SWG\Response(
* response=200,
* description="Update payment",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Payment"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
public function update(Request $request, $publicId)
{
$data = Input::all();
$data['public_id'] = $publicId;
$error = false;
public function update(Request $request, $publicId)
{
$data = Input::all();
$data['public_id'] = $publicId;
$error = false;
if ($request->action == ACTION_ARCHIVE) {
$payment = Payment::scope($publicId)->withTrashed()->firstOrFail();
$this->paymentRepo->archive($payment);
$transformer = new PaymentTransformer(\Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($payment, $transformer, 'invoice');
return $this->response($data);
}
$payment = $this->paymentRepo->save($data);
if ($error) {
return $error;
}
/*
$invoice = Invoice::scope($data['invoice_id'])->with('client', 'invoice_items', 'invitations')->with(['payments' => function($query) {
$query->withTrashed();
}])->withTrashed()->first();
*/
if ($request->action == ACTION_ARCHIVE) {
$payment = Payment::scope($publicId)->withTrashed()->firstOrFail();
$this->paymentRepo->archive($payment);
$transformer = new PaymentTransformer(\Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($payment, $transformer, 'invoice');
return $this->response($data);
}
$payment = $this->paymentRepo->save($data);
if ($error) {
return $error;
}
/*
$invoice = Invoice::scope($data['invoice_id'])->with('client', 'invoice_items', 'invitations')->with(['payments' => function($query) {
$query->withTrashed();
}])->withTrashed()->first();
*/
$transformer = new PaymentTransformer(\Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($payment, $transformer, 'invoice');
return $this->response($data);
}
/**
* @SWG\Post(
@ -190,44 +177,44 @@ class PaymentApiController extends BaseAPIController
}
/**
* @SWG\Delete(
* path="/payments/{payment_id}",
* summary="Delete a payment",
* tags={"payment"},
* @SWG\Parameter(
* in="body",
* name="body",
* @SWG\Schema(ref="#/definitions/Payment")
* ),
* @SWG\Response(
* response=200,
* description="Delete payment",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Payment"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
/**
* @SWG\Delete(
* path="/payments/{payment_id}",
* summary="Delete a payment",
* tags={"payment"},
* @SWG\Parameter(
* in="body",
* name="body",
* @SWG\Schema(ref="#/definitions/Payment")
* ),
* @SWG\Response(
* response=200,
* description="Delete payment",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Payment"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
public function destroy($publicId)
{
public function destroy($publicId)
{
$payment = Payment::scope($publicId)->withTrashed()->first();
$invoiceId = $payment->invoice->public_id;
$payment = Payment::scope($publicId)->withTrashed()->first();
$invoiceId = $payment->invoice->public_id;
$this->paymentRepo->delete($payment);
$this->paymentRepo->delete($payment);
/*
$invoice = Invoice::scope($invoiceId)->with('client', 'invoice_items', 'invitations')->with(['payments' => function($query) {
$query->withTrashed();
}])->first();
*/
$transformer = new PaymentTransformer(\Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($payment, $transformer, 'invoice');
/*
$invoice = Invoice::scope($invoiceId)->with('client', 'invoice_items', 'invitations')->with(['payments' => function($query) {
$query->withTrashed();
}])->first();
*/
$transformer = new PaymentTransformer(\Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($payment, $transformer, 'invoice');
return $this->response($data);
}
return $this->response($data);
}
}

View File

@ -20,8 +20,9 @@ use App\Services\ProductService;
class ProductApiController extends BaseAPIController
{
protected $productService;
protected $productRepo;
protected $productRepo;
protected $entityType = ENTITY_PRODUCT;
public function __construct(ProductService $productService, ProductRepository $productRepo)
{
@ -33,17 +34,11 @@ class ProductApiController extends BaseAPIController
public function index()
{
$products = Product::scope()
->withTrashed()
->orderBy('created_at', 'desc');
$products = Product::scope()->withTrashed();
$products = $products->paginate();
$paginator = Product::scope()->withTrashed()->paginate();
$transformer = new ProductTransformer(\Auth::user()->account, $this->serializer);
$data = $this->createCollection($products, $transformer, 'products', $paginator);
return $this->response($data);
return $this->returnList($products);
}
public function getDatatable()

View File

@ -13,6 +13,8 @@ class TaskApiController extends BaseAPIController
{
protected $taskRepo;
protected $entityType = ENTITY_TASK;
public function __construct(TaskRepository $taskRepo)
{
parent::__construct();
@ -38,25 +40,12 @@ class TaskApiController extends BaseAPIController
*/
public function index()
{
$paginator = Task::scope();
$tasks = Task::scope()
->with($this->getIncluded());
$payments = Task::scope()
->withTrashed()
->with($this->getIncluded())
->orderBy('created_at', 'desc');
if ($clientPublicId = Input::get('client_id')) {
$filter = function($query) use ($clientPublicId) {
$query->where('public_id', '=', $clientPublicId);
};
$tasks->whereHas('client', $filter);
$paginator->whereHas('client', $filter);
}
$tasks = $tasks->orderBy('created_at', 'desc')->paginate();
$paginator = $paginator->paginate();
$transformer = new TaskTransformer(\Auth::user()->account, Input::get('serializer'));
$data = $this->createCollection($tasks, $transformer, 'tasks', $paginator);
return $this->response($data);
return $this->returnList($payments);
}
/**

View File

@ -14,6 +14,8 @@ class TaxRateApiController extends BaseAPIController
protected $taxRateService;
protected $taxRateRepo;
protected $entityType = ENTITY_TAX_RATE;
public function __construct(TaxRateService $taxRateService, TaxRateRepository $taxRateRepo)
{
parent::__construct();
@ -24,15 +26,11 @@ class TaxRateApiController extends BaseAPIController
public function index()
{
$taxRates = TaxRate::scope()->withTrashed();
$taxRates = $taxRates->paginate();
$paginator = TaxRate::scope()->withTrashed()->paginate();
$transformer = new TaxRateTransformer(Auth::user()->account, $this->serializer);
$data = $this->createCollection($taxRates, $transformer, 'tax_rates', $paginator);
return $this->response($data);
$taxRates = TaxRate::scope()
->withTrashed()
->orderBy('created_at', 'desc');
return $this->returnList($taxRates);
}
public function store(CreateTaxRateRequest $request)

View File

@ -14,6 +14,8 @@ class UserApiController extends BaseAPIController
protected $userService;
protected $userRepo;
protected $entityType = ENTITY_USER;
public function __construct(UserService $userService, UserRepository $userRepo)
{
parent::__construct();
@ -24,16 +26,11 @@ class UserApiController extends BaseAPIController
public function index()
{
$user = Auth::user();
$users = User::whereAccountId($user->account_id)->withTrashed();
$users = $users->paginate();
$paginator = User::whereAccountId($user->account_id)->withTrashed()->paginate();
$transformer = new UserTransformer(Auth::user()->account, $this->serializer);
$data = $this->createCollection($users, $transformer, 'users', $paginator);
return $this->response($data);
$users = User::whereAccountId(Auth::user()->account_id)
->withTrashed()
->orderBy('created_at', 'desc');
return $this->returnList($users);
}
/*

View File

@ -14,6 +14,8 @@ class VendorApiController extends BaseAPIController
{
protected $vendorRepo;
protected $entityType = ENTITY_VENDOR;
public function __construct(VendorRepository $vendorRepo)
{
parent::__construct();
@ -46,17 +48,12 @@ class VendorApiController extends BaseAPIController
*/
public function index()
{
$vendors = Vendor::scope()
$vendors = Vendor::scope()
->with($this->getIncluded())
->withTrashed()
->orderBy('created_at', 'desc')
->paginate();
->orderBy('created_at', 'desc');
$transformer = new VendorTransformer(Auth::user()->account, Input::get('serializer'));
$paginator = Vendor::scope()->paginate();
$data = $this->createCollection($vendors, $transformer, ENTITY_VENDOR, $paginator);
return $this->response($data);
return $this->returnList($vendors);
}
/**

View File

@ -4,17 +4,16 @@ class InvoiceRequest extends EntityRequest {
protected $entityType = ENTITY_INVOICE;
/*
public function entity()
{
$expense = parent::entity();
$invoice = parent::entity();
// eager load the contacts
if ($expense && ! count($expense->documents)) {
$expense->load('documents');
if ($invoice && ! count($invoice->invoice_items)) {
$invoice->load('invoice_items');
}
return $expense;
return $invoice;
}
*/
}

View File

@ -101,6 +101,16 @@ class EntityModel extends Eloquent
return $this->getName();
}
public static function getClassName($entityType)
{
return 'App\\Models\\' . ucwords(Utils::toCamelCase($entityType));
}
public static function getTransformerName($entityType)
{
return 'App\\Ninja\\Transformers\\' . ucwords(Utils::toCamelCase($entityType)) . 'Transformer';
}
public function setNullValues()
{
foreach ($this->fillable as $field) {