1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00
invoiceninja/app/controllers/ClientApiController.php

52 lines
1.3 KiB
PHP
Raw Normal View History

2014-05-13 21:20:54 +02:00
<?php
use ninja\repositories\ClientRepository;
2015-01-11 13:30:08 +01:00
class ClientApiController extends Controller
{
protected $clientRepo;
2014-05-13 21:20:54 +02:00
2015-01-11 13:30:08 +01:00
public function __construct(ClientRepository $clientRepo)
{
$this->clientRepo = $clientRepo;
}
2014-05-13 21:20:54 +02:00
2015-01-11 13:30:08 +01:00
public function ping()
{
$headers = Utils::getApiHeaders();
2014-07-27 22:31:41 +02:00
2015-01-11 13:30:08 +01:00
return Response::make('', 200, $headers);
2014-07-27 22:31:41 +02:00
}
2015-01-11 13:30:08 +01:00
public function index()
{
$clients = Client::scope()->with('contacts')->orderBy('created_at', 'desc')->get();
$clients = Utils::remapPublicIds($clients->toArray());
2014-07-27 22:31:41 +02:00
2015-01-11 13:30:08 +01:00
$response = json_encode($clients, JSON_PRETTY_PRINT);
$headers = Utils::getApiHeaders(count($clients));
2014-07-27 22:31:41 +02:00
2015-01-11 13:30:08 +01:00
return Response::make($response, 200, $headers);
2014-07-30 09:08:01 +02:00
}
2015-01-11 13:30:08 +01:00
public function store()
2014-07-30 09:08:01 +02:00
{
2015-01-11 13:30:08 +01:00
$data = Input::all();
$error = $this->clientRepo->getErrors($data);
2014-05-13 23:04:42 +02:00
2015-01-11 13:30:08 +01:00
if ($error) {
$headers = Utils::getApiHeaders();
return Response::make($error, 500, $headers);
} else {
$client = $this->clientRepo->save(false, $data, false);
$client->load('contacts');
$client = Utils::remapPublicIds($client->toArray());
2015-01-11 13:30:08 +01:00
$response = json_encode($client, JSON_PRETTY_PRINT);
$headers = Utils::getApiHeaders();
return Response::make($response, 200, $headers);
}
}
}