From ffe226122fe545b5d1b5141402eec51f8a9a295b Mon Sep 17 00:00:00 2001 From: shubaivan Date: Sun, 29 Mar 2015 15:48:27 +0300 Subject: [PATCH 1/3] postDreamAction --- .../Controller/Api/DreamController.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/Geekhub/DreamBundle/Controller/Api/DreamController.php b/src/Geekhub/DreamBundle/Controller/Api/DreamController.php index 44ff8ea7..c69cdcf6 100644 --- a/src/Geekhub/DreamBundle/Controller/Api/DreamController.php +++ b/src/Geekhub/DreamBundle/Controller/Api/DreamController.php @@ -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,48 @@ 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", "required"=true, "description"="Equipment resources"}, + * {"name"="dreamWorkResources", "dataType"="array", "required"=true, "description"="Work resources"}, + * {"name"="dreamFinancialResources", "dataType"="array", "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; + } } From f67e629b641ccdb9c1f38a6d9a402debf0562c77 Mon Sep 17 00:00:00 2001 From: shubaivan Date: Sun, 29 Mar 2015 16:52:25 +0300 Subject: [PATCH 2/3] add post method for dream --- .../Controller/Api/DreamController.php | 52 ++++++++++++++++++- .../Resources/config/routing/api.yml | 0 .../DreamBundle/Resources/config/services.xml | 5 ++ .../DreamBundle/Service/ObjectUpdater.php | 26 ++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) delete mode 100644 src/Geekhub/DreamBundle/Resources/config/routing/api.yml create mode 100644 src/Geekhub/DreamBundle/Service/ObjectUpdater.php diff --git a/src/Geekhub/DreamBundle/Controller/Api/DreamController.php b/src/Geekhub/DreamBundle/Controller/Api/DreamController.php index c69cdcf6..b52bdb7d 100644 --- a/src/Geekhub/DreamBundle/Controller/Api/DreamController.php +++ b/src/Geekhub/DreamBundle/Controller/Api/DreamController.php @@ -159,10 +159,10 @@ public function postDreamAction(Request $request) { $em = $this->getDoctrine()->getManager(); $data = $request->request->all(); -// $user = $this->getUser(); + $user = $this->getUser(); $data = $this->get('serializer')->serialize($data, 'json'); $dream = $this->get('serializer')->deserialize($data, 'Geekhub\DreamBundle\Entity\Dream', 'json'); -// $dream->setAuthor($user); + $dream->setAuthor($user); $em->persist($dream); $em->flush(); $restView = View::create(); @@ -172,4 +172,52 @@ public function postDreamAction(Request $request) ]); 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", "required"=true, "description"="Equipment resources"}, + * {"name"="dreamWorkResources", "dataType"="array", "required"=true, "description"="Work resources"}, + * {"name"="dreamFinancialResources", "dataType"="array", "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; + } } diff --git a/src/Geekhub/DreamBundle/Resources/config/routing/api.yml b/src/Geekhub/DreamBundle/Resources/config/routing/api.yml deleted file mode 100644 index e69de29b..00000000 diff --git a/src/Geekhub/DreamBundle/Resources/config/services.xml b/src/Geekhub/DreamBundle/Resources/config/services.xml index 26acea05..4e83941c 100644 --- a/src/Geekhub/DreamBundle/Resources/config/services.xml +++ b/src/Geekhub/DreamBundle/Resources/config/services.xml @@ -8,6 +8,7 @@ Geekhub\DreamBundle\EventListener\DreamSubscriber Geekhub\DreamBundle\Twig\DreamExtension Geekhub\DreamBundle\Service\PaginatorService + Geekhub\DreamBundle\Service\ObjectUpdater @@ -33,6 +34,10 @@ + + + + \ No newline at end of file diff --git a/src/Geekhub/DreamBundle/Service/ObjectUpdater.php b/src/Geekhub/DreamBundle/Service/ObjectUpdater.php new file mode 100644 index 00000000..3d52e9ca --- /dev/null +++ b/src/Geekhub/DreamBundle/Service/ObjectUpdater.php @@ -0,0 +1,26 @@ +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; + } +} \ No newline at end of file From 3c1614414c856cc6934b0dd8ca1baf55d39f43b7 Mon Sep 17 00:00:00 2001 From: shubaivan Date: Wed, 8 Apr 2015 23:13:47 +0300 Subject: [PATCH 3/3] cs fix --- src/Geekhub/DreamBundle/Controller/Api/DreamController.php | 4 +++- src/Geekhub/DreamBundle/Model/AbstractPagination.php | 1 - src/Geekhub/DreamBundle/Model/DreamsResponse.php | 1 - .../config/serializer/Entity.AbstractContributeResource.yml | 1 - src/Geekhub/DreamBundle/Resources/config/services.xml | 3 ++- src/Geekhub/DreamBundle/Service/ObjectUpdater.php | 3 ++- src/Geekhub/DreamBundle/Service/PaginatorService.php | 3 --- .../DreamBundle/Tests/Services/PaginationServiceTest.php | 1 - src/Geekhub/ResourceBundle/Controller/Api/FaqController.php | 2 +- src/Geekhub/UserBundle/Controller/Api/UserController.php | 2 +- src/Geekhub/UserBundle/Resources/config/routing.yml | 2 -- src/Geekhub/UserBundle/Resources/config/routing/api.yml | 2 +- .../UserBundle/Resources/config/serializer/Entity.User.yml | 2 +- 13 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/Geekhub/DreamBundle/Controller/Api/DreamController.php b/src/Geekhub/DreamBundle/Controller/Api/DreamController.php index b52bdb7d..854ba05d 100644 --- a/src/Geekhub/DreamBundle/Controller/Api/DreamController.php +++ b/src/Geekhub/DreamBundle/Controller/Api/DreamController.php @@ -50,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') @@ -170,6 +170,7 @@ public function postDreamAction(Request $request) $restView->setData([ "link" => $this->get('router')->generate('get_dream', ['slug' => $dream->getSlug()], true), ]); + return $restView; } @@ -218,6 +219,7 @@ public function putDreamAction(Request $request, $slug) $em->flush(); $view->setStatusCode(200); } + return $view; } } diff --git a/src/Geekhub/DreamBundle/Model/AbstractPagination.php b/src/Geekhub/DreamBundle/Model/AbstractPagination.php index c20f0db5..8cc02a57 100644 --- a/src/Geekhub/DreamBundle/Model/AbstractPagination.php +++ b/src/Geekhub/DreamBundle/Model/AbstractPagination.php @@ -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 diff --git a/src/Geekhub/DreamBundle/Model/DreamsResponse.php b/src/Geekhub/DreamBundle/Model/DreamsResponse.php index 3d183d33..66ea4152 100644 --- a/src/Geekhub/DreamBundle/Model/DreamsResponse.php +++ b/src/Geekhub/DreamBundle/Model/DreamsResponse.php @@ -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 diff --git a/src/Geekhub/DreamBundle/Resources/config/serializer/Entity.AbstractContributeResource.yml b/src/Geekhub/DreamBundle/Resources/config/serializer/Entity.AbstractContributeResource.yml index b4ec94e3..56c174b9 100644 --- a/src/Geekhub/DreamBundle/Resources/config/serializer/Entity.AbstractContributeResource.yml +++ b/src/Geekhub/DreamBundle/Resources/config/serializer/Entity.AbstractContributeResource.yml @@ -10,4 +10,3 @@ Geekhub\DreamBundle\Entity\AbstractContributeResource: dream: expose: true type: array - diff --git a/src/Geekhub/DreamBundle/Resources/config/services.xml b/src/Geekhub/DreamBundle/Resources/config/services.xml index 4e83941c..d563f261 100644 --- a/src/Geekhub/DreamBundle/Resources/config/services.xml +++ b/src/Geekhub/DreamBundle/Resources/config/services.xml @@ -38,6 +38,7 @@ + - \ No newline at end of file + diff --git a/src/Geekhub/DreamBundle/Service/ObjectUpdater.php b/src/Geekhub/DreamBundle/Service/ObjectUpdater.php index 3d52e9ca..aa60c4ae 100644 --- a/src/Geekhub/DreamBundle/Service/ObjectUpdater.php +++ b/src/Geekhub/DreamBundle/Service/ObjectUpdater.php @@ -21,6 +21,7 @@ public function updateObject($objectOld, $objectNew) $accessor->setValue($objectOld, $propertyName, $newValue); } } + return $objectOld; } -} \ No newline at end of file +} diff --git a/src/Geekhub/DreamBundle/Service/PaginatorService.php b/src/Geekhub/DreamBundle/Service/PaginatorService.php index 0897b211..a594f352 100644 --- a/src/Geekhub/DreamBundle/Service/PaginatorService.php +++ b/src/Geekhub/DreamBundle/Service/PaginatorService.php @@ -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; diff --git a/src/Geekhub/DreamBundle/Tests/Services/PaginationServiceTest.php b/src/Geekhub/DreamBundle/Tests/Services/PaginationServiceTest.php index 5705c197..f8f53558 100644 --- a/src/Geekhub/DreamBundle/Tests/Services/PaginationServiceTest.php +++ b/src/Geekhub/DreamBundle/Tests/Services/PaginationServiceTest.php @@ -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 { diff --git a/src/Geekhub/ResourceBundle/Controller/Api/FaqController.php b/src/Geekhub/ResourceBundle/Controller/Api/FaqController.php index dbe1ad97..9883e058 100644 --- a/src/Geekhub/ResourceBundle/Controller/Api/FaqController.php +++ b/src/Geekhub/ResourceBundle/Controller/Api/FaqController.php @@ -83,4 +83,4 @@ public function getFaqAction($slug) return $faq; } -} \ No newline at end of file +} diff --git a/src/Geekhub/UserBundle/Controller/Api/UserController.php b/src/Geekhub/UserBundle/Controller/Api/UserController.php index 7e0b7a09..30a7c351 100644 --- a/src/Geekhub/UserBundle/Controller/Api/UserController.php +++ b/src/Geekhub/UserBundle/Controller/Api/UserController.php @@ -38,4 +38,4 @@ public function getUserAction() return $user; } -} \ No newline at end of file +} diff --git a/src/Geekhub/UserBundle/Resources/config/routing.yml b/src/Geekhub/UserBundle/Resources/config/routing.yml index 577b9963..333493af 100644 --- a/src/Geekhub/UserBundle/Resources/config/routing.yml +++ b/src/Geekhub/UserBundle/Resources/config/routing.yml @@ -70,5 +70,3 @@ profile_view: profile_view_dreams: pattern: /users/{id}/dreams/{status} defaults: { _controller: GeekhubUserBundle:User:userOwnedDreamsView} - - diff --git a/src/Geekhub/UserBundle/Resources/config/routing/api.yml b/src/Geekhub/UserBundle/Resources/config/routing/api.yml index 7cb937df..c591c007 100644 --- a/src/Geekhub/UserBundle/Resources/config/routing/api.yml +++ b/src/Geekhub/UserBundle/Resources/config/routing/api.yml @@ -1,3 +1,3 @@ geek_user: type: rest - resource: "@GeekhubUserBundle/Controller/Api/UserController.php" \ No newline at end of file + resource: "@GeekhubUserBundle/Controller/Api/UserController.php" diff --git a/src/Geekhub/UserBundle/Resources/config/serializer/Entity.User.yml b/src/Geekhub/UserBundle/Resources/config/serializer/Entity.User.yml index 3c30c9cc..76741bf1 100644 --- a/src/Geekhub/UserBundle/Resources/config/serializer/Entity.User.yml +++ b/src/Geekhub/UserBundle/Resources/config/serializer/Entity.User.yml @@ -37,4 +37,4 @@ Geekhub\UserBundle\Entity\User: # workContributions: # expose: true otherContributions: - expose: true \ No newline at end of file + expose: true