Skip to content

Commit

Permalink
refactor: torrent requests sum queries
Browse files Browse the repository at this point in the history
- 3 queries down to one
  • Loading branch information
HDVinnie committed Jun 3, 2020
1 parent 0aca478 commit d3bc402
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
17 changes: 9 additions & 8 deletions app/Http/Controllers/RequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ public function __construct(RequestFacetedRepository $faceted, ChatRepository $c
public function requests(Request $request)
{
$user = $request->user();

$requests = DB::table('requests')
->selectRaw('count(*) as total')
->selectRaw("count(case when filled_by != null then 1 end) as filled")
->selectRaw("count(case when filled_by = null then 1 end) as unfilled")
->selectRaw('count(case when filled_by != null then 1 end) as filled')
->selectRaw('count(case when filled_by = null then 1 end) as unfilled')
->first();
$bounties = DB::table('requests')
->selectRaw('coalesce(sum(bounty), 0) as total')
->selectRaw('coalesce(sum(case when filled_by != null then 1 end), 0) as claimed')
->selectRaw('coalesce(sum(case when filled_by = null then 1 end), 0) as unclaimed')
->first();
$total_bounty = TorrentRequest::all()->sum('bounty');
$claimed_bounty = TorrentRequest::whereNotNull('filled_by')->sum('bounty');
$unclaimed_bounty = TorrentRequest::whereNull('filled_by')->sum('bounty');

$torrentRequests = TorrentRequest::with(['user', 'category', 'type'])->paginate(25);
$repository = $this->faceted;
Expand All @@ -90,9 +93,7 @@ public function requests(Request $request)
'repository' => $repository,
'user' => $user,
'requests' => $requests,
'total_bounty' => $total_bounty,
'claimed_bounty' => $claimed_bounty,
'unclaimed_bounty' => $unclaimed_bounty,
'bounties' => $bounties,
]);
}

Expand Down
6 changes: 3 additions & 3 deletions resources/views/requests/requests.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ class="form-horizontal form-condensed form-torrent-search form-bordered">
<strong>@lang('request.requests'):</strong> {{ $requests->total }} |
<strong>@lang('request.filled'):</strong> {{ $requests->filled }} |
<strong>@lang('request.unfilled'):</strong> {{ $requests->unfilled }} |
<strong>@lang('request.total-bounty'):</strong> {{ $total_bounty }} @lang('bon.bon') |
<strong>@lang('request.bounty-claimed'):</strong> {{ $claimed_bounty }} @lang('bon.bon') |
<strong>@lang('request.bounty-unclaimed'):</strong> {{ $unclaimed_bounty }} @lang('bon.bon')
<strong>@lang('request.total-bounty'):</strong> {{ $bounties->total }} @lang('bon.bon') |
<strong>@lang('request.bounty-claimed'):</strong> {{ $bounties->claimed }} @lang('bon.bon') |
<strong>@lang('request.bounty-unclaimed'):</strong> {{ $bounties->unclaimed }} @lang('bon.bon')
</span>
<a href="{{ route('add_request') }}" role="button" data-toggle="tooltip"
data-original-title="@lang('request.add-request')!" class="btn btn btn-success">
Expand Down

0 comments on commit d3bc402

Please sign in to comment.