diff --git a/src/ApiBundle/Controller/DefaultController.php b/src/ApiBundle/Controller/DefaultController.php index 42d3e49d78eeb310d948d647ce4b634e10fe1b7d..ad49e2704f7bd7c6bc1c76d70b7931f65e0546a1 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 3947b192111f6bcc165cdaddf8d5e1d020a8f06e..50731144bcb913c0d546bd452b0f62d21ea72d5a 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