From 4cbc56b3a671e19a4ed8ac1e243b5befafc4fe71 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Mon, 14 Aug 2023 15:16:11 -0300 Subject: [PATCH 1/4] chore: Start application --- participantes/OpenCodeCo/README.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 participantes/OpenCodeCo/README.md diff --git a/participantes/OpenCodeCo/README.md b/participantes/OpenCodeCo/README.md new file mode 100644 index 00000000..c8f890ea --- /dev/null +++ b/participantes/OpenCodeCo/README.md @@ -0,0 +1,5 @@ +# Swoole & Hyperf + +GitHub: [@opencodeco](https://github.com/opencodeco) +Twitter: [@opencodeco](https://github.com/opencodeco) +Repositório: [github.com/opencodeco/rinha-de-backend-2023-q3](https://github.com/opencodeco/rinha-de-backend-2023-q3) From 99b44d9bb91fd6850c629324d3d79c49934c9dad Mon Sep 17 00:00:00 2001 From: Leo Cavalcante Date: Mon, 14 Aug 2023 15:33:54 -0300 Subject: [PATCH 2/4] chore(docs): Styling --- participantes/OpenCodeCo/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/participantes/OpenCodeCo/README.md b/participantes/OpenCodeCo/README.md index c8f890ea..08b07342 100644 --- a/participantes/OpenCodeCo/README.md +++ b/participantes/OpenCodeCo/README.md @@ -1,5 +1,5 @@ # Swoole & Hyperf -GitHub: [@opencodeco](https://github.com/opencodeco) -Twitter: [@opencodeco](https://github.com/opencodeco) -Repositório: [github.com/opencodeco/rinha-de-backend-2023-q3](https://github.com/opencodeco/rinha-de-backend-2023-q3) +- GitHub: [@opencodeco](https://github.com/opencodeco) +- Twitter: [@opencodeco](https://github.com/opencodeco) +- Repositório: [github.com/opencodeco/rinha-de-backend-2023-q3](https://github.com/opencodeco/rinha-de-backend-2023-q3) (branch `dev`) From 19b9d62e841390b1aa87fd9d572ee2163adacf4c Mon Sep 17 00:00:00 2001 From: Douglas Medeiros Date: Fri, 18 Aug 2023 22:37:08 -0300 Subject: [PATCH 3/4] fix tests and refactor --- .env.example | 2 +- Makefile | 4 +- app/Consumer/PersonConsumer.php | 10 ++++- app/Controller/PersonController.php | 42 +++++++------------ app/Exception/Handler/AppExceptionHandler.php | 1 + app/Job/PersonJob.php | 20 +++++---- app/Listener/DbQueryExecutedListener.php | 10 ++--- .../ResumeExitCoordinatorListener.php | 1 + app/Model/Model.php | 1 + app/Model/Person.php | 22 +++++----- app/Request/PersonRequest.php | 10 ++--- composer.json | 2 +- config/autoload/dependencies.php | 7 ++++ config/routes.php | 2 + docs/user.rest | 22 ++++++++++ test/Cases/CreatePersonTest.php | 19 +++++---- test/HttpTestCase.php | 22 +++++++--- 17 files changed, 122 insertions(+), 75 deletions(-) create mode 100644 docs/user.rest diff --git a/.env.example b/.env.example index 5ea8b32b..5933a689 100644 --- a/.env.example +++ b/.env.example @@ -11,7 +11,7 @@ DB_CHARSET=utf8mb4 DB_COLLATION=utf8mb4_unicode_ci DB_PREFIX= -REDIS_HOST=localhost +REDIS_HOST=cache REDIS_AUTH=(null) REDIS_PORT=6379 REDIS_DB=0 diff --git a/Makefile b/Makefile index bb232e78..9298ebc6 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ default: build push cp .env.example .env vendor/autoload.php: - docker compose exec app composer install + docker compose exec api1 composer install .PHONY: build build: @@ -22,4 +22,4 @@ up: .env .PHONY: test test: vendor/autoload.php - docker compose exec app composer test + docker compose exec api1 composer test diff --git a/app/Consumer/PersonConsumer.php b/app/Consumer/PersonConsumer.php index 836b770d..1cf4b445 100644 --- a/app/Consumer/PersonConsumer.php +++ b/app/Consumer/PersonConsumer.php @@ -2,6 +2,15 @@ declare(strict_types=1); +/** + * This file is part of OpenCodeCo. + * + * @link https://github.com/opencodeco/rinha-de-backend-2023-q3 + * @document https://github.com/opencodeco/rinha-de-backend-2023-q3/wiki + * @contact https://github.com/opencodeco/rinha-de-backend-2023-q3/discussions + * @license https://github.com/opencodeco/rinha-de-backend-2023-q3/blob/dev/LICENSE + */ + namespace App\Consumer; use Hyperf\AsyncQueue\Process\ConsumerProcess; @@ -10,5 +19,4 @@ #[Process(name: "async-queue")] class PersonConsumer extends ConsumerProcess { - } \ No newline at end of file diff --git a/app/Controller/PersonController.php b/app/Controller/PersonController.php index cd34d727..3d6e4927 100644 --- a/app/Controller/PersonController.php +++ b/app/Controller/PersonController.php @@ -1,6 +1,7 @@ driver = $driverFactory->get('default'); } - /** - * @throws \Throwable - */ public function create(PersonRequest $request, ResponseInterface $response): MessageResponseInterface { $person = $request->toPerson(); - if ($this->redis->get($person['nick'])) { + if ($this->cache->get($person['nick'])) { return $response->json(['message' => 'Esse apelido já existe'])->withStatus(422); } - $this->driver->push(new PersonJob($person)); - -// + $this->queue->push(new PersonJob($person)); - $this->redis->set($person['nick'], '.'); - $this->redis->set($person['id'], json_encode($person)); + $this->cache->set($person['nick'], '.'); + $this->cache->set($person['id'], json_encode($person)); return $response->json($person)->withStatus(201)->withHeader('Location', "/pessoas/{$person['id']}"); } public function show(RequestInterface $request, ResponseInterface $response, string $id): MessageResponseInterface { - $cached = $this->redis->get($id); - if ($cached) { - - $person = json_decode($cached); - - return $response->json($person); + if ($cached = $this->cache->get($id)) { + return $response->json(json_decode($cached)); } - $response->raw('Not found')->withStatus(404); + + return $response->raw('Not found')->withStatus(404); } public function search(RequestInterface $request, ResponseInterface $response): MessageResponseInterface @@ -85,7 +74,6 @@ public function search(RequestInterface $request, ResponseInterface $response): public function count(RequestInterface $request, ResponseInterface $response): MessageResponseInterface { - $count = Person::count(); - return $response->json(['count' => $count]); + return $response->json(['count' => Person::count()]); } } diff --git a/app/Exception/Handler/AppExceptionHandler.php b/app/Exception/Handler/AppExceptionHandler.php index 79572ec9..9db3eb42 100644 --- a/app/Exception/Handler/AppExceptionHandler.php +++ b/app/Exception/Handler/AppExceptionHandler.php @@ -1,6 +1,7 @@ params = $params; + public function __construct( + public readonly array $params + ) { } - public function handle() + public function handle(): void { Db::table('person')->insert($this->params); } diff --git a/app/Listener/DbQueryExecutedListener.php b/app/Listener/DbQueryExecutedListener.php index ba84fd08..92a8f269 100644 --- a/app/Listener/DbQueryExecutedListener.php +++ b/app/Listener/DbQueryExecutedListener.php @@ -1,6 +1,7 @@ sql; - if (! Arr::isAssoc($event->bindings)) { + if (!Arr::isAssoc($event->bindings)) { $position = 0; foreach ($event->bindings as $value) { $position = strpos($sql, '?', $position); diff --git a/app/Listener/ResumeExitCoordinatorListener.php b/app/Listener/ResumeExitCoordinatorListener.php index 59b669b2..a257a70d 100644 --- a/app/Listener/ResumeExitCoordinatorListener.php +++ b/app/Listener/ResumeExitCoordinatorListener.php @@ -1,6 +1,7 @@ 'string', 'stack' => 'array' ]; - + protected array $attributes = [ 'stack' => '[]', ]; diff --git a/app/Request/PersonRequest.php b/app/Request/PersonRequest.php index 756b2ad3..049da998 100644 --- a/app/Request/PersonRequest.php +++ b/app/Request/PersonRequest.php @@ -1,6 +1,7 @@ validated(); - + $data = $this->all(); + return [ 'id' => Uuid::uuid4()->toString(), 'nick' => $data['apelido'], diff --git a/composer.json b/composer.json index 697f5800..d74cbdaa 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "description": "Swoole e Hyperf na Rinha de Back-end.", "license": "MIT", "require": { - "php": ">=8.0", + "php": ">=8.2", "hyperf/async-queue": "^3.0", "hyperf/cache": "^3.0", "hyperf/command": "^3.0", diff --git a/config/autoload/dependencies.php b/config/autoload/dependencies.php index 55aa4211..4981b235 100644 --- a/config/autoload/dependencies.php +++ b/config/autoload/dependencies.php @@ -1,6 +1,7 @@ + fn (ContainerInterface $c) => $c->make(DriverFactory::class)->get('default') ]; diff --git a/config/routes.php b/config/routes.php index 8e9c5fcd..86a00acd 100644 --- a/config/routes.php +++ b/config/routes.php @@ -1,6 +1,7 @@ 'application/json', ], 'json' => [ - 'apelido' => 'opencodeco', + 'apelido' => $this->randString(), 'nome' => '', 'nascimento' => '2023-07-01', ], @@ -82,7 +82,7 @@ public function testCreatePersonWithNullName(): void 'Content-Type' => 'application/json', ], 'json' => [ - 'apelido' => 'opencodeco', + 'apelido' => $this->randString(), 'nome' => null, 'nascimento' => '2023-07-01', ], @@ -99,7 +99,7 @@ public function testCreatePersonWithEmptyBirthDate(): void 'Content-Type' => 'application/json', ], 'json' => [ - 'apelido' => 'opencodeco', + 'apelido' => $this->randString(), 'nome' => 'OpenCodeCo', 'nascimento' => '', ], @@ -116,7 +116,7 @@ public function testCreatePersonWithNullBirthDate(): void 'Content-Type' => 'application/json', ], 'json' => [ - 'apelido' => 'opencodeco', + 'apelido' => $this->randString(), 'nome' => 'OpenCodeCo', 'nascimento' => null, ], @@ -133,7 +133,7 @@ public function testCreatePersonWithInvalidBirthDate(): void 'Content-Type' => 'application/json', ], 'json' => [ - 'apelido' => 'opencodeco', + 'apelido' => $this->randString(), 'nome' => 'OpenCodeCo', 'nascimento' => '01-07-2023', ], @@ -150,7 +150,7 @@ public function testCreatePersonWithNullStack(): void 'Content-Type' => 'application/json', ], 'json' => [ - 'apelido' => 'opencodeco', + 'apelido' => $this->randString(), 'nome' => 'OpenCodeCo', 'nascimento' => '2023-07-01', ], @@ -167,7 +167,7 @@ public function testCreatePersonWithSomeStack(): void 'Content-Type' => 'application/json', ], 'json' => [ - 'apelido' => 'opencodeco', + 'apelido' => $this->randString(), 'nome' => 'OpenCodeCo', 'nascimento' => '2023-07-01', 'stack' => ['PHP', 'Swoole', 'Hyperf'], @@ -176,4 +176,9 @@ public function testCreatePersonWithSomeStack(): void assertSame(201, $response->getStatusCode(), $response->getBody()->getContents()); } + + protected function randString(): string + { + return str_pad(bin2hex(random_bytes(10)), 10); + } } diff --git a/test/HttpTestCase.php b/test/HttpTestCase.php index aa09182b..dba49de3 100644 --- a/test/HttpTestCase.php +++ b/test/HttpTestCase.php @@ -1,6 +1,7 @@ client = make(Client::class); + $this->client = \Hyperf\Support\make(Client::class); } public function __call($name, $arguments) { return $this->client->{$name}(...$arguments); } + + protected function setUp(): void + { + parent::setUp(); + Db::beginTransaction(); + } + + protected function tearDown(): void + { + Db::rollBack(); + parent::tearDown(); + } } From cd9ced393762bfb919c11ba52d7c42a3cc9de5d4 Mon Sep 17 00:00:00 2001 From: Leo Cavalcante Date: Fri, 18 Aug 2023 23:53:55 -0300 Subject: [PATCH 4/4] chore: Remove unused file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Esse arquivo da participação ficaria só na `main`. --- participantes/OpenCodeCo/README.md | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 participantes/OpenCodeCo/README.md diff --git a/participantes/OpenCodeCo/README.md b/participantes/OpenCodeCo/README.md deleted file mode 100644 index 08b07342..00000000 --- a/participantes/OpenCodeCo/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Swoole & Hyperf - -- GitHub: [@opencodeco](https://github.com/opencodeco) -- Twitter: [@opencodeco](https://github.com/opencodeco) -- Repositório: [github.com/opencodeco/rinha-de-backend-2023-q3](https://github.com/opencodeco/rinha-de-backend-2023-q3) (branch `dev`)