|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\DTO\DtoEvent; |
|
6
|
|
|
use AppBundle\Entity\Event; |
|
7
|
|
|
use AppBundle\Exception\JsonHttpException; |
|
8
|
|
|
use AppBundle\Form\EventType; |
|
9
|
|
|
use Mcfedr\JsonFormBundle\Controller\JsonController; |
|
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @Route("/schedule/events") |
|
17
|
|
|
*/ |
|
18
|
|
|
class CalendarController extends JsonController |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @Route("/") |
|
22
|
|
|
* @Method("GET") |
|
23
|
|
|
* |
|
24
|
|
|
* @return JsonResponse |
|
25
|
|
|
*/ |
|
26
|
|
|
public function eventsListAction(Request $request) |
|
27
|
|
|
{ |
|
28
|
|
|
return new JsonResponse($this->get('app.google_calendar') |
|
29
|
|
|
->getEventList($request->query->all())); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param Request $request |
|
34
|
|
|
* @Route("") |
|
35
|
|
|
* @Method("POST") |
|
36
|
|
|
* |
|
37
|
|
|
* @return JsonResponse |
|
38
|
|
|
*/ |
|
39
|
|
|
public function newEventAction(Request $request) |
|
40
|
|
|
{ |
|
41
|
|
|
$dtoEvent = new DtoEvent(); |
|
42
|
|
|
$form = $this->createForm(EventType::class, $dtoEvent); |
|
43
|
|
|
$this->handleJsonForm($form, $request); |
|
44
|
|
|
$result = $this->get('app.google_calendar') |
|
45
|
|
|
->createEvent($dtoEvent, $request->query->all()); |
|
46
|
|
|
if (!$result) { |
|
47
|
|
|
throw new JsonHttpException(412, 'Event has not been created'); |
|
48
|
|
|
} |
|
49
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
50
|
|
|
$user = $em->getRepository('AppBundle:User') |
|
51
|
|
|
->find($dtoEvent->getUser()); |
|
52
|
|
|
|
|
53
|
|
|
if (!$user) { |
|
54
|
|
|
throw new JsonHttpException(404, 'User not found.'); |
|
55
|
|
|
} |
|
56
|
|
|
$event = new Event(); |
|
57
|
|
|
$event->setGoogleId($result->id); |
|
58
|
|
|
$event->addUser($user); |
|
59
|
|
|
$user->setEvent($event); |
|
60
|
|
|
|
|
61
|
|
|
$em->persist($user); |
|
62
|
|
|
$em->flush(); |
|
63
|
|
|
|
|
64
|
|
|
return $this->json(['success' => 'Event Created']); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $id |
|
69
|
|
|
* @Route("/{id}") |
|
70
|
|
|
* @Method("GET") |
|
71
|
|
|
* |
|
72
|
|
|
* @return JsonResponse |
|
73
|
|
|
*/ |
|
74
|
|
|
public function singleEventAction($id) |
|
75
|
|
|
{ |
|
76
|
|
|
/** @var Event $event */ |
|
77
|
|
|
$event = $this->getDoctrine()->getRepository('AppBundle:Event') |
|
78
|
|
|
->findByGoogleId($id); |
|
79
|
|
|
$user = $event->getUsers()->first(); |
|
|
|
|
|
|
80
|
|
|
if (!$user) { |
|
81
|
|
|
throw new JsonHttpException(404, 'User not found.'); |
|
82
|
|
|
} |
|
83
|
|
|
$googleEvent = $this->get('app.google_calendar') |
|
84
|
|
|
->getEventById($id); |
|
85
|
|
|
if (!$googleEvent) { |
|
86
|
|
|
throw new JsonHttpException(404, 'Event not found'); |
|
87
|
|
|
} |
|
88
|
|
|
$user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]); |
|
89
|
|
|
return new JsonResponse(['user' => $user, 'event' => $googleEvent]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @Route("/user/{id}") |
|
94
|
|
|
* @Method("GET") |
|
95
|
|
|
* |
|
96
|
|
|
* @return JsonResponse |
|
97
|
|
|
*/ |
|
98
|
|
|
public function userEventsAction($id) |
|
99
|
|
|
{ |
|
100
|
|
|
$user = $this->getDoctrine()->getRepository('AppBundle:User')->find($id); |
|
101
|
|
|
$events = $user->getEvents(); |
|
102
|
|
|
$googleEvents = []; |
|
103
|
|
|
$calendar = $this->get('app.google_calendar'); |
|
104
|
|
|
|
|
105
|
|
|
foreach ($events as $event) { |
|
106
|
|
|
$googleEvents[] = $calendar |
|
107
|
|
|
->getEventById($event->getGoogleId()); |
|
108
|
|
|
} |
|
109
|
|
|
if (!$googleEvents) { |
|
|
|
|
|
|
110
|
|
|
throw new JsonHttpException(404, 'Events not found'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]); |
|
114
|
|
|
|
|
115
|
|
|
$response = array_merge(['user' => $user], ['events' => $googleEvents]); |
|
116
|
|
|
|
|
117
|
|
|
return new JsonResponse($response); |
|
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.