Files
testRestApi/src/Controller/Api/CustomersController.php
T
2026-10-21 18:34:35 +02:00

119 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Controller\Api;
use Cake\Http\Response;
/**
* Customers CRUD - available to every authenticated user.
*
* @property \App\Model\Table\CustomersTable $Customers
*/
class CustomersController extends ApiController
{
/**
* GET /api/customers
*
* Query: page, limit, sort, direction, q (name/email/phone/company/city/tax id),
* type, city, country, with_cars (1 = embed cars)
*
* @return \Cake\Http\Response
*/
public function index(): Response
{
$query = $this->Customers->find();
$q = trim((string)$this->request->getQuery('q', ''));
if ($q !== '') {
$like = '%' . $q . '%';
$query->where(['OR' => [
'Customers.first_name LIKE' => $like,
'Customers.last_name LIKE' => $like,
'Customers.email LIKE' => $like,
'Customers.phone LIKE' => $like,
'Customers.company_name LIKE' => $like,
'Customers.city LIKE' => $like,
'Customers.tax_id LIKE' => $like,
]]);
}
foreach (['type', 'city', 'country'] as $field) {
$value = $this->request->getQuery($field);
if ($value !== null && $value !== '') {
$query->where(["Customers.{$field}" => $value]);
}
}
if (filter_var($this->request->getQuery('with_cars'), FILTER_VALIDATE_BOOLEAN)) {
$query->contain(['Cars']);
}
$result = $this->paginate($query, $this->paginationSettings(
['id', 'type', 'first_name', 'last_name', 'email', 'company_name', 'city', 'country', 'created', 'modified'],
['Customers.id' => 'asc'],
));
return $this->respondPaginated($result);
}
/**
* GET /api/customers/{id} - customer with cars.
*
* @param string $id Customer id
* @return \Cake\Http\Response
*/
public function view(string $id): Response
{
return $this->respond($this->Customers->get($id, contain: ['Cars']));
}
/**
* POST /api/customers
*
* @return \Cake\Http\Response
*/
public function add(): Response
{
$this->request->allowMethod(['post']);
$customer = $this->Customers->newEntity((array)$this->request->getData());
$customer->created_by = $this->currentUser()->id;
if (!$this->Customers->save($customer)) {
return $this->respondValidationError($customer);
}
return $this->respond($customer, 201)
->withLocation('/api/customers/' . $customer->id);
}
/**
* PUT|PATCH /api/customers/{id}
*
* @param string $id Customer id
* @return \Cake\Http\Response
*/
public function edit(string $id): Response
{
$this->request->allowMethod(['put', 'patch']);
$customer = $this->Customers->get($id);
$customer = $this->Customers->patchEntity($customer, (array)$this->request->getData());
if (!$this->Customers->save($customer)) {
return $this->respondValidationError($customer);
}
return $this->respond($customer);
}
/**
* DELETE /api/customers/{id} - also deletes the customer's cars.
*
* @param string $id Customer id
* @return \Cake\Http\Response
*/
public function delete(string $id): Response
{
$this->request->allowMethod(['delete']);
$this->Customers->deleteOrFail($this->Customers->get($id));
return $this->respond(null, 204);
}
}