-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathPosts.php
111 lines (94 loc) · 2.66 KB
/
Posts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace Modules\MyBlog\Http\Controllers\Api;
use App\Abstracts\Http\ApiController;
use Modules\MyBlog\Http\Requests\Post as Request;
use Modules\MyBlog\Http\Resources\Post as Resource;
use Modules\MyBlog\Jobs\CreatePost;
use Modules\MyBlog\Jobs\DeletePost;
use Modules\MyBlog\Jobs\UpdatePost;
use Modules\MyBlog\Models\Post;
class Posts extends ApiController
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\JsonResponse
*/
public function index()
{
$posts = Post::with('category', 'comments')->collect();
return Resource::collection($posts);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\JsonResponse
*/
public function show($id)
{
$post = Post::with('category', 'comments')->find($id);
return new Resource($post);
}
/**
* Store a newly created resource in storage.
*
* @param $request
* @return \Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
$post = $this->dispatch(new CreatePost($request));
return $this->created(route('api.my-blog.posts.show', $post->id), new Resource($post));
}
/**
* Update the specified resource in storage.
*
* @param $post
* @param $request
* @return \Illuminate\Http\JsonResponse
*/
public function update(Post $post, Request $request)
{
$post = $this->dispatch(new UpdatePost($post, $request));
return new Resource($post->fresh());
}
/**
* Enable the specified resource in storage.
*
* @param Post $post
* @return \Illuminate\Http\JsonResponse
*/
public function enable(Post $post)
{
$post = $this->dispatch(new UpdatePost($post, request()->merge(['enabled' => 1])));
return new Resource($post->fresh());
}
/**
* Disable the specified resource in storage.
*
* @param Post $post
* @return \Illuminate\Http\JsonResponse
*/
public function disable(Post $post)
{
$post = $this->dispatch(new UpdatePost($post, request()->merge(['enabled' => 0])));
return new Resource($post->fresh());
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$post = Post::with('comments')->find($id);
try {
$this->dispatch(new DeletePost($post));
return $this->noContent();
} catch(\Exception $e) {
$this->errorUnauthorized($e->getMessage());
}
}
}