66use TeachMe \Http \Requests ;
77
88use Illuminate \Http \Request ;
9+ use TeachMe \Repositories \TicketRepository ;
910
1011class TicketsController extends Controller {
1112
12- protected function selectTicketsList ()
13+ /**
14+ * @var TicketRepository
15+ */
16+ private $ ticketRepository ;
17+
18+ public function __construct (TicketRepository $ ticketRepository )
1319 {
14- return Ticket::selectRaw (
15- 'tickets.*, '
16- . '( SELECT COUNT(*) FROM ticket_comments WHERE ticket_comments.ticket_id = tickets.id ) as num_comments, '
17- . '( SELECT COUNT(*) FROM ticket_votes WHERE ticket_votes.ticket_id = tickets.id ) as num_votes '
18- )->with ('author ' );
20+ $ this ->ticketRepository = $ ticketRepository ;
1921 }
2022
2123 public function latest ()
2224 {
23- $ tickets = $ this ->selectTicketsList ()
24- ->orderBy ('created_at ' , 'DESC ' )
25- ->paginate (20 );
25+ $ tickets = $ this ->ticketRepository ->paginateLatest ();
2626
2727 return view ('tickets/list ' , compact ('tickets ' ));
2828 }
@@ -34,25 +34,19 @@ public function popular()
3434
3535 public function open ()
3636 {
37- $ tickets = $ this ->selectTicketsList ()
38- ->where ('status ' , 'open ' )
39- ->orderBy ('created_at ' , 'DESC ' )
40- ->paginate (20 );
37+ $ tickets = $ this ->ticketRepository ->paginateOpen ();
4138 return view ('tickets/list ' , compact ('tickets ' ));
4239 }
4340
4441 public function closed ()
4542 {
46- $ tickets = $ this ->selectTicketsList ()
47- ->where ('status ' , 'closed ' )
48- ->orderBy ('created_at ' , 'DESC ' )
49- ->paginate (20 );
43+ $ tickets = $ this ->ticketRepository ->paginateClosed ();
5044 return view ('tickets/list ' , compact ('tickets ' ));
5145 }
5246
5347 public function details ($ id )
5448 {
55- $ ticket = Ticket:: findOrFail ($ id );
49+ $ ticket = $ this -> ticketRepository -> findOrFail ($ id );
5650 return view ('tickets/details ' , compact ('ticket ' ));
5751 }
5852
0 commit comments