Skip to content

Commit b173665

Browse files
author
vagrant
committed
Refactoring functions to save tickets, comments and votes through repositories instead of controllers / models
1 parent 629355a commit b173665

File tree

7 files changed

+99
-32
lines changed

7 files changed

+99
-32
lines changed

app/Entities/User.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,4 @@ public function hasVoted(Ticket $ticket)
4747
return $this->voted()->where('ticket_id', $ticket->id)->count();
4848
}
4949

50-
public function vote(Ticket $ticket)
51-
{
52-
if ($this->hasVoted($ticket)) return false;
53-
54-
$this->voted()->attach($ticket);
55-
return true;
56-
}
57-
58-
public function unvote(Ticket $ticket)
59-
{
60-
$this->voted()->detach($ticket);
61-
}
62-
6350
}

app/Http/Controllers/CommentsController.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,38 @@
66
use Illuminate\Http\Request;
77
use TeachMe\Entities\Ticket;
88
use TeachMe\Entities\TicketComment;
9+
use TeachMe\Repositories\CommentRepository;
10+
use TeachMe\Repositories\TicketRepository;
911

1012
class CommentsController extends Controller {
1113

14+
protected $commentRepository;
15+
protected $ticketRepository;
16+
17+
public function __construct(
18+
TicketRepository $ticketRepository,
19+
CommentRepository $commentRepository
20+
)
21+
{
22+
$this->commentRepository = $commentRepository;
23+
$this->ticketRepository = $ticketRepository;
24+
}
25+
1226
public function submit($id, Request $request, Guard $auth)
1327
{
1428
$this->validate($request, [
1529
'comment' => 'required|max:250',
1630
'link' => 'url'
1731
]);
1832

19-
$comment = new TicketComment($request->only(['comment', 'link']));
20-
$comment->user_id = $auth->id();
33+
$ticket = $this->ticketRepository->findOrFail($id);
2134

22-
$ticket = Ticket::findOrFail($id);
23-
$ticket->comments()->save($comment);
35+
$this->commentRepository->create(
36+
$ticket,
37+
currentUser(),
38+
$request->get('comment'),
39+
$request->get('link')
40+
);
2441

2542
session()->flash('success', 'Tu comentario fue guardado exitosamente');
2643
return redirect()->back();

app/Http/Controllers/TicketsController.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22

33
use Illuminate\Auth\Guard;
44
use Illuminate\Support\Facades\Redirect;
5-
use TeachMe\Entities\Ticket;
6-
use TeachMe\Http\Requests;
7-
85
use Illuminate\Http\Request;
6+
97
use TeachMe\Repositories\TicketRepository;
108

119
class TicketsController extends Controller {
1210

13-
/**
14-
* @var TicketRepository
15-
*/
1611
private $ticketRepository;
1712

1813
public function __construct(TicketRepository $ticketRepository)
@@ -55,16 +50,16 @@ public function create()
5550
return view('tickets.create');
5651
}
5752

58-
public function store(Request $request, Guard $auth)
53+
public function store(Request $request)
5954
{
6055
$this->validate($request, [
6156
'title' => 'required|max:120'
6257
]);
6358

64-
$ticket = $auth->user()->tickets()->create([
65-
'title' => $request->get('title'),
66-
'status' => 'open'
67-
]);
59+
$ticket = $this->ticketRepository->openNew(
60+
currentUser(),
61+
$request->get('title')
62+
);
6863

6964
return Redirect::route('tickets.details', $ticket->id);
7065
}

app/Http/Controllers/VotesController.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
<?php namespace TeachMe\Http\Controllers;
22

33
use TeachMe\Entities\Ticket;
4+
use TeachMe\Repositories\TicketRepository;
5+
use TeachMe\Repositories\VoteRepository;
46

57
class VotesController extends Controller {
68

9+
private $ticketRepository;
10+
private $voteRepository;
11+
12+
public function __construct(
13+
TicketRepository $ticketRepository,
14+
VoteRepository $voteRepository
15+
)
16+
{
17+
$this->ticketRepository = $ticketRepository;
18+
$this->voteRepository = $voteRepository;
19+
}
20+
721
public function submit($id)
822
{
9-
$ticket = Ticket::findOrFail($id);
10-
currentUser()->vote($ticket);
23+
$ticket = $this->ticketRepository->findOrFail($id);
24+
$this->voteRepository->vote(currentUser(), $ticket);
1125

1226
return redirect()->back();
1327
}
1428

1529
public function destroy($id)
1630
{
17-
$ticket = Ticket::findOrFail($id);
18-
currentUser()->unvote($ticket);
31+
$ticket = $this->ticketRepository->findOrFail($id);
32+
$this->voteRepository->unvote(currentUser(), $ticket);
1933

2034
return redirect()->back();
2135
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace TeachMe\Repositories;
4+
5+
use TeachMe\Entities\Ticket;
6+
use TeachMe\Entities\TicketComment;
7+
use TeachMe\Entities\User;
8+
9+
class CommentRepository extends BaseRepository {
10+
11+
public function getModel()
12+
{
13+
return new TicketComment();
14+
}
15+
16+
public function create(Ticket $ticket, User $user, $comment, $link = '')
17+
{
18+
$comment = new TicketComment(compact('comment', 'link'));
19+
$comment->user_id = $user->id;
20+
$ticket->comments()->save($comment);
21+
}
22+
23+
}

app/Repositories/TicketRepository.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ public function paginateClosed()
4343
->paginate(20);
4444
}
4545

46+
public function openNew($user, $title)
47+
{
48+
return $user->tickets()->create([
49+
'title' => $title,
50+
'status' => 'open'
51+
]);
52+
}
53+
4654
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace TeachMe\Repositories;
4+
5+
use TeachMe\Entities\Ticket;
6+
use TeachMe\Entities\User;
7+
8+
class VoteRepository {
9+
10+
public function vote(User $user, Ticket $ticket)
11+
{
12+
if ($user->hasVoted($ticket)) return false;
13+
14+
$user->voted()->attach($ticket);
15+
return true;
16+
}
17+
18+
public function unvote(User $user, Ticket $ticket)
19+
{
20+
$user->voted()->detach($ticket);
21+
}
22+
23+
}

0 commit comments

Comments
 (0)