Skip to content

Commit 4b26b8c

Browse files
author
Renato Marinho
authored
Merge pull request #272 from Te7a-Houdini/refactor-issuesController-index
Refactor issueController index method
2 parents 99b5775 + 98b4d8e commit 4b26b8c

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
@@ -22,58 +22,41 @@ class IssueController extends Controller
2222
{
2323
public function index($slug)
2424
{
25-
if ($slug) {
26-
$sprint = Sprint::slug($slug)
27-
->with('issues.user')
28-
->with('issues.users')
29-
->with('issues.commits')
30-
->with('issues.statuses')
31-
->with('issues.status')
32-
->with('issues.comments')
33-
->with('issues.attachments')
34-
->with('issues.type')
35-
->with('issues.productBacklog')
36-
->with('issues.sprint')
37-
->with('issues.configEffort')
38-
->first();
39-
40-
//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
41-
$is = $sprint->issues;
42-
$issues = array();
43-
foreach($is as $i) {
44-
$issues[$i->config_status_id] = array();
45-
}
46-
foreach($is as $i) {
47-
$issues[$i->config_status_id][] = $i;
48-
}
49-
} else {
50-
$sprint = null;
51-
$issues = Auth::user()->issues()
52-
->with('user')
53-
->with('users')
54-
->with('commits')
55-
->with('statuses')
56-
->with('status')
57-
->with('comments')
58-
->with('attachments')
59-
->with('type')
60-
->with('productBacklog')
61-
->with('sprint')
62-
->with('configEffort')
63-
->get()
64-
->sortBy('position')->groupBy('config_status_id');
65-
}
66-
67-
$configStatus = ConfigStatus::type('issues')->get();
25+
[$sprint,$issues] = $this->sprintWithIssues($slug);
6826

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

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

7962
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)