Skip to content

Commit

Permalink
🎉 feat: Creating people
Browse files Browse the repository at this point in the history
  • Loading branch information
leocavalcante committed Aug 15, 2023
1 parent 41c2021 commit ebd4690
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ APP_NAME=rinha-de-backend
APP_ENV=dev

DB_DRIVER=mysql
DB_HOST=localhost
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=opencodeco
DB_USERNAME=root
Expand Down
5 changes: 3 additions & 2 deletions app/Controller/PersonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ final class PersonController
{
public function create(PersonRequest $request, ResponseInterface $response): MessageResponseInterface
{
$person = $request->validated();

$person = $request->toPerson();
$person->saveOrFail();

return $response->json($person)->withStatus(201);
}

Expand Down
39 changes: 39 additions & 0 deletions app/Model/Person.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
*/
class Person extends Model
{
public bool $timestamps = false;

/**
* The table associated with the model.
*/
protected ?string $table = 'person';

/**
* The attributes that are mass assignable.
*/
protected array $fillable = [
'id',
'nick',
'name',
'birth',
'stack',
];

/**
* The attributes that should be cast to native types.
*/
protected array $casts = [];

protected array $attributes = [
'stack' => '[]',
];
}
16 changes: 16 additions & 0 deletions app/Request/PersonRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace App\Request;

use App\Model\Person;
use Hyperf\Codec\Json;
use Hyperf\Validation\Request\FormRequest;

final class PersonRequest extends FormRequest
Expand All @@ -34,6 +36,20 @@ public function rules(): array
'apelido' => 'required|max:32',
'nome' => 'required|max:100',
'nascimento' => 'required|date_format:Y-m-d',
'stack' => 'array'
];
}

public function toPerson(): Person
{
$data = $this->validated();

return Person::create([
'id' => random_bytes(16),
'nick' => $data['apelido'],
'name' => $data['nome'],
'birth' => $data['nascimento'],
'stack' => Json::encode($data['stack'] ?? null),
]);
}
}
20 changes: 19 additions & 1 deletion test/Cases/CreatePersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function testCreatePersonWithInvalidBirthDate(): void
assertSame(422, $response->getStatusCode());
}

public function testCreatePerson(): void
public function testCreatePersonWithNullStack(): void
{
/** @var Response $response */
$response = $this->request('POST', '/pessoas', [
Expand All @@ -158,4 +158,22 @@ public function testCreatePerson(): void

assertSame(201, $response->getStatusCode(), $response->getBody()->getContents());
}

public function testCreatePersonWithSomeStack(): void
{
/** @var Response $response */
$response = $this->request('POST', '/pessoas', [
'headers' => [
'Content-Type' => 'application/json',
],
'json' => [
'apelido' => 'opencodeco',
'nome' => 'OpenCodeCo',
'nascimento' => '2023-07-01',
'stack' => ['PHP', 'Swoole', 'Hyperf'],
],
]);

assertSame(201, $response->getStatusCode(), $response->getBody()->getContents());
}
}

0 comments on commit ebd4690

Please sign in to comment.