Skip to content

Commit 0286cd5

Browse files
committed
Api person method create and getByUuid
1 parent 1e8319c commit 0286cd5

File tree

12 files changed

+126
-200
lines changed

12 files changed

+126
-200
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ php artisan create-db
1515

1616
php artisan migrate:install
1717

18-
php artisan migrate
18+
php artisan migrate
19+
20+
php artisan db:seed

app/Api/V1/Person/Contracts/PersonRepositoryInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ interface PersonRepositoryInterface
1414
*/
1515
public function getAll(): Collection;
1616

17+
/**
18+
* @param string $uuid
19+
* @return PersonModel|null
20+
*/
21+
public function getByUuid(string $uuid): ?PersonModel;
22+
1723
/**
1824
* @param array $data
1925
* @return PersonModel

app/Api/V1/Person/Controllers/PersonController.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Api\V1\Person\Contracts\PersonRepositoryInterface;
66
use App\Api\V1\Person\Providers\PersonServiceProvider;
77
use App\Enum\HttpResponseStatusCodeEnum;
8+
use App\Enum\ResponseEnum;
89
use App\Http\Controllers\Controller;
910
use App\Utils\HttpResponseUtils;
1011
use Illuminate\Http\Request;
@@ -37,7 +38,11 @@ public function __construct(PersonRepositoryInterface $person_repository)
3738
*/
3839
public function index(Request $request)
3940
{
40-
$response_type = ($request->response_type) ?: 'json';
41+
$response_type = 'json';
42+
43+
if (isset($request->response_type)) {
44+
$response_type = $request->response_type;
45+
}
4146

4247
$persons = $this->person_repository->getAll()->toArray();
4348

@@ -50,15 +55,32 @@ public function index(Request $request)
5055
}
5156

5257
/**
53-
* Show the form for creating a new resource.
58+
* Display the specified resource.
59+
*
60+
* @param string $uuid
61+
* @param Request $request
5462
*
5563
* @return \Illuminate\Http\Response
5664
*/
57-
public function create()
65+
public function show(string $uuid, Request $request)
5866
{
59-
//
67+
$response_type = 'json';
68+
69+
if (isset($request->response_type)) {
70+
$response_type = $request->response_type;
71+
}
72+
73+
$person = $this->person_repository->getByUuid($uuid)->toArray();
74+
75+
if ($response_type !== 'json') {
76+
$person = self::httpResponse($person, $response_type);
77+
}
78+
79+
return response($person, HttpResponseStatusCodeEnum::OK)
80+
->header("Content-Type", "text/{$response_type}");
6081
}
6182

83+
6284
/**
6385
* Store a newly created resource in storage.
6486
*
@@ -67,7 +89,20 @@ public function create()
6789
*/
6890
public function store(Request $request)
6991
{
70-
//
92+
$response_type = 'json';
93+
94+
if (isset($request->response_type)) {
95+
$response_type = $request->response_type;
96+
}
97+
98+
$person = $this->person_repository->create($request->all())->toArray();
99+
100+
if ($response_type !== 'json') {
101+
$person = self::httpResponse($person, $response_type);
102+
}
103+
104+
return response($person, HttpResponseStatusCodeEnum::OK)
105+
->header("Content-Type", "text/{$response_type}");
71106
}
72107

73108

app/Api/V1/Person/Repositories/PersonRepository.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Enum\HttpResponseStatusCodeEnum;
99
use App\Enum\ResponseEnum;
1010
use App\Utils\HelperUtils;
11+
use App\Utils\NumberUtils;
1112
use DB;
1213
use Illuminate\Database\Eloquent\Collection;
1314

@@ -22,6 +23,21 @@ public function getAll(): Collection
2223
return PersonModel::all();
2324
}
2425

