|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Acme\Transformers\TagTransformer; |
|
6
|
|
|
use App\Tag; |
|
7
|
|
|
use App\Task; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
|
|
10
|
|
|
use App\Http\Requests; |
|
11
|
|
|
use App\Http\Controllers\Controller; |
|
12
|
|
|
use Input; |
|
13
|
|
|
|
|
14
|
|
|
class TagController extends ApiController |
|
15
|
|
|
{ |
|
16
|
|
|
protected $tagTranformer; |
|
17
|
|
|
|
|
18
|
|
|
function __construct(TagTransformer $tagTransformer) |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
$this->tagTranformer = $tagTransformer; |
|
21
|
|
|
|
|
22
|
|
|
// TODO: Post test not working with auth middlerare |
|
23
|
|
|
//$this->middleware('auth.basic', ['only' => 'store']); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Display a listing of the resource. |
|
28
|
|
|
* |
|
29
|
|
|
* @return \Illuminate\Http\Response |
|
30
|
|
|
*/ |
|
31
|
|
|
public function index($taskId = null) |
|
32
|
|
|
{ |
|
33
|
|
|
// 1. No es retorna tot: paginació |
|
34
|
|
|
//return Tag::all(); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
$tags = $this->getTags($taskId); |
|
37
|
|
|
|
|
38
|
|
|
return $this->respond($this->tagTranformer->transformCollection($tags))->setStatusCode(200); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Show the form for creating a new resource. |
|
43
|
|
|
* |
|
44
|
|
|
* @return \Illuminate\Http\Response |
|
45
|
|
|
*/ |
|
46
|
|
|
public function create() |
|
47
|
|
|
{ |
|
48
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Store a newly created resource in storage. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
|
|
55
|
|
|
* @return \Illuminate\Http\Response |
|
56
|
|
|
*/ |
|
57
|
|
|
public function store() |
|
58
|
|
|
{ |
|
59
|
|
|
if (!Input::get('title')) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->setStatusCode(422)->respondWithError('Parameters failed validation for a task'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
Tag::create(Input::all()); |
|
65
|
|
|
|
|
66
|
|
|
return $this->respondCreated('Tag successfully created.'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Display the specified resource. |
|
71
|
|
|
* |
|
72
|
|
|
* @param int $id |
|
73
|
|
|
* @return \Illuminate\Http\Response |
|
74
|
|
|
*/ |
|
75
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$tag = Tag::find($id); |
|
78
|
|
|
|
|
79
|
|
|
if (!$tag) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->respondNotFound('Tag does not exist'); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->respond($this->tagTranformer->transform($tag))->setStatusCode(200); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Show the form for editing the specified resource. |
|
89
|
|
|
* |
|
90
|
|
|
* @param int $id |
|
91
|
|
|
* @return \Illuminate\Http\Response |
|
92
|
|
|
*/ |
|
93
|
|
|
public function edit($id) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
// |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Update the specified resource in storage. |
|
100
|
|
|
* |
|
101
|
|
|
* @param \Illuminate\Http\Request $request |
|
102
|
|
|
* @param int $id |
|
103
|
|
|
* @return \Illuminate\Http\Response |
|
104
|
|
|
*/ |
|
105
|
|
|
public function update(Request $request, $id) |
|
106
|
|
|
{ |
|
107
|
|
|
$tag = Tag::find($id); |
|
108
|
|
|
|
|
109
|
|
|
if (!$tag) |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->respondNotFound('Tag does not exist!!'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$tag->title = $request->title; |
|
|
|
|
|
|
115
|
|
|
$tag->save(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Remove the specified resource from storage. |
|
120
|
|
|
* |
|
121
|
|
|
* @param int $id |
|
122
|
|
|
* @return \Illuminate\Http\Response |
|
123
|
|
|
*/ |
|
124
|
|
|
public function destroy($id) |
|
125
|
|
|
{ |
|
126
|
|
|
Tag::destroy($id); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param $taskId |
|
131
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getTags($taskId) |
|
134
|
|
|
{ |
|
135
|
|
|
return $taskId ? Task::findOrFail($taskId)->tags : Tag::all(); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.