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
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
97 changes: 96 additions & 1 deletion src/Geekhub/DreamBundle/Controller/Api/DreamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -49,7 +50,7 @@ public function getDreamsAction(ParamFetcher $paramFetcher)

$repository = $manager->getRepository('GeekhubDreamBundle:Dream');

if(!$paramFetcher->get('status')){
if (!$paramFetcher->get('status')) {
$queryBuilder = $repository->createQueryBuilder('dream')
->where('dream.currentStatus != :identifier1','dream.currentStatus != :identifier2')
->setParameter('identifier1', 'fail')
Expand Down Expand Up @@ -127,4 +128,98 @@ 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;
}
}
1 change: 0 additions & 1 deletion src/Geekhub/DreamBundle/Model/AbstractPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Type;
use Geekhub\DreamBundle\Model\PaginationInterface;

/**
* Class AbstractPagination
Expand Down
1 change: 0 additions & 1 deletion src/Geekhub/DreamBundle/Model/DreamsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Type;
use Geekhub\DreamBundle\Model\AbstractPagination;

/**
* Class DreamResponse
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ Geekhub\DreamBundle\Entity\AbstractContributeResource:
dream:
expose: true
type: array<Geekhub\DreamBundle\Entity\Dream>

8 changes: 7 additions & 1 deletion src/Geekhub/DreamBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -33,6 +34,11 @@
<argument type="service" id="doctrine" />
<tag name="twig.extension" />
</service>

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

</service>

</services>

</container>
</container>
27 changes: 27 additions & 0 deletions src/Geekhub/DreamBundle/Service/ObjectUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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;
}
}
3 changes: 0 additions & 3 deletions src/Geekhub/DreamBundle/Service/PaginatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Geekhub\DreamBundle\Service;

use Doctrine\Common\Persistence\ObjectManager;
use FOS\RestBundle\Request\ParamFetcher;
use Geekhub\DreamBundle\Entity\Dream;
use Geekhub\DreamBundle\Model\DreamsResponse;
use Symfony\Bundle\FrameworkBundle\Routing\Router;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
use Geekhub\DreamBundle\Model\DreamsResponse;
use Geekhub\DreamBundle\Service\PaginatorService;

class PaginationServiceTest extends WebTestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ public function getFaqAction($slug)

return $faq;
}
}
}
2 changes: 1 addition & 1 deletion src/Geekhub/UserBundle/Controller/Api/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ public function getUserAction()

return $user;
}
}
}
2 changes: 0 additions & 2 deletions src/Geekhub/UserBundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,3 @@ profile_view:
profile_view_dreams:
pattern: /users/{id}/dreams/{status}
defaults: { _controller: GeekhubUserBundle:User:userOwnedDreamsView}


2 changes: 1 addition & 1 deletion src/Geekhub/UserBundle/Resources/config/routing/api.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
geek_user:
type: rest
resource: "@GeekhubUserBundle/Controller/Api/UserController.php"
resource: "@GeekhubUserBundle/Controller/Api/UserController.php"
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ Geekhub\UserBundle\Entity\User:
# workContributions:
# expose: true
otherContributions:
expose: true
expose: true