Skip to content

Commit c5e1b84

Browse files
committed
Add api path to retrieve current user
1 parent 42e57de commit c5e1b84

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\Controller\API;
4+
5+
use App\Entity\Card;
6+
use App\Entity\UserCard;
7+
use App\Entity\UserNotification;
8+
use Symfony\Component\Routing\Annotation\Route;
9+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10+
use Symfony\Component\HttpFoundation\JsonResponse;
11+
use Symfony\Component\HttpFoundation\Request;
12+
13+
class APITempController extends AbstractController
14+
{
15+
16+
17+
/**
18+
* @Route("/api/temp/start_transmission", name="api.temp.start_transmission")
19+
* @Route("/api/temp/start_transmission/")
20+
*/
21+
public function startTransmission(Request $request) {
22+
$em = $this->getDoctrine()->getManager();
23+
$user = $this->getUser();
24+
25+
if(!$user) {
26+
$response = new JsonResponse(['version' => $this->getParameter('api_version'), 'status' => 403, 'data' => []]);
27+
return $response;
28+
}
29+
30+
$token = $request->query->get('token');
31+
if(base64_encode(date('H')) != $token) {
32+
$response = new JsonResponse(['version' => $this->getParameter('api_version'), 'status' => 403, 'data' => []]);
33+
return $response;
34+
}
35+
36+
// Give Card
37+
$card = $em->getRepository(Card::class)->findOneBy(array('id' => 54));
38+
$existingUserCard = $em->getRepository(UserCard::class)->findOneBy(array('card' => $card,'user' => $user));
39+
if($existingUserCard == null) {
40+
$newUserCard = new UserCard();
41+
$newUserCard->setCard($card);
42+
$newUserCard->setUser($user);
43+
$newUserCard->setGivenDate(new \DateTime());
44+
45+
$em->persist($newUserCard);
46+
47+
$newNotification = new UserNotification();
48+
$newNotification->setUser($user);
49+
$newNotification->setNotificationType(3);
50+
$newNotification->setNotificationData("");
51+
$newNotification->setConnectedCard($newUserCard->getCard());
52+
$newNotification->setConnectedUser($user);
53+
54+
$em->persist($newNotification);
55+
$em->flush();
56+
}
57+
58+
return new JsonResponse(['version' => $this->getParameter('api_version'), 'status' => 200, 'data' => []]);
59+
}
60+
}

src/Controller/API/APIUserController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,21 @@ public function userDetailSpinPlays(Request $request, int $userId)
191191
return $response;
192192
}
193193
}
194+
195+
196+
/**
197+
* @Route("/api/user/current", name="api.users.current")
198+
* @Route("/api/user/current/")
199+
*/
200+
public function userCurrent(Request $request) {
201+
$user = $this->getUser();
202+
203+
if(!$user) {
204+
$response = new JsonResponse(['version' => $this->getParameter('api_version'), 'status' => 403, 'data' => []]);
205+
return $response;
206+
}
207+
208+
$response = new JsonResponse(['version' => $this->getParameter('api_version'), 'status' => 200, 'data' => $user->getJSON()]);
209+
return $response;
210+
}
194211
}

0 commit comments

Comments
 (0)