|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GiveBlood\Support\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use GiveBlood\Support\Http\Controllers\Controller; |
|
7
|
|
|
use GiveBlood\Modules\Campaign\Comment; |
|
8
|
|
|
use GiveBlood\Modules\Campaign\Campaign; |
|
9
|
|
|
use Tymon\JWTAuth\Facades\JWTAuth; |
|
10
|
|
|
use Carbon\Carbon; |
|
11
|
|
|
|
|
12
|
|
|
class CommentsController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
public function __construct() |
|
15
|
|
|
{ |
|
16
|
|
|
$this->middleware('jwt.auth', [ 'except' => [ 'index', 'show' ] ]); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function index($campaign) |
|
20
|
|
|
{ |
|
21
|
|
|
$comments = Comment::where('campaign_id', $campaign)->get(); |
|
22
|
|
|
|
|
23
|
|
|
return response()->json($comments, 200); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function create(Request $request, $campaign) |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
$user = JWTAuth::parseToken()->authenticate(); |
|
30
|
|
|
|
|
31
|
|
|
//$campaign = Campaign::find($id); |
|
32
|
|
|
$camp = Campaign::find($campaign); |
|
33
|
|
|
$comment = new Comment(); |
|
34
|
|
|
$comment->id = str_random(); |
|
|
|
|
|
|
35
|
|
|
$comment->comment = $request[ 'comment' ]; |
|
36
|
|
|
$comment->user_id = $user->id; |
|
37
|
|
|
$comment->campaign_id = $camp->id; |
|
38
|
|
|
// $comment->created_at = Carbon::now(); |
|
39
|
|
|
$comment->save(); |
|
40
|
|
|
|
|
41
|
|
|
// return response()->json($comment); |
|
42
|
|
|
return response()->json( |
|
|
|
|
|
|
43
|
|
|
[ |
|
44
|
|
|
'status_code' => 201, |
|
45
|
|
|
'message' => 'Comment added!' |
|
46
|
|
|
], 201 |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
public function update($id, Request $request) |
|
52
|
|
|
{ |
|
53
|
|
|
$comment = Comment::find($id); |
|
54
|
|
|
|
|
55
|
|
|
$user = JWTAuth::parseToken()->authenticate(); |
|
56
|
|
|
|
|
57
|
|
|
if ($user->id !== $comment->user_id) { |
|
58
|
|
|
return response()->json( |
|
|
|
|
|
|
59
|
|
|
[ |
|
60
|
|
|
'status_code' => 401, |
|
61
|
|
|
'message' => 'You haven\'t permission to update this entry' |
|
62
|
|
|
], 401 |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$comment->comment = $request[ 'comment' ]; |
|
67
|
|
|
|
|
68
|
|
|
if (!$comment) { |
|
69
|
|
|
return response()->json( |
|
70
|
|
|
[ |
|
71
|
|
|
'error_code' => 404, |
|
72
|
|
|
'error_message' => 'Comment not found!' |
|
73
|
|
|
], 404 |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$comment->save(); |
|
78
|
|
|
|
|
79
|
|
|
return response()->json( |
|
80
|
|
|
[ |
|
81
|
|
|
'status_code' => 200, |
|
82
|
|
|
'message' => 'Comment updated!' |
|
83
|
|
|
], 200 |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
View Code Duplication |
public function destroy($id) |
|
88
|
|
|
{ |
|
89
|
|
|
$comment = Comment::find($id); |
|
90
|
|
|
|
|
91
|
|
|
$user = JWTAuth::parseToken()->authenticate(); |
|
92
|
|
|
|
|
93
|
|
|
if ($user->id !== $comment->user_id) { |
|
94
|
|
|
return response()->json( |
|
|
|
|
|
|
95
|
|
|
[ |
|
96
|
|
|
'status_code' => 401, |
|
97
|
|
|
'message' => 'You haven\'t permission to update this entry' |
|
98
|
|
|
], 401 |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// Notify error in not found |
|
103
|
|
|
if (!$comment) { |
|
104
|
|
|
return response()->json( |
|
105
|
|
|
[ |
|
106
|
|
|
'error_code' => 404, |
|
107
|
|
|
'message' => 'Comment not found!' |
|
108
|
|
|
], 404 |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$Comment->delete(); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
return response()->json( |
|
115
|
|
|
[ |
|
116
|
|
|
'status_code' => 204, |
|
117
|
|
|
'message' => 'Comment deleted' |
|
118
|
|
|
], 204 |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: