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

postDreamAction #158

Open
wants to merge 3 commits into
base: rest_api
Choose a base branch
from
Open
Changes from 2 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
93 changes: 93 additions & 0 deletions src/Geekhub/DreamBundle/Controller/Api/DreamController.php
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Request\ParamFetcher;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;

class DreamController extends FOSRestController
{
@@ -127,4 +128,96 @@ public function getDreamAction($slug)

return $dream;
}

/**
* Create dream
*
* @ApiDoc(
* resource = true,
* description = "Create single dream",
* parameters={
* {"name"="title", "dataType"="string", "required"=true, "description"="Dream name"},
* {"name"="description", "dataType"="string", "required"=true, "description"="Description about dream"},
* {"name"="phone", "dataType"="integer", "required"=true, "description"="Phone number", "format"="(xxx) xxx xxx xxx"},
* {"name"="dreamEquipmentResources", "dataType"="array<Geekhub\DreamBundle\Entity\EquipmentResource>", "required"=true, "description"="Equipment resources"},
* {"name"="dreamWorkResources", "dataType"="array<Geekhub\DreamBundle\Entity\WorkResource>", "required"=true, "description"="Work resources"},
* {"name"="dreamFinancialResources", "dataType"="array<Geekhub\DreamBundle\Entity\FinancialResource>", "required"=true, "description"="Financial resources"}
* },
* output = "string",
* statusCodes = {
* 201 = "Dream sucessful created",
* 400 = "When dream not created"
* },
* section="Post Dream"
* )
*
* @param Request $request
*
* @return mixed
*/
public function postDreamAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$data = $request->request->all();
$user = $this->getUser();
$data = $this->get('serializer')->serialize($data, 'json');
$dream = $this->get('serializer')->deserialize($data, 'Geekhub\DreamBundle\Entity\Dream', 'json');
$dream->setAuthor($user);
$em->persist($dream);
$em->flush();
$restView = View::create();
$restView->setStatusCode(201);
$restView->setData([
"link" => $this->get('router')->generate('get_dream', ['slug' => $dream->getSlug()], true),
]);
return $restView;
}

/**
* Update existing dream from the submitted data or create a new dream at a specific location.
*
* @ApiDoc(
* resource = true,
* description = "Create/Update single dream",
* parameters={
* {"name"="title", "dataType"="string", "required"=true, "description"="Dream name"},
* {"name"="description", "dataType"="string", "required"=true, "description"="Description about dream"},
* {"name"="phone", "dataType"="integer", "required"=true, "description"="Phone number", "format"="(xxx) xxx xxx xxx"},
* {"name"="dreamFinancialResources", "dataType"="array<AppBundle\Document\EquipmentResource>", "required"=true, "description"="Equipment resources"},
* {"name"="dreamWorkResources", "dataType"="array<AppBundle\Document\WorkResource>", "required"=true, "description"="Work resources"},
* {"name"="dreamFinancialResources", "dataType"="array<AppBundle\Document\FinancialResource>", "required"=true, "description"="Financial resources"}
* },
* section="Put Dream",
* statusCodes = {
* 200 = "Dream successful update",
* 404 = "Return when dream with current slug not isset"
* }
* )
*
*
* @param Request $request the request object
* @param string $slug the page id
* @return mixed
*/
public function putDreamAction(Request $request, $slug)
{
$data = $request->request->all();
$em = $this->getDoctrine()->getManager();
$dreamOld = $em->getRepository('GeekhubDreamBundle:Dream')
->findOneBySlug($slug);
$data = $this->get('serializer')->serialize($data, 'json');
$dreamNew = $this->get('serializer')->deserialize($data, 'Geekhub\DreamBundle\Entity\Dream', 'json');
$view = View::create();
if (!$dreamOld) {
$dreamNew->setAuthor($this->getUser());
$em->persist($dreamNew);
$em->flush();
$view->setStatusCode(404);
} else {
$this->get('services.object_updater_class')->updateObject($dreamOld, $dreamNew);
$em->flush();
$view->setStatusCode(200);
}
return $view;
}
}
Empty file.
5 changes: 5 additions & 0 deletions src/Geekhub/DreamBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
<parameter key="geekhub.dream.dream_subscriber.class">Geekhub\DreamBundle\EventListener\DreamSubscriber</parameter>
<parameter key="dream.twig.contribution_extension.class">Geekhub\DreamBundle\Twig\DreamExtension</parameter>
<parameter key="paginator_service.class">Geekhub\DreamBundle\Service\PaginatorService</parameter>
<parameter key="services.object_updater_class">Geekhub\DreamBundle\Service\ObjectUpdater</parameter>
</parameters>

<services>
@@ -33,6 +34,10 @@
<argument type="service" id="doctrine" />
<tag name="twig.extension" />
</service>

<service id="services.object_updater_class" class="%services.object_updater_class%">

</service>
</services>

</container>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add empty line at the end, or use php-cs-fixer

26 changes: 26 additions & 0 deletions src/Geekhub/DreamBundle/Service/ObjectUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Geekhub\DreamBundle\Service;

use Symfony\Component\PropertyAccess\PropertyAccessor;

class ObjectUpdater
{
public function updateObject($objectOld, $objectNew)
{
if (get_class($objectOld) != get_class($objectNew)) {
throw new \Exception('class not equals');
}
$accessor = new PropertyAccessor();
$reflect = new \ReflectionClass($objectOld);
$properties = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PRIVATE);
foreach ($properties as $property) {
$propertyName = $property->getName();
$newValue = $accessor->getValue($objectNew, $propertyName);
if ($newValue !== null) {
$accessor->setValue($objectOld, $propertyName, $newValue);
}
}
return $objectOld;
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add empty line at the end