|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Innoflash\Events\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use FaithGen\SDK\Helpers\CommentHelper; |
|
7
|
|
|
use FaithGen\SDK\Http\Requests\CommentRequest; |
|
8
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Illuminate\Routing\Controller; |
|
11
|
|
|
use Innoflash\Events\Http\Requests\CreateRequest; |
|
12
|
|
|
use Innoflash\Events\Http\Requests\DeleteRequest; |
|
13
|
|
|
use Innoflash\Events\Http\Requests\TogglePublishRequest; |
|
14
|
|
|
use Innoflash\Events\Http\Requests\UpdateRequest; |
|
15
|
|
|
use Innoflash\Events\Http\Resources\Event as EventResource; |
|
16
|
|
|
use Innoflash\Events\Http\Resources\EventDetails; |
|
17
|
|
|
use Innoflash\Events\Models\Event; |
|
18
|
|
|
use Innoflash\Events\Services\EventsService; |
|
19
|
|
|
use Intervention\Image\Exception\NotFoundException; |
|
20
|
|
|
|
|
21
|
|
|
class EventController extends Controller |
|
22
|
|
|
{ |
|
23
|
|
|
use AuthorizesRequests; |
|
24
|
|
|
|
|
25
|
|
|
protected $eventsService; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(EventsService $eventsService) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->eventsService = $eventsService; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Creates and event. |
|
34
|
|
|
* |
|
35
|
|
|
* @param CreateRequest $request |
|
36
|
|
|
* |
|
37
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
38
|
|
|
*/ |
|
39
|
|
|
public function create(CreateRequest $request) |
|
40
|
|
|
{ |
|
41
|
|
|
$params = $request->validated(); |
|
42
|
|
|
if (! $request->has('location')) { |
|
43
|
|
|
if (! auth()->user()->profile->location) { |
|
44
|
|
|
throw new NotFoundException('No location found for this event, set one or set your profile location!', |
|
45
|
|
|
404); |
|
46
|
|
|
} else { |
|
47
|
|
|
$params['location'] = auth()->user()->profile->location; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this->eventsService->createFromParent($params, 'Event created successfully'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Fetches the events for the given date. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Request $request |
|
58
|
|
|
* |
|
59
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection |
|
60
|
|
|
*/ |
|
61
|
|
|
public function index(Request $request) |
|
62
|
|
|
{ |
|
63
|
|
|
if ($request->has('date')) { |
|
64
|
|
|
$date = Carbon::parse($request->date); |
|
65
|
|
|
} else { |
|
66
|
|
|
$date = Carbon::now(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$events = auth()->user()->events() |
|
70
|
|
|
->whereDate('start', '>=', $date->startOfMonth()) |
|
71
|
|
|
->whereDate('end', '<=', $date->endOfMonth()) |
|
72
|
|
|
->published() |
|
73
|
|
|
->orderBy('start', 'asc') |
|
74
|
|
|
->get(); |
|
75
|
|
|
|
|
76
|
|
|
EventResource::wrap('events'); |
|
77
|
|
|
|
|
78
|
|
|
return EventResource::collection($events); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Updates an event. |
|
83
|
|
|
* |
|
84
|
|
|
* @param UpdateRequest $request |
|
85
|
|
|
* |
|
86
|
|
|
* @return \Illuminate\Http\JsonResponse|mixed |
|
87
|
|
|
*/ |
|
88
|
|
|
public function update(UpdateRequest $request) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->eventsService->update($request->validated(), 'Event updated successfully!'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Toggles ublish status. |
|
95
|
|
|
* |
|
96
|
|
|
* @param TogglePublishRequest $request |
|
97
|
|
|
* |
|
98
|
|
|
* @return \Illuminate\Http\JsonResponse|mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
public function togglePublish(TogglePublishRequest $request) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->eventsService->update($request->validated(), 'Event publish status changed successfully!'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Deletes event. |
|
107
|
|
|
* |
|
108
|
|
|
* @param DeleteRequest $request |
|
109
|
|
|
* |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
public function destroy(DeleteRequest $request) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
return $this->eventsService->destroy('Event deleted successfully!'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Views the singular event. |
|
119
|
|
|
* |
|
120
|
|
|
* @param Event $event |
|
121
|
|
|
* |
|
122
|
|
|
* @return EventDetails |
|
123
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
124
|
|
|
*/ |
|
125
|
|
|
public function view(Event $event) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->authorize('view', $event); |
|
128
|
|
|
EventDetails::withoutWrapping(); |
|
129
|
|
|
|
|
130
|
|
|
return new EventDetails($event); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Gets the comments for the event. |
|
135
|
|
|
* |
|
136
|
|
|
* @param Request $request |
|
137
|
|
|
* @param Event $event |
|
138
|
|
|
* |
|
139
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection |
|
140
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
141
|
|
|
*/ |
|
142
|
|
|
public function comments(Request $request, Event $event) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->authorize('view', $event); |
|
145
|
|
|
|
|
146
|
|
|
return CommentHelper::getComments($event, $request); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Sends a comment to an event. |
|
151
|
|
|
* |
|
152
|
|
|
* @param CommentRequest $request |
|
153
|
|
|
* |
|
154
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
155
|
|
|
*/ |
|
156
|
|
|
public function comment(CommentRequest $request) |
|
157
|
|
|
{ |
|
158
|
|
|
if (Carbon::parse($this->eventsService->getEvent()->end)->isPast()) { |
|
159
|
|
|
abort(400, 'This event is over, you can`t send anymore comments'); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return CommentHelper::createComment($this->eventsService->getEvent(), $request); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Deletes the banner for an event. |
|
167
|
|
|
* |
|
168
|
|
|
* @param Event $event |
|
169
|
|
|
* |
|
170
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
171
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
172
|
|
|
*/ |
|
173
|
|
|
public function destroyBanner(Event $event) |
|
174
|
|
|
{ |
|
175
|
|
|
$this->authorize('view', $event); |
|
176
|
|
|
|
|
177
|
|
|
return $this->eventsService->deleteBanner($event); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.