1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Api\V1; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
|
7
|
|
|
use App\Models\Post; |
8
|
|
|
use App\Models\Author; |
9
|
|
|
|
10
|
|
|
use Validator; |
11
|
|
|
|
12
|
|
|
use Illuminate\Support\Facades\Redis; |
13
|
|
|
|
14
|
|
|
class PostsController extends BaseController |
15
|
|
|
{ |
16
|
|
|
protected $model = \App\Models\Post::class; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Post listing. |
21
|
|
|
* |
22
|
|
|
* @param int $id |
23
|
|
|
* @return \Illuminate\Http\Response |
24
|
|
|
*/ |
25
|
|
|
public function index() |
26
|
|
|
{ |
27
|
|
|
$posts = Redis::get('posts'); |
28
|
|
|
if (! $posts) { |
29
|
|
|
$posts = Post::with('author')->all(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $this->response->array([ |
33
|
|
|
'status' => 'success', |
34
|
|
|
'message' => '', |
35
|
|
|
'data' => [ |
36
|
|
|
'posts' => $posts, |
37
|
|
|
], |
38
|
|
|
]); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get post by ID. |
43
|
|
|
* |
44
|
|
|
* @param int $id |
45
|
|
|
* @return \Illuminate\Http\Response |
46
|
|
|
*/ |
47
|
|
|
public function show($id) |
48
|
|
|
{ |
49
|
|
|
$post = Redis::get('posts:'.$id); |
50
|
|
|
if (! $post) { |
51
|
|
|
$post = Post::with('author')->findOrFail($id); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->response->array([ |
55
|
|
|
'status' => 'success', |
56
|
|
|
'message' => '', |
57
|
|
|
'data' => [ |
58
|
|
|
'post' => $post, |
59
|
|
|
], |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Save a new post. |
66
|
|
|
* |
67
|
|
|
* @return \Illuminate\Http\Response |
68
|
|
|
*/ |
69
|
|
|
public function store() |
70
|
|
|
{ |
71
|
|
|
$validator = $this->validator($this->requests); |
72
|
|
|
if ($validator->fails()) { |
73
|
|
|
throw new \Dingo\Api\Exception\StoreResourceFailedException( |
74
|
|
|
'Could not create new resource.', |
75
|
|
|
$validator->errors() |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$post = $this->model::with('author')->create($this->requests->toArray()); |
80
|
|
|
|
81
|
|
|
// cache it |
82
|
|
|
Redis::set('posts:'.$post->id, $post); |
83
|
|
|
Redis::set('posts:authors:'.$post->id, $post->author); |
84
|
|
|
Redis::append('posts', $post); |
85
|
|
|
Redis::append('posts:authors', $post->author); |
86
|
|
|
|
87
|
|
|
return $this->response->array($post); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get post author. |
93
|
|
|
* |
94
|
|
|
* @param int $id |
95
|
|
|
* @return \Illuminate\Http\Response |
96
|
|
|
*/ |
97
|
|
|
public function author($id) |
98
|
|
|
{ |
99
|
|
|
$author = Redis::get('posts:authors:'.$id); |
100
|
|
|
if (! $author) { |
101
|
|
|
$author = Post::findOrFail($id)->author; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->response->array($author); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get post images. |
110
|
|
|
* |
111
|
|
|
* @param int $id |
112
|
|
|
* @return \Illuminate\Http\Response |
113
|
|
|
*/ |
114
|
|
|
public function images($id) |
115
|
|
|
{ |
116
|
|
|
$images = Redis::get('posts:images:'.$id); |
117
|
|
|
if (! $images) { |
118
|
|
|
$images = Post::findOrFail($id)->images; |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$images = Post::with('images')->findOrFail($id)->images; |
122
|
|
|
|
123
|
|
|
return $this->response->array($images); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Delete a post. |
128
|
|
|
* |
129
|
|
|
* @return \Illuminate\Http\Response |
130
|
|
|
*/ |
131
|
|
|
public function delete($id) |
132
|
|
|
{ |
133
|
|
|
$post = $this->model::findOrFail($id); |
134
|
|
|
$post->delete(); |
135
|
|
|
|
136
|
|
|
// remove cache |
137
|
|
|
Redis::del('posts:'.$id); |
138
|
|
|
Redis::del('posts:authors:'.$id); |
139
|
|
|
Redis::del('posts:images:'.$id); |
140
|
|
|
|
141
|
|
|
return $this->response->array([ |
142
|
|
|
'status' => 'success', |
143
|
|
|
'message' => 'Resource deleted.', |
144
|
|
|
]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get store validator. |
152
|
|
|
* |
153
|
|
|
* @param \Illuminate\Http\Request $request |
154
|
|
|
* @return \Illuminate\Validation\Validator |
155
|
|
|
*/ |
156
|
|
|
protected function validator(Request $request) |
157
|
|
|
{ |
158
|
|
|
return Validator::make($request->all(), [ |
159
|
|
|
'author_id' => 'required|integer|exists:authors,id', |
160
|
|
|
'title' => 'required|unique:posts|max:255', |
161
|
|
|
]); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|