Last active
July 14, 2020 17:52
-
-
Save jasny/615f5ea74a3a1c02d5b661124d652d1c to your computer and use it in GitHub Desktop.
Example of a CRUD controllers for new framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Jasny\DB\Option\Functions as opts; | |
use Jasny\HttpAttributes\Request\Accept; | |
use Jasny\HttpAttributes\Request\ParsedBody; | |
use Jasny\HttpAttributes\Request\PathParam; | |
use Jasny\HttpAttributes\Response\ContentType; | |
use Jasny\HttpAttributes\Route\Delete; | |
use Jasny\HttpAttributes\Route\Get; | |
use Jasny\HttpAttributes\Route\Post; | |
use Jasny\Persist\DataMapper; | |
use Jasny\Persist\NotFoundException; | |
use Jasny\Validation\ValidationException; | |
use Psr\HttpMessage\ResponseInterface as Prs7Response; | |
/** | |
* Create, read, update, delete a client. | |
*/ | |
final class CrudClient extends Controller | |
{ | |
/** | |
* @param DataMapper<Client> $clients | |
*/ | |
public function __construct ( | |
DataMapper $this->clients, | |
) { | |
$this->handle( | |
fn(NotFoundException $_) => $this->produces('text/plain')->notFound('Client not found')), | |
fn(ValidationException $exception) => $this->badRequest($exception->getErrors())), | |
); | |
} | |
/** | |
* Create a new client. | |
*/ | |
@@Post("/clients") | |
@@Accept("application/json") | |
public function create(@@ParsedBody array $input): Psr7Response | |
{ | |
$client = $this->clients->create(); | |
$client->set($input); | |
$this->clients->save($client); | |
return $this->created("/clients/" . $client->id); | |
} | |
/** | |
* Return the information of a single client. | |
*/ | |
@@Get("/clients/:id") | |
@@ContentType("application/json") | |
public function read(@@PathParam("id") Client $client): Psr7Response | |
{ | |
return $this->output($client); | |
} | |
/** | |
* Modify an existing client | |
*/ | |
@@Post("/clients/:id") | |
@@Accept("application/json") | |
public function update( | |
@@PathParam("id") Client $client, | |
@@ParsedBody array $input, | |
): void { | |
$client->set($input); | |
$this->clients->save($client); | |
} | |
/** | |
* Delete a client. | |
*/ | |
@@Delete("/clients/:id") | |
public function create(@@PathParam("id") Client $client): void | |
{ | |
$this->clients->delete($client); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Services\CustomerReader; | |
use Jasny\HttpAttributes\Request\Accept; | |
use Jasny\HttpAttributes\Request\ParsedBody; | |
use Jasny\HttpAttributes\Request\QueryParam; | |
use Jasny\HttpAttributes\Response\ContentType; | |
use Jasny\HttpAttributes\Request; | |
use Jasny\HttpAttributes\Route\Get; | |
use Psr\HttpMessage\ResponseInterface as Prs7Response; | |
/** | |
* Get a list of clients. | |
*/ | |
final class ListClients extends Controller | |
{ | |
public function __construct ( | |
ClientReader $this->clients, | |
) { } | |
@@Get("/clients") | |
@@Consumes("application/json") | |
public function _ ( | |
@@Request\ParsedBody array $filter, | |
@@Request\QueryParam("page") int $page = 1, | |
@@Request\QueryParam("sort") string $sort = "name", | |
): Prs7Response { | |
$result = $this->clients->fetch( | |
$filter, | |
opts\omit('password') | |
opts\sort($sort), | |
opts\page($page, 25), | |
opts\hydrate('plan_id'), | |
opts\lookup('orders')->where(['completed' => 0])->as('running_orders'), | |
opts\lookup('orders')->where(['completed' => 1])->limit(3), | |
opts\lookup('orders')->count()->as('order_count'), | |
) | |
->map(fn(array $client) => $client['type'] = ($client['order_count'] === 0 ? 'client' : 'prospect')); | |
return $this->output($result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment