1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Post; |
6
|
|
|
|
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
|
9
|
|
|
class PostController extends Controller{ |
10
|
|
|
|
11
|
|
|
public function __construct(){ |
12
|
|
|
|
13
|
|
|
$this->middleware('oauth', ['except' => ['index', 'show']]); |
14
|
|
|
$this->middleware('authorize:' . __CLASS__, ['except' => ['index', 'show', 'store']]); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function index(){ |
18
|
|
|
|
19
|
|
|
$posts = Post::all(); |
20
|
|
|
return $this->success($posts, 200); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function store(Request $request){ |
24
|
|
|
|
25
|
|
|
$this->validateRequest($request); |
26
|
|
|
|
27
|
|
|
$post = Post::create([ |
|
|
|
|
28
|
|
|
'title' => $request->get('title'), |
29
|
|
|
'content'=> $request->get('content'), |
30
|
|
|
'user_id' => $this->getUserId() |
31
|
|
|
]); |
32
|
|
|
|
33
|
|
|
return $this->success("The post with with id {$post->id} has been created", 201); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
View Code Duplication |
public function show($id){ |
|
|
|
|
37
|
|
|
|
38
|
|
|
$post = Post::find($id); |
39
|
|
|
|
40
|
|
|
if(!$post){ |
41
|
|
|
return $this->error("The post with {$id} doesn't exist", 404); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->success($post, 200); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function update(Request $request, $id){ |
48
|
|
|
|
49
|
|
|
$post = Post::find($id); |
50
|
|
|
|
51
|
|
|
if(!$post){ |
52
|
|
|
return $this->error("The post with {$id} doesn't exist", 404); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->validateRequest($request); |
56
|
|
|
|
57
|
|
|
$post->title = $request->get('title'); |
58
|
|
|
$post->content = $request->get('content'); |
59
|
|
|
$post->user_id = $this->getUserId(); |
60
|
|
|
|
61
|
|
|
$post->save(); |
62
|
|
|
|
63
|
|
|
return $this->success("The post with with id {$post->id} has been updated", 200); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function destroy($id){ |
|
|
|
|
67
|
|
|
|
68
|
|
|
$post = Post::find($id); |
69
|
|
|
|
70
|
|
|
if(!$post){ |
71
|
|
|
return $this->error("The post with {$id} doesn't exist", 404); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// no need to delete the comments for the current post, |
75
|
|
|
// since we used on delete cascase on update cascase. |
76
|
|
|
// $post->comments()->delete(); |
|
|
|
|
77
|
|
|
$post->delete(); |
78
|
|
|
|
79
|
|
|
return $this->success("The post with with id {$id} has been deleted along with it's comments", 200); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function validateRequest(Request $request){ |
83
|
|
|
|
84
|
|
|
$rules = [ |
85
|
|
|
'title' => 'required', |
86
|
|
|
'content' => 'required' |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
$this->validate($request, $rules); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function isAuthorized(Request $request){ |
93
|
|
|
|
94
|
|
|
$resource = "posts"; |
95
|
|
|
$post = Post::find($this->getArgs($request)["post_id"]); |
96
|
|
|
|
97
|
|
|
return $this->authorizeUser($request, $resource, $post); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.