Skip to content

Commit 57f0f9c

Browse files
Refactoring to improve API performance (#1605)
1 parent cee7149 commit 57f0f9c

File tree

10 files changed

+154
-78
lines changed

10 files changed

+154
-78
lines changed

app/Http/Controllers/Api/EventsController.php

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,29 @@ public function indexAttending(
265265

266266
// get the events
267267
$events = $query
268-
->with('visibility', 'venue','tags', 'entities','series','eventType','threads')
268+
->with([
269+
'visibility',
270+
'venue.links',
271+
'venue.photos',
272+
'venue.locations',
273+
'venue.entityStatus',
274+
'venue.entityType',
275+
'eventStatus',
276+
'eventType',
277+
'promoter.links',
278+
'promoter.photos',
279+
'promoter.locations',
280+
'promoter.entityStatus',
281+
'promoter.entityType',
282+
'series',
283+
'tags',
284+
'entities',
285+
'photos',
286+
'attendees' => function ($q) {
287+
$q->where('response_type_id', 1);
288+
},
289+
'threads'
290+
])
269291
->paginate($listResultSet->getLimit());
270292

271293
return response()->json(new EventCollection($events));
@@ -304,7 +326,29 @@ public function indexRecommended(
304326

305327
$events = $query
306328
->visible($this->user)
307-
->with('visibility', 'venue', 'tags', 'entities', 'series', 'eventType', 'threads')
329+
->with([
330+
'visibility',
331+
'venue.links',
332+
'venue.photos',
333+
'venue.locations',
334+
'venue.entityStatus',
335+
'venue.entityType',
336+
'eventStatus',
337+
'eventType',
338+
'promoter.links',
339+
'promoter.photos',
340+
'promoter.locations',
341+
'promoter.entityStatus',
342+
'promoter.entityType',
343+
'series',
344+
'tags',
345+
'entities',
346+
'photos',
347+
'attendees' => function ($q) {
348+
$q->where('response_type_id', 1);
349+
},
350+
'threads'
351+
])
308352
->paginate($listResultSet->getLimit());
309353

310354
return response()->json(new EventCollection($events));
@@ -369,13 +413,25 @@ public function indexByDate(
369413
->orderBy('name', 'ASC')
370414
->with([
371415
'visibility',
372-
'venue',
416+
'venue.links',
417+
'venue.photos',
418+
'venue.locations',
419+
'venue.entityStatus',
420+
'venue.entityType',
373421
'eventStatus',
374422
'eventType',
375-
'promoter',
423+
'promoter.links',
424+
'promoter.photos',
425+
'promoter.locations',
426+
'promoter.entityStatus',
427+
'promoter.entityType',
376428
'series',
377429
'tags',
378430
'entities',
431+
'photos',
432+
'attendees' => function ($q) {
433+
$q->where('response_type_id', 1);
434+
},
379435
])
380436
->paginate($listResultSet->getLimit());
381437

@@ -817,14 +873,6 @@ public function show(?Event $event, OembedExtractor $embedExtractor): JsonRespon
817873
'photos',
818874
]);
819875

820-
// None of this is actually loaded, so commenting out for now
821-
822-
// $thread = Thread::where('event_id', '=', $event->id)->first();
823-
// // check blacklist status
824-
// $blacklist = $this->checkBlackList($event);
825-
// // extract all the links from the event body and convert into embeds
826-
// $embeds = $embedExtractor->getEmbedsForEvent($event);
827-
828876
return response()->json(new EventResource($event));
829877
}
830878

app/Http/Controllers/Api/ForumsController.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use App\Models\Visibility;
1717
use App\Services\SessionStore\ListParameterSessionStore;
1818
use Illuminate\Contracts\View\View;
19+
use Illuminate\Http\JsonResponse;
1920
use Illuminate\Http\RedirectResponse;
2021
use Illuminate\Http\Request;
2122
use Illuminate\Http\Response;
@@ -90,12 +91,12 @@ public function index(
9091
Request $request,
9192
ListParameterSessionStore $listParamSessionStore,
9293
ListEntityResultBuilder $listEntityResultBuilder
93-
): string {
94+
): JsonResponse {
9495
// if the gate does not allow this user to show a forum redirect to home
9596
if (Gate::denies('show_forum')) {
9697
flash()->error('Unauthorized', 'Your cannot view the forum index');
9798

98-
return redirect()->back();
99+
return response()->json(['message' => 'Unauthorized'], 403);
99100
}
100101

101102
// initialized listParamSessionStore with base index key
@@ -107,7 +108,8 @@ public function index(
107108

108109
// create the base query including any required joins; needs select to make sure only event entities are returned
109110
$baseQuery = Forum::query()
110-
->select('forums.*');
111+
->with(['visibility', 'threadsCount'])
112+
->select('forums.*');
111113

112114
$listEntityResultBuilder
113115
->setFilter($this->filter)
@@ -121,7 +123,7 @@ public function index(
121123
// get the query builder
122124
$query = $listResultSet->getList();
123125

124-
// get the blogs
126+
// get the forums
125127
$forums = $query->paginate($listResultSet->getLimit());
126128

127129
return response()->json(new ForumCollection($forums));
@@ -166,7 +168,7 @@ public function indexAll(
166168

167169
/* @phpstan-ignore-next-line */
168170
$forums = $query->visible($this->user)
169-
->with('visibility')
171+
->with(['visibility', 'threadsCount'])
170172
->paginate(1000000);
171173

172174
// saves the updated session
@@ -234,7 +236,7 @@ public function filter(
234236

235237
/* @phpstan-ignore-next-line */
236238
$forums = $query->visible($this->user)
237-
->with('visibility')
239+
->with(['visibility', 'threadsCount'])
238240
->paginate($listResultSet->getLimit());
239241

240242
// saves the updated session

app/Http/Controllers/Api/PostsController.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Illuminate\Support\Facades\Log;
2525
use Illuminate\Support\Facades\Mail;
2626
use Illuminate\View\View;
27+
use Illuminate\Http\JsonResponse;
2728
use Str;
2829
use Symfony\Component\HttpFoundation\Response;
2930

@@ -84,12 +85,13 @@ public function index(
8485
Request $request,
8586
ListParameterSessionStore $listParamSessionStore,
8687
ListEntityResultBuilder $listEntityResultBuilder
87-
): string {
88+
): JsonResponse {
8889
// if the gate does not allow this user to show a forum redirect to home
8990
if (Gate::denies('show_forum')) {
9091
flash()->error('Unauthorized', 'Your cannot view the forum');
9192

92-
return redirect()->back();
93+
// return unauthorized response
94+
return response()->json(['message' => 'Unauthorized'], 403);
9395
}
9496

9597
// initialized listParamSessionStore with base index key
@@ -130,10 +132,10 @@ public function index(
130132
// get the query builder
131133
$query = $listResultSet->getList();
132134

133-
// get the threads
134-
$threads = $query->paginate($listResultSet->getLimit());
135+
// get the posts
136+
$posts = $query->paginate($listResultSet->getLimit());
135137

136-
return response()->json(new PostCollection($threads));
138+
return response()->json(new PostCollection($posts));
137139
}
138140

139141
/**

app/Http/Controllers/Api/SeriesController.php

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use App\Models\Visibility;
2424
use App\Services\ImageHandler;
2525
use Carbon\Carbon;
26-
use Storage;
2726
use App\Services\RssFeed;
2827
use App\Services\SessionStore\ListParameterSessionStore;
2928
use App\Services\StringHelper;
@@ -191,7 +190,11 @@ public function index(
191190

192191
// get the events
193192
$series = $query
194-
->with('occurrenceType', 'visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos', 'upcomingEvent')
193+
->with(['occurrenceType', 'occurrenceWeek', 'occurrenceDay', 'visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos' => function ($query) {
194+
$query->where('photos.is_primary', '=', 1);
195+
}, 'upcomingEvent' => function ($query) {
196+
$query->with(['venue.links', 'promoter.links', 'entities', 'tags', 'photos', 'series', 'eventType', 'eventStatus', 'visibility']);
197+
}])
195198
->paginate($listResultSet->getLimit());
196199

197200
return response()->json(new SeriesCollection($series));
@@ -219,7 +222,11 @@ public function popular(Request $request): JsonResponse
219222
->groupBy('series.id');
220223

221224
$series = $query
222-
->with(['visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos', 'upcomingEvent'])
225+
->with(['occurrenceType', 'occurrenceWeek', 'occurrenceDay', 'visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos' => function ($query) {
226+
$query->where('photos.is_primary', '=', 1);
227+
}, 'upcomingEvent' => function ($query) {
228+
$query->with(['venue.links', 'promoter.links', 'entities', 'tags', 'photos', 'series', 'eventType', 'eventStatus', 'visibility']);
229+
}])
223230
->orderByDesc('attendees_count')
224231
->paginate($limit);
225232

@@ -268,7 +275,11 @@ public function indexFollowing(
268275

269276
// get the events
270277
$series = $query
271-
->with(['occurrenceType','visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos', 'upcomingEvent'])
278+
->with(['occurrenceType', 'occurrenceWeek', 'occurrenceDay', 'visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos' => function ($query) {
279+
$query->where('photos.is_primary', '=', 1);
280+
}, 'upcomingEvent' => function ($query) {
281+
$query->with(['venue.links', 'promoter.links', 'entities', 'tags', 'photos', 'series', 'eventType', 'eventStatus', 'visibility']);
282+
}])
272283
->paginate($listResultSet->getLimit());
273284

274285
// saves the updated session
@@ -346,7 +357,7 @@ public function indexCancelled(
346357
Request $request,
347358
ListParameterSessionStore $listParamSessionStore,
348359
ListEntityResultBuilder $listEntityResultBuilder
349-
): string {
360+
): JsonResponse {
350361
// initialized listParamSessionStore with baseindex key
351362
$listParamSessionStore->setBaseIndex('internal_series');
352363
$listParamSessionStore->setKeyPrefix('internal_series_cancelled');
@@ -370,29 +381,20 @@ public function indexCancelled(
370381

371382
// get the events
372383
$series = $query
373-
->with(['occurrenceType', 'visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos', 'upcomingEvent'])
384+
->with(['occurrenceType', 'occurrenceWeek', 'occurrenceDay', 'visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos' => function ($query) {
385+
$query->where('photos.is_primary', '=', 1);
386+
}, 'upcomingEvent' => function ($query) {
387+
$query->with(['venue.links', 'promoter.links', 'entities', 'tags', 'photos', 'series', 'eventType', 'eventStatus', 'visibility']);
388+
}])
374389
->paginate($listResultSet->getLimit());
375390

376391
// saves the updated session
377392
$listParamSessionStore->save();
378393

379394
$this->hasFilter = $listResultSet->getFilters() != $listResultSet->getDefaultFilters() || $listResultSet->getIsEmptyFilter();
380395

381-
return view('series.index')
382-
->with(array_merge(
383-
[
384-
'limit' => $listResultSet->getLimit(),
385-
'sort' => $listResultSet->getSort(),
386-
'direction' => $listResultSet->getSortDirection(),
387-
'hasFilter' => $this->hasFilter,
388-
'filters' => $listResultSet->getFilters(),
389-
'slug' => 'Cancelled',
390-
],
391-
$this->getFilterOptions(),
392-
$this->getListControlOptions()
393-
))
394-
->with(compact('series'))
395-
->render();
396+
return response()->json(new SeriesCollection($series));
397+
396398
}
397399

398400
/**
@@ -404,7 +406,7 @@ public function indexWeek(
404406
Request $request,
405407
ListParameterSessionStore $listParamSessionStore,
406408
ListEntityResultBuilder $listEntityResultBuilder
407-
): string {
409+
): JsonResponse {
408410
// initialized listParamSessionStore with baseindex key
409411
$listParamSessionStore->setBaseIndex('internal_series');
410412
$listParamSessionStore->setKeyPrefix('internal_series_cancelled');
@@ -430,29 +432,19 @@ public function indexWeek(
430432

431433
// get the events
432434
$series = $query
433-
->with(['occurrenceType', 'visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos', 'upcomingEvent'])
435+
->with(['occurrenceType', 'occurrenceWeek', 'occurrenceDay', 'visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos' => function ($query) {
436+
$query->where('photos.is_primary', '=', 1);
437+
}, 'upcomingEvent' => function ($query) {
438+
$query->with(['venue.links', 'promoter.links', 'entities', 'tags', 'photos', 'series', 'eventType', 'eventStatus', 'visibility']);
439+
}])
434440
->paginate($listResultSet->getLimit());
435441

436442
// saves the updated session
437443
$listParamSessionStore->save();
438444

439445
$this->hasFilter = $listResultSet->getFilters() != $listResultSet->getDefaultFilters() || $listResultSet->getIsEmptyFilter();
440446

441-
return view('series.index')
442-
->with(array_merge(
443-
[
444-
'limit' => $listResultSet->getLimit(),
445-
'sort' => $listResultSet->getSort(),
446-
'direction' => $listResultSet->getSortDirection(),
447-
'hasFilter' => $this->hasFilter,
448-
'filters' => $listResultSet->getFilters(),
449-
'slug' => 'Week',
450-
],
451-
$this->getFilterOptions(),
452-
$this->getListControlOptions()
453-
))
454-
->with(compact('series'))
455-
->render();
447+
return response()->json(new SeriesCollection($series));
456448
}
457449

458450
/**
@@ -490,7 +482,11 @@ public function indexRelatedTo(
490482
// get the events
491483
// @phpstan-ignore-next-line
492484
$series = $query->visible($this->user)
493-
->with(['occurrenceType', 'visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos', 'upcomingEvent'])
485+
->with(['occurrenceType', 'occurrenceWeek', 'occurrenceDay', 'visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos' => function ($query) {
486+
$query->where('photos.is_primary', '=', 1);
487+
}, 'upcomingEvent' => function ($query) {
488+
$query->with(['venue.links', 'promoter.links', 'entities', 'tags', 'photos', 'series', 'eventType', 'eventStatus', 'visibility']);
489+
}])
494490
->paginate($listResultSet->getLimit());
495491

496492
// saves the updated session
@@ -550,7 +546,11 @@ public function indexTags(
550546

551547
// get the events
552548
$series = $query
553-
->with(['occurrenceType', 'visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos', 'upcomingEvent'])
549+
->with(['occurrenceType', 'occurrenceWeek', 'occurrenceDay', 'visibility', 'eventStatus', 'eventType', 'promoter', 'venue', 'tags', 'entities', 'photos' => function ($query) {
550+
$query->where('photos.is_primary', '=', 1);
551+
}, 'upcomingEvent' => function ($query) {
552+
$query->with(['venue.links', 'promoter.links', 'entities', 'tags', 'photos', 'series', 'eventType', 'eventStatus', 'visibility']);
553+
}])
554554
->paginate($listResultSet->getLimit());
555555

556556
// saves the updated session
@@ -682,7 +682,7 @@ public function export(
682682

683683
// get the events
684684
$series = $query
685-
->with('occurrenceType', 'visibility', 'tags')
685+
->with(['occurrenceType', 'visibility', 'tags', 'eventType', 'venue', 'entities'])
686686
->paginate($listResultSet->getLimit());
687687

688688
// saves the updated session

0 commit comments

Comments
 (0)