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
3d21d6ed
Commit
3d21d6ed
authored
Oct 27, 2021
by
Lech Sawon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dto in controller
parent
202320dd
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
71 additions
and
5 deletions
+71
-5
services.yaml
config/services.yaml
+1
-0
AddReservationController.php
src/Controller/Api/AddReservationController.php
+20
-5
AddReservationDto.php
src/Dto/AddReservationDto.php
+50
-0
No files found.
config/services.yaml
View file @
3d21d6ed
...
@@ -21,6 +21,7 @@ services:
...
@@ -21,6 +21,7 @@ services:
-
'
../src/Kernel.php'
-
'
../src/Kernel.php'
-
'
../src/Tests/'
-
'
../src/Tests/'
-
'
../src/Service/'
-
'
../src/Service/'
-
'
../src/Dto'
# controllers are imported separately to make sure services can be injected
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
# as action arguments even if you don't extend any base controller class
...
...
src/Controller/Api/AddReservationController.php
View file @
3d21d6ed
...
@@ -5,6 +5,7 @@ declare(strict_types=1);
...
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace
App\Controller\Api
;
namespace
App\Controller\Api
;
use
App\Dto\AddReservationDto
;
use
App\Entity\Reservation
;
use
App\Entity\Reservation
;
use
App\Entity\Room
;
use
App\Entity\Room
;
use
App\Repository\RoomRepository
;
use
App\Repository\RoomRepository
;
...
@@ -12,6 +13,7 @@ use App\Service\Reservation\AddReservation;
...
@@ -12,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\Validator\Validator\ValidatorInterface
;
final
class
AddReservationController
final
class
AddReservationController
{
{
...
@@ -21,12 +23,25 @@ final class AddReservationController
...
@@ -21,12 +23,25 @@ final class AddReservationController
public
function
__invoke
(
public
function
__invoke
(
Request
$request
,
Request
$request
,
AddReservation
$addReservation
,
AddReservation
$addReservation
,
RoomRepository
$roomRepository
RoomRepository
$roomRepository
,
ValidatorInterface
$validator
)
:
JsonResponse
{
)
:
JsonResponse
{
$array
=
json_decode
(
$request
->
getContent
(),
true
);
//walidacjia danych przy pomocy DTO i symfony validator
$dto
=
AddReservationDto
::
createFromArray
(
$array
);
$errors
=
$validator
->
validate
(
$dto
);
if
(
count
(
$errors
)
>
0
)
{
$errorsString
=
(
string
)
$errors
;
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
$array
=
json_decode
(
$request
->
getContent
(),
true
);
// moglibyśmy np przekazać DTO do serwisu i tam zająć się znalezieniem Room i zapisemr
$room
=
$roomRepository
->
find
(
$array
[
'roomId'
]);
$room
=
$roomRepository
->
find
(
$dto
->
getRoomId
());
if
(
!
$room
instanceof
Room
)
{
if
(
!
$room
instanceof
Room
)
{
//było by lepiej użyć własnego exception
//było by lepiej użyć własnego exception
...
@@ -37,8 +52,8 @@ final class AddReservationController
...
@@ -37,8 +52,8 @@ final class AddReservationController
$reservation
$reservation
->
setRoom
(
$room
)
->
setRoom
(
$room
)
->
setDateFrom
(
new
\DateTime
(
$
array
[
'dateFrom'
]
))
->
setDateFrom
(
new
\DateTime
(
$
dto
->
getDateFrom
()
))
->
setDateEnd
(
new
\DateTime
(
$
array
[
'dateTo'
]
))
->
setDateEnd
(
new
\DateTime
(
$
dto
->
getDateFrom
()
))
;
;
$addReservation
->
add
(
$reservation
);
$addReservation
->
add
(
$reservation
);
...
...
src/Dto/AddReservationDto.php
0 → 100644
View file @
3d21d6ed
<?php
declare
(
strict_types
=
1
);
namespace
App\Dto
;
use
Symfony\Component\Validator\Constraints
as
Assert
;
final
class
AddReservationDto
{
/**
* @Assert\GreaterThan(0)
*/
private
int
$roomId
;
private
string
$dateFrom
;
private
string
$dateTo
;
public
function
__construct
(
int
$roomId
,
string
$dateFrom
,
string
$dateTo
)
{
$this
->
roomId
=
$roomId
;
$this
->
dateFrom
=
$dateFrom
;
$this
->
dateTo
=
$dateTo
;
}
public
function
getRoomId
()
:
int
{
return
$this
->
roomId
;
}
public
function
getDateFrom
()
:
string
{
return
$this
->
dateFrom
;
}
public
function
getDateTo
()
:
string
{
return
$this
->
dateTo
;
}
public
static
function
createFromArray
(
array
$array
)
:
self
{
return
new
self
(
$array
[
'roomId'
],
$array
[
'dateFrom'
],
$array
[
'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