26+
/**
27+
* @param string $uuid
28+
* @return PersonModel|null
29+
*/
30+
public function getByUuid(string $uuid): ?PersonModel
31+
{
32+
$person = PersonModel::find($uuid);
33+
34+
if (empty($person)) {
35+
abort(HttpResponseStatusCodeEnum::NOT_FOUND, ResponseEnum::NOT_FOUND);
36+
}
37+
38+
return $person;
39+
}
40+
2541
/**
2642
* @param array $data
2743
* @return PersonModel
@@ -69,25 +85,31 @@ public function delete(string $id): bool
6985
*/
7086
private function savePerson(array $data, ?string $id = null): PersonModel
7187
{
72-
if (!HelperUtils::validateFields($data, ['nome'])) {
88+
if (!HelperUtils::validateFields($data, ['nome', 'cpf', 'data_nascimento'])) {
7389
abort(HttpResponseStatusCodeEnum::NOT_FOUND, ResponseEnum::DATA_IS_NULL);
7490
}
7591

76-
$person = PersonModel::where([
77-
['nome', $data['nome']]
78-
])->first();
79-
80-
if ($person instanceof PersonModel && $person->id != $id) {
81-
abort(HttpResponseStatusCodeEnum::BAD_REQUEST, ResponseEnum::ALREADY_EXISTS);
92+
if (NumberUtils::validateCpf($data['cpf'])) {
93+
abort(HttpResponseStatusCodeEnum::BAD_REQUEST, ResponseEnum::DOCUMENT_NOT_VALID);
8294
}
8395

96+
// $person = PersonModel::where([
97+
// ['nome', $data['nome']]
98+
// ])->first();
99+
//
100+
// if ($person instanceof PersonModel && $person->id != $id) {
101+
// abort(HttpResponseStatusCodeEnum::BAD_REQUEST, ResponseEnum::ALREADY_EXISTS);
102+
// }
103+
84104
if (is_null($id)) {
85105
$person = new PersonModel();
86106
} else if (!$person = PersonModel::find($id)) {
87107
abort(HttpResponseStatusCodeEnum::NOT_FOUND, ResponseEnum::NOT_FOUND);
88108
}
89109

90110
$person->nome = HelperUtils::array_get($data, 'nome', $person->nome ?: null);
111+
$person->cpf = HelperUtils::array_get($data, 'cpf', $person->cpf ?: null);
112+
$person->data_nascimento = HelperUtils::array_get($data, 'data_nascimento', $person->data_nascimento ?: null);
91113
$person->save();
92114

93115
return $person;

app/Enum/ResponseEnum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ abstract class ResponseEnum
1919
const ALREADY_EXISTS = 'Already exists';
2020
const HAS_RELATION = 'Has relation';
2121
const DATA_IS_NULL = 'Data is null';
22+
const DOCUMENT_NOT_VALID = 'Document is not valid';
2223
}

app/Exceptions/Handler.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
namespace App\Exceptions;
44

5+
use App\Utils\HttpResponseUtils;
56
use Exception;
67
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
8+
use Illuminate\Support\Facades\Log;
79

810
class Handler extends ExceptionHandler
911
{
12+
use HttpResponseUtils;
13+
1014
/**
1115
* A list of the exception types that are not reported.
1216
*
@@ -31,7 +35,7 @@ class Handler extends ExceptionHandler
3135
*
3236
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
3337
*
34-
* @param \Exception $exception
38+
* @param \Exception $exception
3539
* @return void
3640
*/
3741
public function report(Exception $exception)
@@ -42,12 +46,22 @@ public function report(Exception $exception)
4246
/**
4347
* Render an exception into an HTTP response.
4448
*
45-
* @param \Illuminate\Http\Request $request
46-
* @param \Exception $exception
49+
* @param \Illuminate\Http\Request $request
50+
* @param \Exception $exception
4751
* @return \Illuminate\Http\Response
4852
*/
4953
public function render($request, Exception $exception)
5054
{
51-
return parent::render($request, $exception);
55+
// Log::info($exception->getCode());
56+
// if ($exception->getCode() === 0 && !isset($exception->getStatusCode()) || $exception->getStatusCode() === 0) {
57+
// return parent::render($request, $exception);
58+
// }
59+
60+
$code = (($exception->getCode()) ?: ($exception->httpStatusCode))
61+
62+
return response()->json(
63+
HttpResponseUtils::httpClientError($exception->getMessage()),
64+
($exception->getCode() !== 0) ? $exception->getCode() : $exception->getStatusCode()
65+
);
5266
}
5367
}

app/Http/Controllers/Auth/ForgotPasswordController.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

app/Http/Controllers/Auth/LoginController.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

app/Http/Controllers/Auth/RegisterController.php

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy