1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/ClientApiController.php

203 lines
5.6 KiB
PHP
Raw Normal View History

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