1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Post; |
6
|
|
|
use App\Comment; |
7
|
|
|
|
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
|
10
|
|
|
class PostCommentController extends Controller{ |
11
|
|
|
|
12
|
|
|
public function __construct(){ |
13
|
|
|
|
14
|
|
|
$this->middleware('oauth', ['except' => ['index', 'show']]); |
15
|
|
|
$this->middleware('authorize:' . __CLASS__, ['except' => ['index', 'show', 'store']]); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function index($post_id){ |
19
|
|
|
|
20
|
|
|
$post = Post::find($post_id); |
21
|
|
|
|
22
|
|
|
if(!$post){ |
23
|
|
|
return $this->error("The post with {$post_id} doesn't exist", 404); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$comments = $post->comments; |
27
|
|
|
return $this->success($comments, 200); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function store(Request $request, $post_id){ |
31
|
|
|
|
32
|
|
|
$post = Post::find($post_id); |
33
|
|
|
|
34
|
|
|
if(!$post){ |
35
|
|
|
return $this->error("The post with {$post_id} doesn't exist", 404); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->validateRequest($request); |
39
|
|
|
|
40
|
|
|
$comment = Comment::create([ |
|
|
|
|
41
|
|
|
'content' => $request->get('content'), |
42
|
|
|
'user_id'=> $this->getUserId(), |
43
|
|
|
'post_id'=> $post_id |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
return $this->success("The comment with id {$comment->id} has been created and assigned to the post with id {$post_id}", 201); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function update(Request $request, $post_id, $comment_id){ |
50
|
|
|
|
51
|
|
|
$comment = Comment::find($comment_id); |
52
|
|
|
$post = Post::find($post_id); |
53
|
|
|
|
54
|
|
|
if(!$comment || !$post){ |
55
|
|
|
return $this->error("The comment with {$comment_id} or the post with id {$post_id} doesn't exist", 404); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->validateRequest($request); |
59
|
|
|
|
60
|
|
|
$comment->content = $request->get('content'); |
61
|
|
|
$comment->user_id = $this->getUserId(); |
62
|
|
|
$comment->post_id = $post_id; |
63
|
|
|
|
64
|
|
|
$comment->save(); |
65
|
|
|
|
66
|
|
|
return $this->success("The comment with with id {$comment->id} has been updated", 200); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function destroy($post_id, $comment_id){ |
70
|
|
|
|
71
|
|
|
$comment = Comment::find($comment_id); |
72
|
|
|
$post = Post::find($post_id); |
73
|
|
|
|
74
|
|
|
if(!$comment || !$post){ |
75
|
|
|
return $this->error("The comment with {$comment_id} or the post with id {$post_id} doesn't exist", 404); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if(!$post->comments()->find($comment_id)){ |
79
|
|
|
return $this->error("The comment with id {$comment_id} isn't assigned to the post with id {$post_id}", 409); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$comment->delete(); |
83
|
|
|
|
84
|
|
|
return $this->success("The comment with id {$comment_id} has been removed of the post {$post_id}", 200); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function validateRequest(Request $request){ |
88
|
|
|
|
89
|
|
|
$rules = [ |
90
|
|
|
'content' => 'required' |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
$this->validate($request, $rules); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function isAuthorized(Request $request){ |
97
|
|
|
|
98
|
|
|
$resource = "comments"; |
99
|
|
|
$comment = Comment::find($this->getArgs($request)["comment_id"]); |
100
|
|
|
|
101
|
|
|
return $this->authorizeUser($request, $resource, $comment); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.