Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code challenge 5 solution #5

Open
wants to merge 1 commit into
base: challenge-5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CODING-CHALLENGE-6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# RESTful Webservices in Symfony

## Coding Challenge 6 - POST vs. PUT

### Tasks

- implement controllers to create and update attendees and workshops
- both should be possible with JSON and XML

### Solution

- use an `ArgumentValueResolver` (or alternativly the new `#[MapRequestPayload]` attribute) to deserialize the request's content
- use the `*Repository`s to save the object into the database
- *CREATE:* use HTTP method `POST`, return an HTTP 201 (Created) status code and
set the `Location` header with the help of the `UrlGenerator`
- *UPDATE:* use HTTP method `PUT`, return an HTTP 204 (No Content) status code and leave the response body empty
14 changes: 14 additions & 0 deletions CODING-CHALLENGE-7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# RESTful Webservices in Symfony

## Coding Challenge 7 - Validation

### Tasks

- introduce Symfony's Validator to validate the request content used to create and update workshops and attendees

### Solution

- require the Symfony Validator component: `composer require validator`
- add validation constraints to your model properties (e.g. NotBlank, Email)
- inject the `Validator` Service in your `ValueResolver`s
- for now throw an `UnprocessableEntityHttpException` on validation errors
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"symfony/runtime": "6.3.*",
"symfony/serializer": "6.3.*",
"symfony/yaml": "6.3.*",
"webmozart/assert": "^1.11"
"webmozart/assert": "^1.11",
"willdurand/negotiation": "^3.1"
},
"config": {
"allow-plugins": {
Expand Down
112 changes: 84 additions & 28 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/Controller/Attendee/ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ public function __invoke(Request $request): Response
$request->query->getInt('size', 10)
);

$serializedAttendeeCollection = $this->serializer->serialize($attendeeCollection, 'json');
$serializedAttendeeCollection = $this->serializer->serialize($attendeeCollection, $request->getRequestFormat());

return new Response($serializedAttendeeCollection, Response::HTTP_OK, [
'Content-Type' => 'application/json',
]);
return new Response($serializedAttendeeCollection, Response::HTTP_OK);
}
}
9 changes: 4 additions & 5 deletions src/Controller/Attendee/ReadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Controller\Attendee;

use App\Entity\Attendee;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
Expand All @@ -17,12 +18,10 @@ public function __construct(
) {
}

public function __invoke(Attendee $attendee): Response
public function __invoke(Request $request, Attendee $attendee): Response
{
$serializedAttendee = $this->serializer->serialize($attendee, 'json');
$serializedAttendee = $this->serializer->serialize($attendee, $request->getRequestFormat());

return new Response($serializedAttendee, Response::HTTP_OK, [
'Content-Type' => 'application/json',
]);
return new Response($serializedAttendee, Response::HTTP_OK);
}
}
6 changes: 2 additions & 4 deletions src/Controller/Workshop/ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ public function __invoke(Request $request): Response
$request->query->getInt('size', 10)
);

$serializedWorkshopCollection = $this->serializer->serialize($workshopCollection, 'json');
$serializedWorkshopCollection = $this->serializer->serialize($workshopCollection, $request->getRequestFormat());

return new Response($serializedWorkshopCollection, Response::HTTP_OK, [
'Content-Type' => 'application/json',
]);
return new Response($serializedWorkshopCollection, Response::HTTP_OK);
}
}
9 changes: 4 additions & 5 deletions src/Controller/Workshop/ReadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Controller\Workshop;

use App\Entity\Workshop;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
Expand All @@ -17,12 +18,10 @@ public function __construct(
) {
}

public function __invoke(Workshop $workshop): Response
public function __invoke(Request $request, Workshop $workshop): Response
{
$serializedWorkshop = $this->serializer->serialize($workshop, 'json');
$serializedWorkshop = $this->serializer->serialize($workshop, $request->getRequestFormat());

return new Response($serializedWorkshop, Response::HTTP_OK, [
'Content-Type' => 'application/json',
]);
return new Response($serializedWorkshop, Response::HTTP_OK);
}
}
Loading
Loading