invoiceninja/app/Http/Controllers/ClientApiController.php

150 lines
4.1 KiB
PHP
Raw Normal View History

2015-03-17 11:30:56 +10:00
<?php namespace App\Http\Controllers;
2015-03-17 07:45:25 +10:00
2015-04-08 21:19:58 +03:00
use Utils;
use Response;
use Input;
2015-11-27 14:55:28 +02:00
use Auth;
2015-04-08 21:19:58 +03:00
use App\Models\Client;
use App\Ninja\Repositories\ClientRepository;
2015-10-28 21:22:07 +02:00
use App\Http\Requests\CreateClientRequest;
2015-11-27 14:55:28 +02:00
use App\Http\Controllers\BaseAPIController;
use App\Ninja\Transformers\ClientTransformer;
2016-01-26 22:36:00 +11:00
use App\Services\ClientService;
use App\Http\Requests\UpdateClientRequest;
2015-03-17 07:45:25 +10:00
class ClientApiController extends BaseAPIController
2015-03-17 07:45:25 +10:00
{
protected $clientRepo;
2016-01-26 22:36:00 +11:00
protected $clientService;
2015-03-17 07:45:25 +10:00
2016-01-26 22:36:00 +11:00
public function __construct(ClientRepository $clientRepo, ClientService $clientService)
2015-03-17 07:45:25 +10:00
{
parent::__construct();
2015-03-17 07:45:25 +10:00
$this->clientRepo = $clientRepo;
2016-01-26 22:36:00 +11:00
$this->clientService = $clientService;
2015-03-17 07:45:25 +10:00
}
public function ping()
{
$headers = Utils::getApiHeaders();
return Response::make('', 200, $headers);
}
2015-11-08 23:57:28 +02:00
/**
* @SWG\Get(
* path="/clients",
* summary="List of clients",
* tags={"client"},
* @SWG\Response(
* response=200,
* description="A list with clients",
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Client"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
2015-03-17 07:45:25 +10:00
public function index()
{
2015-07-02 23:21:29 +03:00
$clients = Client::scope()
2015-11-27 14:55:28 +02:00
->with($this->getIncluded())
2016-01-24 16:59:01 +00:00
->orderBy('created_at', 'desc');
// 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();
2015-03-17 07:45:25 +10:00
2015-11-27 14:55:28 +02:00
$transformer = new ClientTransformer(Auth::user()->account, Input::get('serializer'));
$paginator = Client::scope()->paginate();
2015-11-27 14:55:28 +02:00
$data = $this->createCollection($clients, $transformer, ENTITY_CLIENT, $paginator);
2015-03-17 07:45:25 +10:00
2015-11-27 14:55:28 +02:00
return $this->response($data);
2015-03-17 07:45:25 +10:00
}
2015-11-08 23:57:28 +02:00
/**
* @SWG\Post(
* path="/clients",
* tags={"client"},
* summary="Create a client",
* @SWG\Parameter(
* in="body",
* name="body",
* @SWG\Schema(ref="#/definitions/Client")
* ),
* @SWG\Response(
* response=200,
* description="New client",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Client"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
2015-10-28 21:22:07 +02:00
public function store(CreateClientRequest $request)
2015-03-17 07:45:25 +10:00
{
2016-01-24 17:06:34 +00:00
$client = $this->clientRepo->save($request->input());
2015-11-27 14:55:28 +02:00
$client = Client::scope($client->public_id)
->with('country', 'contacts', 'industry', 'size', 'currency')
->first();
2015-10-28 21:22:07 +02:00
2015-11-27 14:55:28 +02:00
$transformer = new ClientTransformer(Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($client, $transformer, ENTITY_CLIENT);
2015-11-27 14:55:28 +02:00
return $this->response($data);
2015-03-17 07:45:25 +10:00
}
2016-01-26 22:36:00 +11:00
/**
2016-01-26 22:37:16 +11:00
* @SWG\Put(
2016-01-26 22:36:00 +11:00
* path="/clients/{client_id}",
* tags={"client"},
* summary="Update a client",
* @SWG\Parameter(
* in="body",
* name="body",
* @SWG\Schema(ref="#/definitions/Client")
* ),
* @SWG\Response(
* response=200,
* description="Update client",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Client"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
2016-01-27 07:52:30 +11:00
public function update(UpdateClientRequest $request, $publicId)
2016-01-26 22:36:00 +11:00
{
2016-01-27 07:52:30 +11:00
$data = $request->input();
$data['public_id'] = $publicId;
$this->clientService->save($data);
2016-01-26 22:36:00 +11:00
2016-01-27 07:52:30 +11:00
$client = Client::scope($publicId)
2016-01-26 22:36:00 +11:00
->with('country', 'contacts', 'industry', 'size', 'currency')
->first();
$transformer = new ClientTransformer(Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($client, $transformer, ENTITY_CLIENT);
return $this->response($data);
}
2015-03-17 07:45:25 +10:00
}