1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\DTO\DtoEvent; |
6
|
|
|
use AppBundle\Entity\Event; |
7
|
|
|
use AppBundle\Entity\User; |
8
|
|
|
use AppBundle\Exception\JsonHttpException; |
9
|
|
|
use AppBundle\Form\EventType; |
10
|
|
|
use Mcfedr\JsonFormBundle\Controller\JsonController; |
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @Route("/schedule/events") |
18
|
|
|
*/ |
19
|
|
|
class CalendarController extends JsonController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @Route("/") |
23
|
|
|
* @Method("GET") |
24
|
|
|
* |
25
|
|
|
* @return JsonResponse |
26
|
|
|
*/ |
27
|
|
|
public function eventsListAction(Request $request) |
28
|
|
|
{ |
29
|
|
|
return new JsonResponse($this->get('app.google_calendar') |
30
|
|
|
->getEventList($request->query->all())); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Request $request |
35
|
|
|
* @Route("") |
36
|
|
|
* @Method("POST") |
37
|
|
|
* |
38
|
|
|
* @return JsonResponse |
39
|
|
|
*/ |
40
|
|
|
public function newEventAction(Request $request) |
41
|
|
|
{ |
42
|
|
|
$dtoEvent = new DtoEvent(); |
43
|
|
|
$form = $this->createForm(EventType::class, $dtoEvent); |
44
|
|
|
$this->handleJsonForm($form, $request); |
45
|
|
|
$result = $this->get('app.google_calendar') |
46
|
|
|
->createEvent($dtoEvent, $request->query->all()); |
47
|
|
|
if (!$result) { |
48
|
|
|
throw new JsonHttpException(412, 'Event has not been created'); |
49
|
|
|
} |
50
|
|
|
$em = $this->getDoctrine()->getManager(); |
51
|
|
|
/** @var User $user */ |
52
|
|
|
$user = $em->getRepository('AppBundle:User') |
53
|
|
|
->find($dtoEvent->getUser()); |
54
|
|
|
|
55
|
|
|
if (!$user) { |
56
|
|
|
throw new JsonHttpException(404, 'User not found.'); |
57
|
|
|
} |
58
|
|
|
$event = new Event(); |
59
|
|
|
$event->setGoogleId($result->id); |
60
|
|
|
$event->addUser($user); |
61
|
|
|
$user->setEvent($event); |
62
|
|
|
|
63
|
|
|
$em->persist($user); |
64
|
|
|
$em->flush(); |
65
|
|
|
|
66
|
|
|
return $this->json(['success' => 'Event Created']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $id |
71
|
|
|
* @Route("/{id}") |
72
|
|
|
* @Method("GET") |
73
|
|
|
* |
74
|
|
|
* @return JsonResponse |
75
|
|
|
*/ |
76
|
|
|
public function singleEventAction($id) |
77
|
|
|
{ |
78
|
|
|
/** @var Event $event */ |
79
|
|
|
$event = $this->getDoctrine()->getRepository('AppBundle:Event') |
80
|
|
|
->findByGoogleId($id); |
81
|
|
|
$user = $event->getUsers()->first(); |
|
|
|
|
82
|
|
|
if (!$user) { |
83
|
|
|
throw new JsonHttpException(404, 'User not found.'); |
84
|
|
|
} |
85
|
|
|
$googleEvent = $this->get('app.google_calendar') |
86
|
|
|
->getEventById($id); |
87
|
|
|
if (!$googleEvent) { |
88
|
|
|
throw new JsonHttpException(404, 'Event not found'); |
89
|
|
|
} |
90
|
|
|
$user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]); |
91
|
|
|
return new JsonResponse(['user' => $user, 'event' => $googleEvent]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @Route("/user/{id}") |
96
|
|
|
* @Method("GET") |
97
|
|
|
* |
98
|
|
|
* @return JsonResponse |
99
|
|
|
*/ |
100
|
|
|
public function userEventsAction($id) |
101
|
|
|
{ |
102
|
|
|
$user = $this->getDoctrine()->getRepository('AppBundle:User')->find($id); |
103
|
|
|
$events = $user->getEvents(); |
104
|
|
|
$googleEvents = []; |
105
|
|
|
$calendar = $this->get('app.google_calendar'); |
106
|
|
|
|
107
|
|
|
foreach ($events as $event) { |
108
|
|
|
$googleEvents[] = $calendar |
109
|
|
|
->getEventById($event->getGoogleId()); |
110
|
|
|
} |
111
|
|
|
if (!$googleEvents) { |
|
|
|
|
112
|
|
|
throw new JsonHttpException(404, 'Events not found'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]); |
116
|
|
|
|
117
|
|
|
return new JsonResponse(['user' => $user, 'events' => $googleEvents]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $id |
122
|
|
|
* @Route("/{id}") |
123
|
|
|
* @Method("DELETE") |
124
|
|
|
* @return JsonResponse |
125
|
|
|
*/ |
126
|
|
|
public function removeEventAction($id) |
127
|
|
|
{ |
128
|
|
|
$this->get('app.google_calendar') |
129
|
|
|
->deleteEvent($id); |
130
|
|
|
$em = $this->getDoctrine()->getManager(); |
131
|
|
|
/** @var Event $event */ |
132
|
|
|
$event = $em->getRepository('AppBundle:Event')->findByGoogleId($id); |
133
|
|
|
$em->remove($event); |
134
|
|
|
$em->flush(); |
135
|
|
|
|
136
|
|
|
return $this->json(['success' => 'Event was removed']); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param Request $request |
141
|
|
|
* @Method("PATCH") |
142
|
|
|
* @Route("/{id}") |
143
|
|
|
* |
144
|
|
|
* @return JsonResponse |
145
|
|
|
*/ |
146
|
|
|
public function editEventAction(Request $request, $id) |
147
|
|
|
{ |
148
|
|
|
$dtoEvent = new DtoEvent(); |
149
|
|
|
$form = $this->createForm(EventType::class, $dtoEvent); |
150
|
|
|
$this->handleJsonForm($form, $request); |
151
|
|
|
$this->get('app.google_calendar') |
152
|
|
|
->editEvent($dtoEvent, $id, $request->query->all()); |
153
|
|
|
|
154
|
|
|
return $this->json(['success' => 'Event edited']); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* FOR DEV ONLY. |
159
|
|
|
* @Method("PUT") |
160
|
|
|
* @Route("/clear") |
161
|
|
|
*/ |
162
|
|
|
public function clearAction() |
163
|
|
|
{ |
164
|
|
|
$result = $this->get('app.google_calendar')->clear(); |
165
|
|
|
|
166
|
|
|
return $this->json($result); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.