Last active
August 23, 2019 20:42
-
-
Save chrisguitarguy/88cf096adadb470aae066fcca2d17d6f to your computer and use it in GitHub Desktop.
Revisions
-
chrisguitarguy revised this gist
Aug 23, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ <?php use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -
chrisguitarguy revised this gist
Aug 23, 2019 . 2 changed files with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ use Symfony\Component\Serializer\Annotation\Groups; class ClientDto extends Dto { /** * @Groups(Dto::GROUP_DEFAULT) This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Validator\Constraints as Assert; class ClientDto extends Dto { /** * @Groups(Dto::GROUP_DEFAULT) -
chrisguitarguy revised this gist
Aug 23, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,7 @@ public function createClientAction(Request $request) : Response $client = $this->createAndStoreClientFromDto($dto); return $this->json(ClientDto::fromClient($client), Response::HTTP_CREATED, [ 'Location' => $this->generateUrl('clients.view', [ 'clientId' => $client->getId(), ]), -
chrisguitarguy revised this gist
Aug 23, 2019 . 1 changed file with 38 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class ClientController extends AbstractController { // ... public function createClientAction(Request $request) : Response { // probably want to validate that the request is JSON and such here... // this will throw a NotEncodableValueException from the serializer component // if the JSON is invalid. May want to catch and convert to a bad request // exception. $dto = $this->get('serializer')->deserialize( $request->getContent(), ClientDto::class, 'json', ['groups' => Dto::GROUP_CREATE] ); $validationErrors = $this->get('validator')->validate($dto, null, [Dto::GROUP_CREATE]); if (count($validationError) > 0) { return $this->json($validationErrors, Response::HTTP_BAD_REQUEST); } $client = $this->createAndStoreClientFromDto($dto); return $this->json($client, Response::HTTP_CREATED, [ 'Location' => $this->generateUrl('clients.view', [ 'clientId' => $client->getId(), ]), ], [ 'groups' => Dto::GROUP_DEFAULT, ]); } } -
chrisguitarguy revised this gist
Aug 23, 2019 . 1 changed file with 29 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ <?php use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Validator\Constraints as Assert; class ClientDto { /** * @Groups(Dto::GROUP_DEFAULT) * @var string */ public $id; /** * @Groups({Dto::GROUP_DEFAULT, Dto::GROUP_CREATE}) * @Assert\NotBlank(groups={Dto::GROUP_CREATE, Dto::GROUP_UPDATE}) * @Assert\Type(type="string", groups={Dto::GROUP_CREATE, Dto::GROUP_UPDATE}) * @var string */ public $name; /** * @Groups({Dto::GROUP_DEFAULT, Dto::GROUP_CREATE, Dto::GROUP_UPDATE}) * @Assert\NotBlank(groups={Dto::GROUP_CREATE, Dto::GROUP_UPDATE}) * @Assert\Type(type="string", groups={Dto::GROUP_CREATE, Dto::GROUP_UPDATE}) * @Assert\Url(protocols={"http", "https"}, groups={Dto::GROUP_CREATE, Dto::GROUP_UPDATE}) * @var string */ public $site_url; } -
chrisguitarguy revised this gist
Aug 23, 2019 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ <?php use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; class ClientController extends AbstractController { public function viewClientAction(string $clientId) : Response { $client = $this->getClientOr404($clientId); return $this->json(ClientDto::fromClient($client), Response::HTTP_OK, [], [ 'groups' => Dto::GROUP_DEFAULT, ]); } } -
chrisguitarguy revised this gist
Aug 23, 2019 . 1 changed file with 21 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ <?php use Symfony\Component\Serializer\Annotation\Groups; class ClientDto { /** * @Groups(Dto::GROUP_DEFAULT) */ public $id; /** * @Groups({Dto::GROUP_DEFAULT, Dto::GROUP_CREATE}) */ public $name; /** * @Groups({Dto::GROUP_DEFAULT, Dto::GROUP_CREATE, Dto::GROUP_UPDATE}) */ public $site_url; } -
chrisguitarguy revised this gist
Aug 23, 2019 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ abstract class Dto { public const GROUP_DEFAULT = 'default'; public const GROUP_CREATE = 'create'; public const GROUP_UPDATE = 'update'; } -
chrisguitarguy revised this gist
Aug 23, 2019 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ <?php abstract class Dto { const GROUP_DEFAULT = 'default'; const GROUP_CREATE = 'create'; const GROUP_UPDATE = 'update'; } -
chrisguitarguy revised this gist
Aug 23, 2019 . 1 changed file with 13 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ <?php use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class ClientController extends AbstractController { public function viewClientAction(string $clientId) : Response { $client = $this->getClientOr404($clientId); return $this->json(ClientDto::fromClient($client)); } } -
chrisguitarguy created this gist
Aug 23, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ <?php class ClientDto { public $id; public $name; public $site_url; }
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ <?php class Client { private $id; private $name; private $siteUrl; // constructor, getters, etc }