Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove n+1 queries from conference index and conference show pages #501

Merged
merged 7 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions app/ApiResources/Talk.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ public function getType()
public function attributes()
{
return [
'title' => $this->talk->current()->title,
'description' => $this->talk->current()->description,
'type' => $this->talk->current()->type,
'length' => $this->talk->current()->length,
'level' => $this->talk->current()->level,
'slides' => $this->talk->current()->slides,
'title' => $this->talk->currentRevision->title,
'description' => $this->talk->currentRevision->description,
'type' => $this->talk->currentRevision->type,
'length' => $this->talk->currentRevision->length,
'level' => $this->talk->currentRevision->level,
'slides' => $this->talk->currentRevision->slides,
'public' => $this->talk->public,
'organizer_notes' => $this->talk->current()->organizer_notes,
'created_at' => (string) $this->talk->current()->created_at,
'updated_at' => (string) $this->talk->current()->updated_at,
'organizer_notes' => $this->talk->currentRevision->organizer_notes,
'created_at' => (string) $this->talk->currentRevision->created_at,
'updated_at' => (string) $this->talk->currentRevision->updated_at,
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Collections/TalksCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TalksCollection extends Collection
public function sortByTitle()
{
return $this->sortBy(function (Talk $talk) {
return strtolower($talk->current()->title);
return strtolower($talk->currentRevision->title);
})->values();
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/TalksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TalksController extends Controller
public function show($id)
{
try {
$talk = auth()->guard('api')->user()->talks()->findOrFail($id);
$talk = auth()->guard('api')->user()->talks()->withCurrentRevision()->findOrFail($id);
} catch (Exception $e) {
App::abort(404);
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/UserTalksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function index($userId)
$talks = auth()->guard('api')
->user()
->talks()
->withCurrentRevision()
->when((bool) request()->query('include-archived'), function ($query) {
$query->withoutGlobalScope('active');
})
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/ConferencesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ public function show($id)
return redirect('/');
}

$talks = auth()->user()->talks->sortByTitle()->map(function ($talk) use ($conference) {
$talks = auth()->user()->talks()->withCurrentRevision()->get()->sortByTitle()->map(function ($talk) use ($conference) {
return TalkTransformer::transform($talk, $conference);
});

$conference->loadCount('openIssues');

return view('conferences.show', [
'conference' => $conference,
'talks' => $talks,
Expand Down Expand Up @@ -119,6 +121,7 @@ public function destroy($id)
private function showPublic($id)
{
$conference = Conference::approved()->findOrFail($id);
$conference->loadCount('openIssues');

return view('conferences.showPublic', [
'conference' => $conference,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/PublicProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function index(SpeakerSearchRequest $request)
public function show($profile_slug)
{
$user = $this->getPublicUserByProfileSlug($profile_slug);
$talks = $user->talks()->public()->get()->sortByTitle();
$talks = $user->talks()->withCurrentRevision()->public()->get()->sortByTitle();
$bios = $user->bios()->public()->get();

return view('account.public-profile.show', [
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/SubmissionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function store(Request $request)
}

$conference = Conference::findOrFail($request->input('conferenceId'));
$talkRevision = $talk->current();
$talkRevision = $talk->loadCurrentRevision()->currentRevision;
$submission = $conference->submissions()->create(['talk_revision_id' => $talkRevision->id]);

return response()->json([
Expand Down
14 changes: 7 additions & 7 deletions app/Http/Controllers/TalksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function edit($id)

return view('talks.edit', [
'talk' => $talk,
'current' => $talk->current(),
'current' => $talk->currentRevision,
]);
}

Expand Down Expand Up @@ -103,7 +103,7 @@ public function show($id, Request $request)
{
$talk = auth()->user()->talks()->findOrFail($id);

$current = $request->filled('revision') ? $talk->revisions()->findOrFail($request->input('revision')) : $talk->current();
$current = $request->filled('revision') ? $talk->revisions()->findOrFail($request->input('revision')) : $talk->loadCurrentRevision()->currentRevision;

$submissions = Submission::where('talk_revision_id', $current->id)
->with(['conference', 'acceptance', 'rejection'])
Expand All @@ -129,7 +129,7 @@ public function destroy($id)
public function archiveIndex(Request $request)
{
$talks = $this->sortTalks(
auth()->user()->archivedTalks()->get(),
auth()->user()->archivedTalks()->withCurrentRevision()->get(),
$request->input('sort')
);

Expand Down Expand Up @@ -161,13 +161,13 @@ private function filterTalks($filter)
{
switch ($filter) {
case 'submitted':
return auth()->user()->talks()->submitted()->get();
return auth()->user()->talks()->withCurrentRevision()->submitted()->get();
break;
case 'accepted':
return auth()->user()->talks()->accepted()->get();
return auth()->user()->talks()->withCurrentRevision()->accepted()->get();
break;
default:
return auth()->user()->talks()->get();
return auth()->user()->talks()->withCurrentRevision()->get();
break;
}
}
Expand All @@ -186,7 +186,7 @@ private function sortTalks($talks, $sort)
$this->sorted_by = 'alpha';

return $talks->sortBy(function ($talk) {
return strtolower($talk->current()->title);
return strtolower($talk->currentRevision->title);
});
break;
}
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Livewire/ConferenceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public function getConferencesProperty()
->sortByCfpOpening($this->sort)
->sortByCfpClosing($this->sort);
})
->when(auth()->user(), fn ($query) => $query->with('submissions'))
->withCount('openIssues')
->get()
->groupByMonth($this->dateColumn())
->sortKeys()
Expand Down
7 changes: 6 additions & 1 deletion app/Models/Conference.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ public function issues()
return $this->hasMany(ConferenceIssue::class);
}

public function openIssues()
{
return $this->issues()->whereOpen();
}

// @todo: Deprecate?
public static function closingSoonest()
{
Expand Down Expand Up @@ -332,7 +337,7 @@ public function isFavoritedBy(User $user)

public function isFlagged()
{
return $this->issues()->whereOpen()->exists();
return $this->open_issues_count > 0;
}

public function isRejected()
Expand Down
23 changes: 21 additions & 2 deletions app/Models/Talk.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Collections\TalksCollection;
use App\Models\TalkRevision;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;

Expand Down Expand Up @@ -59,9 +60,9 @@ public function acceptances()
return $this->hasManyThrough(Acceptance::class, TalkRevision::class);
}

public function current()
public function currentRevision()
{
return $this->revisions()->orderBy('created_at', 'DESC')->first();
return $this->belongsTo(TalkRevision::class);
}

public function revisions()
Expand Down Expand Up @@ -111,6 +112,24 @@ public function scopeAccepted($query)
$query->has('acceptances');
}

public function scopeWithCurrentRevision($query)
{
$query->addSelect([
'current_revision_id' => TalkRevision::select('id')
->whereColumn('talk_id', 'talks.id')
->latest()
->take(1),
])->with('currentRevision');
}

public function loadCurrentRevision()
{
return $this->setRelation(
'currentRevision',
TalkRevision::where('talk_id', $this->id)->latest()->take(1)->first(),
);
}

public function getMySubmissionForConference(Conference $conference)
{
return $conference->mySubmissions()->filter(function ($item) {
Expand Down
7 changes: 4 additions & 3 deletions app/Transformers/TalkForConferenceTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ class TalkForConferenceTransformer
{
public static function transform(Talk $talk, Conference $conference)
{
$currentTalk = $talk->current();
$currentRevision = $talk->currentRevision;
$currentRevision->setRelation('talk', $talk);

$submission = $talk->getMySubmissionForConference($conference);
$acceptance = $submission ? $submission->acceptance : null;
$rejection = $submission ? $submission->rejection : null;

return [
'id' => $talk->id,
'title' => $currentTalk->title,
'url' => $currentTalk->getUrl(),
'title' => $currentRevision->title,
'url' => $currentRevision->getUrl(),
'submitted' => (bool) $submission,
'submissionId' => $submission ? $submission->id : null,
'accepted' => (bool) $acceptance,
Expand Down
11 changes: 11 additions & 0 deletions database/factories/ConferenceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ public function withSpeakerPackage()
]);
}

public function withOpenIssue()
{
return $this->afterCreating(function ($conference) {
ConferenceIssue::factory()
->open()
->create([
'conference_id' => $conference->id,
]);
});
}

public function withClosedIssue()
{
return $this->afterCreating(function ($conference) {
Expand Down
9 changes: 9 additions & 0 deletions database/factories/ConferenceIssueFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public function definition()
];
}

public function open()
{
return $this->state(function () {
return [
'closed_at' => null,
];
});
}

public function closed()
{
return $this->state(function () {
Expand Down
4 changes: 3 additions & 1 deletion database/factories/TalkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public function author(User $user)
public function submitted()
{
return $this->afterCreating(function (Talk $talk) {
Conference::factory()->received($talk->current())->create();
Conference::factory()
->received($talk->loadCurrentRevision()->currentRevision)
->create();
});
}

Expand Down
4 changes: 2 additions & 2 deletions resources/views/account/public-profile/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class="no-underline block mt-8 text-indigo"
@forelse ($talks as $talk)
<h4 class="text-2xl text-indigo">
<a href="{{ route('speakers-public.talks.show', ['profileSlug' => $user->profile_slug, 'talkId' => $talk->id]) }}">
{{ $talk->current()->title }}
{{ $talk->currentRevision->title }}
</a>
</h4>
<span class="text-sm text-gray-500">
{{ $talk->current()->length }}-minute {{ $talk->current()->type }} talk at {{ $talk->current()->level }} level
{{ $talk->currentRevision->length }}-minute {{ $talk->currentRevision->type }} talk at {{ $talk->currentRevision->level }} level
</span>
@empty
This speaker has not made any of their talks public yet.
Expand Down
4 changes: 2 additions & 2 deletions resources/views/talks/listing.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<x-listing :title="$talk->current()->title" :href="route('talks.show', $talk)">
<x-listing :title="$talk->currentRevision->title" :href="route('talks.show', $talk)">
<x-slot name="actions">
<a href="{{ route('talks.edit', $talk) }}" title="Edit">
@svg('compose', 'w-5 fill-current inline')
Expand All @@ -22,7 +22,7 @@
<div>{{ $talk->created_at->toFormattedDateString() }}</div>
</div>
<div class="flex items-end">
<div>{{ $talk->current()->length }}-minute {{ $talk->current()->level }} {{ $talk->current()->type }}</div>
<div>{{ $talk->currentRevision->length }}-minute {{ $talk->currentRevision->level }} {{ $talk->currentRevision->type }}</div>
</div>
</x-slot>
</x-listing>
6 changes: 3 additions & 3 deletions resources/views/talks/show-public.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class="inline-block"
Return to profile for {{ $user->name }}
</x-button.primary>

<h2 class="text-4xl mt-8">{{ $talk->current()->title }}</h2>
<h2 class="text-4xl mt-8">{{ $talk->currentRevision->title }}</h2>

<p style="font-style: italic;">
{{ $talk->current()->length }} minute {{ $talk->current()->level }} {{ $talk->current()->type }}
{{ $talk->currentRevision->length }} minute {{ $talk->currentRevision->level }} {{ $talk->currentRevision->type }}
</p>

<h3 class="text-3xl mt-8">Description/Proposal</h3>

{!! markdown($talk->current()->getDescription()) !!}
{!! markdown($talk->currentRevision->getDescription()) !!}
</x-panel>

@endsection
2 changes: 1 addition & 1 deletion resources/views/talks/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<x-side-menu title="Revisions">
<x-slot name="body">
@foreach ($talk->revisions as $revision)
@if ($talk->current()->id == $revision->id)
@if ($talk->currentRevision->id == $revision->id)
<a
href="/talks/{{ $talk->id }}"
class="{{ $baseLinkClasses }} {{ $revision->id == $current->id ? $activeLinkClasses : '' }}"
Expand Down
36 changes: 36 additions & 0 deletions tests/Feature/ConferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,7 @@ function conferences_with_reported_issues_are_flagged()

$conference->reportIssue('spam', 'Conference has spam', $user);

$conference->loadCount('openIssues');
$this->assertTrue($conference->isFlagged());
}

Expand All @@ -1178,6 +1179,7 @@ function conferences_with_closed_issues_are_not_flagged()
{
$conference = Conference::factory()->withClosedIssue()->create();

$conference->loadCount('openIssues');
$this->assertFalse($conference->isFlagged());
}

Expand Down Expand Up @@ -1266,4 +1268,38 @@ public function rejected_conferences_are_not_searchable(): void
$this->assertFalse($conferenceA->shouldBeSearchable());
$this->assertTrue($conferenceB->shouldBeSearchable());
}

/** @test */
public function conferences_with_open_issues_are_flagged_on_the_index_page(): void
{
$conference = Conference::factory()->withOpenIssue()->create();

$response = Livewire::test(ConferenceList::class);

tap($response->conferences->flatten(), function ($conferences) {
$this->assertEquals(1, $conferences->count());
$this->assertTrue($conferences->first()->isFlagged());
});
}

/** @test */
public function conferences_with_open_issues_are_flagged_on_the_show_page(): void
{
$conference = Conference::factory()->withOpenIssue()->create();

$response = $this->actingAs(User::factory()->create())
->get(route('conferences.show', $conference));

$response->assertSee('An issue has been reported for this conference.');
}

/** @test */
public function conferences_with_open_issues_are_flagged_on_the_public_show_page(): void
{
$conference = Conference::factory()->withOpenIssue()->create();

$response = $this->get(route('conferences.show', $conference));

$response->assertSee('An issue has been reported for this conference.');
}
}
Loading
Loading