-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathUserTricksController.php
158 lines (132 loc) · 4.03 KB
/
UserTricksController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace Controllers;
use Illuminate\Support\Facades\Auth;
use Tricks\Repositories\TagRepositoryInterface;
use Tricks\Repositories\TrickRepositoryInterface;
use Tricks\Repositories\CategoryRepositoryInterface;
class UserTricksController extends BaseController
{
/**
* Trick repository.
*
* @var \Tricks\Repositories\TrickRepositoryInterface
*/
protected $trick;
/**
* Tag repository.
*
* @var \Tricks\Repositories\TagRepositoryInterface
*/
protected $tags;
/**
* Category repository.
*
* @var \Tricks\Repositories\CategoryRepositoryInterface
*/
protected $categories;
/**
* Create a new TrickController instance.
*
* @param \Tricks\Repositories\TrickRepositoryInterface $trick
* @param \Tricks\Repositories\TagRepositoryInterface $tags
* @param \Tricks\Repositories\CategoryRepositoryInterface $categories
* @return void
*/
public function __construct(
TrickRepositoryInterface $trick,
TagRepositoryInterface $tags,
CategoryRepositoryInterface $categories
) {
parent::__construct();
$this->beforeFilter('auth');
$this->trick = $trick;
$this->tags = $tags;
$this->categories = $categories;
}
/**
* Show the create new trick page.
*
* @return \Response
*/
public function getNew()
{
$tagList = $this->tags->listAll();
$categoryList = $this->categories->listAll();
$this->view('tricks.new', compact('tagList', 'categoryList'));
}
/**
* Handle the creation of a new trick.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postNew()
{
$form = $this->trick->getCreationForm();
if (! $form->isValid()) {
return $this->redirectBack([ 'errors' => $form->getErrors() ]);
}
$data = $form->getInputData();
$data['user_id'] = Auth::user()->id;
$trick = $this->trick->create($data);
return $this->redirectRoute('user.index');
}
/**
* Show the edit trick page.
*
* @param string $slug
* @return \Response
*/
public function getEdit($slug)
{
$trick = $this->trick->findBySlug($slug);
$tagList = $this->tags->listAll();
$categoryList = $this->categories->listAll();
$selectedTags = $this->trick->listTagsIdsForTrick($trick);
$selectedCategories = $this->trick->listCategoriesIdsForTrick($trick);
$this->view('tricks.edit', [
'tagList' => $tagList,
'selectedTags' => $selectedTags,
'categoryList' => $categoryList,
'selectedCategories' => $selectedCategories,
'trick' => $trick
]);
}
/**
* Handle the editing of a trick.
*
* @param string $slug
* @return \Illuminate\Http\RedirectResponse
*/
public function postEdit($slug)
{
$trick = $this->trick->findBySlug($slug);
$form = $this->trick->getEditForm($trick->id);
if (! $form->isValid()) {
return $this->redirectBack([ 'errors' => $form->getErrors() ]);
}
$data = $form->getInputData();
$trick = $this->trick->edit($trick, $data);
return $this->redirectRoute('tricks.edit', [ $trick->slug ], [
'success' => 'Trick has been updated'
]);
}
/**
* Delete a trick from the database.
*
* @param string $slug
* @return \Illuminate\Http\RedirectResponse
*/
public function getDelete($slug)
{
$trick = $this->trick->findBySlug($slug);
if ($trick->user_id != Auth::user()->id) {
return "This trick doesn't belong to you";
}
$trick->tags()->detach();
$trick->categories()->detach();
$trick->delete();
return $this->redirectRoute('user.index', null, [
'success' => 'Trick has been deleted'
]);
}
}