Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sulu/sulu-workshop
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 844da41415d509e380e92a7533fafabf5c540f04
Choose a base ref
..
head repository: sulu/sulu-workshop
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 04e0084923ad2e0221692fd4e34eb4511963fea6
Choose a head ref
2 changes: 1 addition & 1 deletion config/packages/sulu_admin.yaml
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ sulu_admin:
resources:
events:
routes:
list: app.get_events
list: app.get_event_list
detail: app.get_event

event_registrations:
136 changes: 83 additions & 53 deletions src/Controller/Admin/EventController.php
Original file line number Diff line number Diff line change
@@ -7,26 +7,19 @@
use App\Common\DoctrineListRepresentationFactory;
use App\Entity\Event;
use App\Repository\EventRepository;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sulu\Bundle\MediaBundle\Entity\MediaRepositoryInterface;
use Sulu\Component\Rest\AbstractRestController;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Routing\Annotation\Route;

/**
* @RouteResource("event")
*/
class EventController extends AbstractRestController implements ClassResourceInterface
class EventController extends AbstractController
{
/**
* @var EventRepository
*/
private $repository;
private $eventRepository;

/**
* @var MediaRepositoryInterface
@@ -42,55 +35,60 @@ public function __construct(
EventRepository $repository,
MediaRepositoryInterface $mediaRepository,
DoctrineListRepresentationFactory $doctrineListRepresentationFactory,
ViewHandlerInterface $viewHandler,
?TokenStorageInterface $tokenStorage = null
) {
$this->repository = $repository;
$this->eventRepository = $repository;
$this->mediaRepository = $mediaRepository;
$this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory;

parent::__construct($viewHandler, $tokenStorage);
}

public function cgetAction(Request $request): Response
/**
* @Route("/admin/api/events/{id}", methods={"GET"}, name="app.get_event")
*/
public function getAction(int $id, Request $request): Response
{
$locale = $request->query->get('locale');
$listRepresentation = $this->doctrineListRepresentationFactory->createDoctrineListRepresentation(
Event::RESOURCE_KEY,
[],
['locale' => $locale]
);
$event = $this->load($id, $request);
if (!$event) {
throw new NotFoundHttpException();
}

return $this->handleView($this->view($listRepresentation));
return $this->json($this->getDataForEntity($event));
}

public function getAction(int $id, Request $request): Response
/**
* @Route("/admin/api/events/{id}", methods={"PUT"}, name="app.put_event")
*/
public function putAction(int $id, Request $request): Response
{
$entity = $this->load($id, $request);
if (!$entity) {
$event = $this->load($id, $request);
if (!$event) {
throw new NotFoundHttpException();
}

return $this->handleView($this->view($entity));
$this->mapDataToEntity($request->toArray(), $event);
$this->save($event);

return $this->json($this->getDataForEntity($event));
}

/**
* @Route("/admin/api/events", methods={"POST"}, name="app.post_event")
*/
public function postAction(Request $request): Response
{
$entity = $this->create($request);

$this->mapDataToEntity($request->request->all(), $entity);
$event = $this->create($request);

$this->save($entity);
$this->mapDataToEntity($request->toArray(), $event);
$this->save($event);

return $this->handleView($this->view($entity));
return $this->json($this->getDataForEntity($event), 201);
}

/**
* @Rest\Post("/events/{id}")
* @Route("/admin/api/events/{id}", methods={"POST"}, name="app.post_event_trigger")
*/
public function postTriggerAction(int $id, Request $request): Response
{
$event = $this->repository->findById($id, (string) $this->getLocale($request));
$event = $this->eventRepository->findById($id, (string) $this->getLocale($request));
if (!$event) {
throw new NotFoundHttpException();
}
@@ -104,30 +102,57 @@ public function postTriggerAction(int $id, Request $request): Response
break;
}

$this->repository->save($event);
$this->eventRepository->save($event);

return $this->handleView($this->view($event));
return $this->json($this->getDataForEntity($event));
}

public function putAction(int $id, Request $request): Response
/**
* @Route("/admin/api/events/{id}", methods={"DELETE"}, name="app.delete_event")
*/
public function deleteAction(int $id): Response
{
$entity = $this->load($id, $request);
if (!$entity) {
throw new NotFoundHttpException();
}
$this->remove($id);

$this->mapDataToEntity($request->request->all(), $entity);
return $this->json(null, 204);
}

$this->save($entity);
/**
* @Route("/admin/api/events", methods={"GET"}, name="app.get_event_list")
*/
public function getListAction(Request $request): Response
{
$listRepresentation = $this->doctrineListRepresentationFactory->createDoctrineListRepresentation(
Event::RESOURCE_KEY,
[],
['locale' => $this->getLocale($request)]
);

return $this->handleView($this->view($entity));
return $this->json($listRepresentation->toArray());
}

public function deleteAction(int $id): Response
/**
* @return array<string, mixed>
*/
protected function getDataForEntity(Event $entity): array
{
$this->remove($id);

return $this->handleView($this->view());
$image = $entity->getImage();
$startDate = $entity->getStartDate();
$endDate = $entity->getEndDate();

return [
'id' => $entity->getId(),
'enabled' => $entity->isEnabled(),
'title' => $entity->getTitle(),
'image' => $image
? ['id' => $image->getId()]
: null,
'teaser' => $entity->getTeaser(),
'description' => $entity->getDescription(),
'startDate' => $startDate ? $startDate->format('c') : null,
'endDate' => $endDate ? $endDate->format('c') : null,
'location' => $entity->getLocation(),
];
}

/**
@@ -166,21 +191,26 @@ protected function mapDataToEntity(array $data, Event $entity): void

protected function load(int $id, Request $request): ?Event
{
return $this->repository->findById($id, (string) $this->getLocale($request));
return $this->eventRepository->findById($id, (string) $this->getLocale($request));
}

protected function create(Request $request): Event
{
return $this->repository->create((string) $this->getLocale($request));
return $this->eventRepository->create((string) $this->getLocale($request));
}

protected function save(Event $entity): void
{
$this->repository->save($entity);
$this->eventRepository->save($entity);
}

protected function remove(int $id): void
{
$this->repository->remove($id);
$this->eventRepository->remove($id);
}

public function getLocale(Request $request): ?string
{
return $request->query->has('locale') ? (string) $request->query->get('locale') : null;
}
}
22 changes: 20 additions & 2 deletions src/Controller/Website/EventWebsiteController.php
Original file line number Diff line number Diff line change
@@ -16,9 +16,27 @@

class EventWebsiteController extends AbstractController
{
/**
* @var EventRepository
*/
private $eventRepository;

/**
* @var TemplateAttributeResolverInterface
*/
private $templateAttributeResolver;

public function __construct(
EventRepository $repository,
TemplateAttributeResolverInterface $templateAttributeResolver
) {
$this->eventRepository = $repository;
$this->templateAttributeResolver = $templateAttributeResolver;
}

public function indexAction(int $id, Request $request): Response
{
$event = $this->get(EventRepository::class)->findById($id, $request->getLocale());
$event = $this->eventRepository->findById($id, $request->getLocale());
if (!$event) {
throw new NotFoundHttpException();
}
@@ -50,7 +68,7 @@ public function indexAction(int $id, Request $request): Response

return $this->render(
'events/index.html.twig',
$this->get(TemplateAttributeResolverInterface::class)->resolve(
$this->templateAttributeResolver->resolve(
[
'event' => $event,
'success' => $request->query->get('success'),
31 changes: 0 additions & 31 deletions src/Entity/Event.php
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Sulu\Bundle\MediaBundle\Entity\MediaInterface;

/**
@@ -58,8 +57,6 @@ class Event
* @var Collection<string, EventTranslation>
*
* @ORM\OneToMany(targetEntity="App\Entity\EventTranslation", mappedBy="event", cascade={"ALL"}, indexBy="locale")
*
* @Serializer\Exclude
*/
private $translations;

@@ -72,8 +69,6 @@ class Event
* @var MediaInterface|null
*
* @ORM\ManyToOne(targetEntity="Sulu\Bundle\MediaBundle\Entity\MediaInterface")
*
* @Serializer\Exclude
*/
private $image = null;

@@ -141,33 +136,13 @@ public function getImage(): ?MediaInterface
return $this->image;
}

/**
* @return array<string, mixed>|null
*
* @Serializer\VirtualProperty
* @Serializer\SerializedName("image")
*/
public function getImageData(): ?array
{
if (!$this->image) {
return null;
}

return [
'id' => $this->image->getId(),
];
}

public function setImage(?MediaInterface $image): self
{
$this->image = $image;

return $this;
}

/**
* @Serializer\VirtualProperty(name="title")
*/
public function getTitle(): ?string
{
$translation = $this->getTranslation($this->locale);
@@ -190,9 +165,6 @@ public function setTitle(string $title): self
return $this;
}

/**
* @Serializer\VirtualProperty(name="teaser")
*/
public function getTeaser(): ?string
{
$translation = $this->getTranslation($this->locale);
@@ -215,9 +187,6 @@ public function setTeaser(string $teaser): self
return $this;
}

/**
* @Serializer\VirtualProperty(name="description")
*/
public function getDescription(): ?string
{
$translation = $this->getTranslation($this->locale);
Loading