1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoeSangue\Http\Controllers\API; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use DoeSangue\Http\Requests\UpdateCampaignRequest; |
7
|
|
|
use DoeSangue\Http\Controllers\Controller; |
8
|
|
|
use DoeSangue\Models\Campaign; |
9
|
|
|
|
10
|
|
|
class CampaignController extends Controller |
11
|
|
|
{ |
12
|
|
|
public function __construct() |
13
|
|
|
{ |
14
|
|
|
$this->middleware('jwt.auth', ['except' => ['index', 'show']]); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function index() |
18
|
|
|
{ |
19
|
|
|
$campaigns = Campaign::all(); |
20
|
|
|
|
21
|
|
|
return response()->json(compact('campaigns')); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function store(Request $request) |
25
|
|
|
{ |
26
|
|
|
$this->validate($request, |
27
|
|
|
[ |
28
|
|
|
'title' => 'required|min:60', |
29
|
|
|
'expires' => 'required|date', |
30
|
|
|
'id_user' => 'required|integer', |
31
|
|
|
]); |
32
|
|
|
|
33
|
|
|
$campaign = new Campaign(); |
34
|
|
|
$campaign->title = $request[ 'title' ]; |
|
|
|
|
35
|
|
|
$campaign->expires = $request[ 'expires' ]; |
|
|
|
|
36
|
|
|
$campaign->id_user = $request['id_user']; |
|
|
|
|
37
|
|
|
$campaign->save(); |
38
|
|
|
|
39
|
|
|
return response()->json($campaign, 201); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function show($id) |
43
|
|
|
{ |
44
|
|
|
$campaign = Campaign::findOrFail($id); |
45
|
|
|
|
46
|
|
|
return response()->json(compact('campaign')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function update(UpdateCampaignRequest $request, $id) |
50
|
|
|
{ |
51
|
|
|
$campaign = Campaign::find($id); |
52
|
|
|
$campaign->title = $request[ 'title' ]; |
53
|
|
|
$campaign->expires = $request[ 'expires' ]; |
54
|
|
|
$campaign->save(); |
55
|
|
|
|
56
|
|
|
return response()->json($campaign); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function destroy($id) |
60
|
|
|
{ |
61
|
|
|
$campaign = Campaign::find($id); |
62
|
|
|
// Notify error in not found |
63
|
|
|
if (!$campaign) { |
64
|
|
|
return response()->json( |
65
|
|
|
[ |
66
|
|
|
'error_code' => '404', |
67
|
|
|
'message' => 'Campaign not found!' |
68
|
|
|
], 404 |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
$campaign->delete(); |
72
|
|
|
|
73
|
|
|
return response()->json([ |
74
|
|
|
'title' => $campaign->title, |
75
|
|
|
'id_user' => $campaign->id_user, |
76
|
|
|
'status' => 'deleted' |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get campaigns by specific donor/user |
82
|
|
|
* @return Response |
83
|
|
|
*/ |
84
|
|
|
public function campaigns(Donor $id){ |
85
|
|
|
|
86
|
|
|
$donor = Donor::find($id); |
87
|
|
|
$campaigns = Campaigns::where('id_user', $donor->id)->get(); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.