From 072c1b5cb843cb2fb800aafca94bc3a2c98366d8 Mon Sep 17 00:00:00 2001 From: Jeremy Guiselin <jeremy.guiselin@student.ecp.fr> Date: Tue, 17 Jan 2017 14:04:10 +0100 Subject: [PATCH] save purchase --- .../Controller/DefaultController.php | 28 ++++++++++++- .../Repository/LeagueOnDeviceRepository.php | 41 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/ApiBundle/Controller/DefaultController.php b/src/ApiBundle/Controller/DefaultController.php index 42d3e49..ad49e27 100644 --- a/src/ApiBundle/Controller/DefaultController.php +++ b/src/ApiBundle/Controller/DefaultController.php @@ -153,7 +153,28 @@ class DefaultController extends FOSRestController } - $view->setStatusCode(500); + $view->setStatusCode(400); + return $this->handleView($view); + } + + public function savePurchaseAction(Request $request) + { + $view = View::create(); + + if ($request->getMethod() === 'POST') { + $content = (array)\GuzzleHttp\json_decode($request->getContent()); + $deviceRepository = $this->getDeviceRepository(); + $device = $deviceRepository->findOneBy(['uuid' => $content['uuid']]); + $notificationRepository = $this->getLeagueOnDeviceRepository(); + try { + $notificationRepository->savePurchase($device, $content['transactionId'], $content['leagues']); + } catch (\Exception $e) { + $view->setStatusCode(400); + $this->getLogger()->err($e->getMessage()); + } + } + + $view->setStatusCode(403); return $this->handleView($view); } @@ -230,6 +251,11 @@ class DefaultController extends FOSRestController return $this->getDoctrine()->getRepository('BackendBundle:League'); } + private function getLeagueOnDeviceRepository() + { + return $this->getDoctrine()->getRepository('BackendBundle:LeagueOnDevice'); + } + private function getLogger() { return $this->get('logger'); diff --git a/src/BackendBundle/Repository/LeagueOnDeviceRepository.php b/src/BackendBundle/Repository/LeagueOnDeviceRepository.php index 3947b19..5073114 100644 --- a/src/BackendBundle/Repository/LeagueOnDeviceRepository.php +++ b/src/BackendBundle/Repository/LeagueOnDeviceRepository.php @@ -9,9 +9,50 @@ namespace BackendBundle\Repository; +use BackendBundle\Entity\Device; +use BackendBundle\Entity\LeagueOnDevice; +use BackendBundle\Entity\Transaction; use Doctrine\ORM\EntityRepository; class LeagueOnDeviceRepository extends EntityRepository { + public function savePurchase(Device $device, string $transactionId, array $leagues) + { + $em = $this->getEntityManager(); + $qb = $this->getEntityManager()->getRepository('BackendBundle:League')->createQueryBuilder('l'); + $ids = array_map(function ($el) { + return $el['id']; + }, $leagues); + $qb + ->select('l') + ->where('l.id IN (:ids)') + ->setParameter('ids', $ids); + $leagues = $qb->getQuery()->getResult(); + + foreach ($leagues as $league) { + $lod = $this->findOneBy([ + 'device' => $device, + 'league' => $league + ]); + + if ($lod) { + $lod->setStatus(LeagueOnDevice::STATUS_UNLOCK); + } else { + $lod = new LeagueOnDevice(); + $lod->setDevice($device); + $lod->setLeague($league); + $em->persist($lod); + } + } + + $transaction = new Transaction(); + $transaction->setStoreTransactionId($transactionId); + $transaction->setDate(new \DateTime()); + $transaction->setDevice($device); + $em->persist($transaction); + + $em->flush(); + + } } \ No newline at end of file -- GitLab