|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Tag; |
|
6
|
|
|
use App\Transformers\TagTransformer; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
|
|
9
|
|
|
use App\Http\Requests; |
|
10
|
|
|
use App\Http\Controllers\Controller; |
|
11
|
|
|
use Illuminate\Support\Facades\Response; |
|
12
|
|
|
use Symfony\Component\Console\Input\Input; |
|
13
|
|
|
|
|
14
|
|
|
class TagController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* TaskController constructor. |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct(TagTransformer $tagTransformer) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->tagTransformer = $tagTransformer; |
|
|
|
|
|
|
22
|
|
|
$this->middleware('auth.basic', ['only' => 'store']); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Display a listing of the resource. |
|
27
|
|
|
* |
|
28
|
|
|
* @return \Illuminate\Http\Response |
|
29
|
|
|
*/ |
|
30
|
|
|
public function index($tagId = null) |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
//$tags = Tag::all(); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
$tag = $this->getTags($tagId); |
|
|
|
|
|
|
37
|
|
|
return $this->respond($this->tagTransformer->transformCollection($tag->all())); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Show the form for creating a new resource. |
|
42
|
|
|
* |
|
43
|
|
|
* @return \Illuminate\Http\Response |
|
44
|
|
|
*/ |
|
45
|
|
|
public function create() |
|
46
|
|
|
{ |
|
47
|
|
|
// |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Store a newly created resource in storage. |
|
52
|
|
|
* |
|
53
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
|
|
54
|
|
|
* @return \Illuminate\Http\Response |
|
55
|
|
|
*/ |
|
56
|
|
|
public function store() |
|
57
|
|
|
{ |
|
58
|
|
|
|
|
59
|
|
View Code Duplication |
if (! Input::get('name') or ! Input::get('done') or ! Input::get('priority')) |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
return $this->setStatusCode(IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY) |
|
|
|
|
|
|
62
|
|
|
->respondWithError('Parameters failed validation for a tag.'); |
|
63
|
|
|
} |
|
64
|
|
|
Tag::create(Input::all()); |
|
|
|
|
|
|
65
|
|
|
return $this->respondCreated('Tag successfully created.'); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Display the specified resource. |
|
71
|
|
|
* |
|
72
|
|
|
* @param int $id |
|
73
|
|
|
* @return \Illuminate\Http\Response |
|
74
|
|
|
*/ |
|
75
|
|
|
public function show($id) |
|
76
|
|
|
{ |
|
77
|
|
|
|
|
78
|
|
|
$tag = Tag::find($id); |
|
79
|
|
|
// $task = Task::where('id',$id)->first(); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
if (! $tag){ |
|
82
|
|
|
return Response::json([ |
|
83
|
|
|
|
|
84
|
|
|
'error' => [ |
|
85
|
|
|
'message' => 'La tag no existeix', |
|
86
|
|
|
'code' |
|
87
|
|
|
] |
|
88
|
|
|
], 404); |
|
89
|
|
|
} |
|
90
|
|
|
return Response::json([ |
|
91
|
|
|
$this->tagTransfomer->transform($tag) |
|
|
|
|
|
|
92
|
|
|
], 200); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Show the form for editing the specified resource. |
|
97
|
|
|
* |
|
98
|
|
|
* @param int $id |
|
99
|
|
|
* @return \Illuminate\Http\Response |
|
100
|
|
|
*/ |
|
101
|
|
|
public function edit($id) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
// |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Update the specified resource in storage. |
|
108
|
|
|
* |
|
109
|
|
|
* @param \Illuminate\Http\Request $request |
|
110
|
|
|
* @param int $id |
|
111
|
|
|
* @return \Illuminate\Http\Response |
|
112
|
|
|
*/ |
|
113
|
|
|
public function update(Request $request, $id) |
|
114
|
|
|
{ |
|
115
|
|
|
$tag = Tag::findorFail($id); |
|
116
|
|
|
$this->saveTag($request, $tag); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Remove the specified resource from storage. |
|
121
|
|
|
* |
|
122
|
|
|
* @param int $id |
|
123
|
|
|
* @return \Illuminate\Http\Response |
|
124
|
|
|
*/ |
|
125
|
|
|
public function destroy($id) |
|
126
|
|
|
{ |
|
127
|
|
|
Tag::destroy($id); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param Request $request |
|
132
|
|
|
* @param $tag |
|
133
|
|
|
*/ |
|
134
|
|
|
public function saveTag(Request $request, $tag) |
|
135
|
|
|
{ |
|
136
|
|
|
$tag->name = $request->name; |
|
|
|
|
|
|
137
|
|
|
$tag->priority = $request->priority; |
|
|
|
|
|
|
138
|
|
|
$tag->done = $request->done; |
|
|
|
|
|
|
139
|
|
|
$tag->save(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: