Skip to content

Commit d1fc84f

Browse files
committed
Refactor issueController index method
1 parent b42e171 commit d1fc84f

File tree

2 files changed

+29
-46
lines changed

2 files changed

+29
-46
lines changed

app/Http/Controllers/Web/IssueController.php

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,58 +21,41 @@ class IssueController extends Controller
2121
{
2222
public function index($slug)
2323
{
24-
if ($slug) {
25-
$sprint = Sprint::slug($slug)
26-
->with('issues.user')
27-
->with('issues.users')
28-
->with('issues.commits')
29-
->with('issues.statuses')
30-
->with('issues.status')
31-
->with('issues.comments')
32-
->with('issues.attachments')
33-
->with('issues.type')
34-
->with('issues.productBacklog')
35-
->with('issues.sprint')
36-
->with('issues.configEffort')
37-
->first();
38-
39-
//when viewing from sprint planning, issues need to be passed in an array indexed by their status, so they can be put in the appropriate kanban columns
40-
$is = $sprint->issues;
41-
$issues = array();
42-
foreach($is as $i) {
43-
$issues[$i->config_status_id] = array();
44-
}
45-
foreach($is as $i) {
46-
$issues[$i->config_status_id][] = $i;
47-
}
48-
} else {
49-
$sprint = null;
50-
$issues = Auth::user()->issues()
51-
->with('user')
52-
->with('users')
53-
->with('commits')
54-
->with('statuses')
55-
->with('status')
56-
->with('comments')
57-
->with('attachments')
58-
->with('type')
59-
->with('productBacklog')
60-
->with('sprint')
61-
->with('configEffort')
62-
->get()
63-
->sortBy('position')->groupBy('config_status_id');
64-
}
65-
66-
$configStatus = ConfigStatus::type('issues')->get();
24+
[$sprint,$issues] = $this->sprintWithIssues($slug);
6725

6826
if (!is_null($sprint) && !count($sprint)) {
6927
return redirect()->route('sprints.index');
7028
}
7129

7230
return view('issues.index')
7331
->with('sprint', $sprint)
74-
->with('issues', $issues)
75-
->with('configStatus', $configStatus);
32+
->with('issues', $issues->sortBy('position')->groupBy('config_issue_effort_id'))
33+
->with('configStatus', ConfigStatus::type('issues')->get());
34+
}
35+
36+
private function sprintWithIssues($slug)
37+
{
38+
if ($slug)
39+
{
40+
$sprint = $this->eagerLoad(Sprint::slug($slug),'issues.')->first();
41+
42+
return [$sprint , $sprint->issues];
43+
}
44+
45+
return [ null , $this->eagerLoad(Auth::user()->issues())->get()];
46+
}
47+
48+
private function eagerLoad($query , $relation = '')
49+
{
50+
$eagerLoaders = collect(['user','users','commits','statuses',
51+
'comments','attachments','type',
52+
'productBacklog','sprint','configEffort']);
53+
54+
$eagerLoaders->each(function($loader) use (&$query,$relation){
55+
$query = $query->with($relation . $loader);
56+
});
57+
58+
return $query;
7659
}
7760

7861
public function create($scope, $slug, $parent_id = null)

resources/views/issues/index.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class="btn btn-sm btn-primary" data-toggle="modal" data-target="#modalLarge">
5353
{{$status->title}}
5454
(
5555
@if(isset($issues[$status->id]))
56-
<span>{{count($issues[$status->id])}}</span>
56+
<span>{{$issues[$status->id]->count()}}</span>
5757
@else
5858
<span>0</span>
5959
@endif

0 commit comments

Comments
 (0)