Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
symfony_2504
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Lech Sawoń
symfony_2504
Commits
c1c9ba16
Commit
c1c9ba16
authored
Oct 28, 2021
by
Lech Sawon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
deserialize with symfony serializer
parent
3d21d6ed
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
7 deletions
+17
-7
AddReservationController.php
src/Controller/Api/AddReservationController.php
+9
-7
AddReservationDto.php
src/Dto/AddReservationDto.php
+8
-0
No files found.
src/Controller/Api/AddReservationController.php
View file @
c1c9ba16
...
@@ -13,6 +13,7 @@ use App\Service\Reservation\AddReservation;
...
@@ -13,6 +13,7 @@ use App\Service\Reservation\AddReservation;
use
Symfony\Component\HttpFoundation\JsonResponse
;
use
Symfony\Component\HttpFoundation\JsonResponse
;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\Routing\Annotation\Route
;
use
Symfony\Component\Routing\Annotation\Route
;
use
Symfony\Component\Serializer\SerializerInterface
;
use
Symfony\Component\Validator\Validator\ValidatorInterface
;
use
Symfony\Component\Validator\Validator\ValidatorInterface
;
final
class
AddReservationController
final
class
AddReservationController
...
@@ -21,25 +22,26 @@ final class AddReservationController
...
@@ -21,25 +22,26 @@ final class AddReservationController
* @Route("/api/reservation", name="api_add_reservation", methods={"POST"})
* @Route("/api/reservation", name="api_add_reservation", methods={"POST"})
*/
*/
public
function
__invoke
(
public
function
__invoke
(
// stanowczo za dużo zależności. w mojej ocenie 3 to żółte światło 5 czerwone
Request
$request
,
Request
$request
,
AddReservation
$addReservation
,
AddReservation
$addReservation
,
RoomRepository
$roomRepository
,
RoomRepository
$roomRepository
,
ValidatorInterface
$validator
ValidatorInterface
$validator
,
SerializerInterface
$serializer
)
:
JsonResponse
{
)
:
JsonResponse
{
$array
=
json_decode
(
$request
->
getContent
(),
true
);
//walidacjia danych przy pomocy DTO i symfony validator
$dto
=
$serializer
->
deserialize
(
$request
->
getContent
(),
AddReservationDto
::
class
,
'json'
);
$dto
=
AddReservationDto
::
createFromArray
(
$array
);
$errors
=
$validator
->
validate
(
$dto
);
$errors
=
$validator
->
validate
(
$dto
);
if
(
count
(
$errors
)
>
0
)
{
if
(
count
(
$errors
)
>
0
)
{
// zwracane błędy powinny być bardziej czytelne, teraz jest to uproszczone
$errorsString
=
(
string
)
$errors
;
$errorsString
=
(
string
)
$errors
;
return
new
JsonResponse
(
$errorsString
);
return
new
JsonResponse
(
$errorsString
);
}
}
// ten kod mógłby być bardziej elegancki, ale jako przykład jest good enough
// ten kod mógłby być bardziej elegancki, ale jako przykład jest good enough
// moglibyśmy np przekazać DTO do serwisu i tam zająć się znalezieniem Room i zapisemr
// moglibyśmy np przekazać DTO do serwisu i tam zająć się znalezieniem Room i zapisem
// w ramach ćwiczeń można przeprowadzić refactoring przenoszący to co poniżej do serwisu
$room
=
$roomRepository
->
find
(
$dto
->
getRoomId
());
$room
=
$roomRepository
->
find
(
$dto
->
getRoomId
());
...
...
src/Dto/AddReservationDto.php
View file @
c1c9ba16
...
@@ -5,16 +5,24 @@ declare(strict_types=1);
...
@@ -5,16 +5,24 @@ declare(strict_types=1);
namespace
App\Dto
;
namespace
App\Dto
;
use
Symfony\Component\Validator\Constraints
as
Assert
;
use
Symfony\Component\Validator\Constraints
as
Assert
;
use
Symfony\Component\Serializer\Annotation\SerializedName
;
final
class
AddReservationDto
final
class
AddReservationDto
{
{
/**
/**
* @Assert\GreaterThan(0)
* @Assert\GreaterThan(0)
* @SerializedName("room_id")
*/
*/
private
int
$roomId
;
private
int
$roomId
;
/**
* @SerializedName("date_from")
*/
private
string
$dateFrom
;
private
string
$dateFrom
;
/**
* @SerializedName("date_to")
*/
private
string
$dateTo
;
private
string
$dateTo
;
public
function
__construct
(
int
$roomId
,
string
$dateFrom
,
string
$dateTo
)
public
function
__construct
(
int
$roomId
,
string
$dateFrom
,
string
$dateTo
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment