1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Api; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
use App\Models\Post; |
7
|
|
|
use App\Services\PostProcesses; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Http\Response; |
10
|
|
|
use Validator; |
11
|
|
|
|
12
|
|
|
class BlogController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Create a new controller instance. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
// |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Display a listing of the resource. |
26
|
|
|
* |
27
|
|
|
* @param \Illuminate\Http\Request |
28
|
|
|
* |
29
|
|
|
* @return \Illuminate\Http\Response |
30
|
|
|
*/ |
31
|
|
|
public function index(Request $request) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$publishedPosts = Post::allPublishedPosts()->get(); |
34
|
|
|
|
35
|
|
|
return response()->json([ |
|
|
|
|
36
|
|
|
'code' => 200, |
37
|
|
|
'message' => 'Welcome to the '.config('blog.title').' API', |
38
|
|
|
'data' => [ |
39
|
|
|
'title' => config('blog.title'), |
40
|
|
|
'subtitle' => config('blog.subtitle'), |
41
|
|
|
'description' => config('blog.description'), |
42
|
|
|
'total_posts' => $publishedPosts->count(), |
43
|
|
|
'blog_image' => config('blog.post_image'), |
44
|
|
|
'latest_post' => [ |
45
|
|
|
'post_id' => $publishedPosts->last()->id, |
46
|
|
|
'post_title' => $publishedPosts->last()->title, |
47
|
|
|
'post_subtitle' => $publishedPosts->last()->subtitle, |
48
|
|
|
'post_updated_at' => $publishedPosts->last()->updated_at, |
49
|
|
|
'post_published_at' => $publishedPosts->last()->published_at, |
50
|
|
|
'post_subtitle' => $publishedPosts->last()->subtitle, |
51
|
|
|
], |
52
|
|
|
'sitemap' => config('app.url').'/sitemap.xml', |
53
|
|
|
'rss' => [ |
54
|
|
|
'blog' => route('feeds.blog'), |
55
|
|
|
], |
56
|
|
|
|
57
|
|
|
], |
58
|
|
|
], Response::HTTP_OK); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Display the posts paginated. |
63
|
|
|
* |
64
|
|
|
* @param \Illuminate\Http\Request |
65
|
|
|
* |
66
|
|
|
* @return \Illuminate\Http\Response |
67
|
|
|
*/ |
68
|
|
|
public function posts(Request $request) |
69
|
|
|
{ |
70
|
|
|
$validator = Validator::make($request->all(), [ |
71
|
|
|
'tag' => 'string|max:255', |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
if ($validator->fails()) { |
75
|
|
|
return response()->json([ |
|
|
|
|
76
|
|
|
'errors' => $validator->errors(), |
77
|
|
|
], Response::HTTP_UNPROCESSABLE_ENTITY); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$tag = $request->get('tag'); |
81
|
|
|
$service = new PostProcesses($tag); |
82
|
|
|
$data = $service->getResponse(); |
83
|
|
|
|
84
|
|
|
return response()->json($data['posts'], Response::HTTP_OK); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Display all the posts. |
89
|
|
|
* |
90
|
|
|
* @param \Illuminate\Http\Request |
91
|
|
|
* |
92
|
|
|
* @return \Illuminate\Http\Response |
93
|
|
|
*/ |
94
|
|
|
public function allPosts(Request $request) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$publishedPosts = Post::allPublishedPosts()->get(); |
97
|
|
|
|
98
|
|
|
return response()->json($publishedPosts, Response::HTTP_OK); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the Latest Post. |
103
|
|
|
* |
104
|
|
|
* @param \Illuminate\Http\Request |
105
|
|
|
* |
106
|
|
|
* @return \Illuminate\Http\Response |
107
|
|
|
*/ |
108
|
|
|
public function latestPost(Request $request) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$post = Post::allPublishedPosts()->first(); |
111
|
|
|
|
112
|
|
|
if (!$post) { |
113
|
|
|
$post = null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return response()->json($post, Response::HTTP_OK); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Gets the posts authors. |
121
|
|
|
* |
122
|
|
|
* @param \Illuminate\Http\Request |
123
|
|
|
* |
124
|
|
|
* @return \Illuminate\Http\Response |
125
|
|
|
*/ |
126
|
|
|
public function getPostsAuthors(Request $request) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$authors = Post::activeAuthors()->get(); |
129
|
|
|
|
130
|
|
|
if (!$authors) { |
|
|
|
|
131
|
|
|
$authors = []; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return response()->json($authors, Response::HTTP_OK); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Gets posts by author. |
139
|
|
|
* |
140
|
|
|
* @param \Illuminate\Http\Request |
141
|
|
|
* |
142
|
|
|
* @return \Illuminate\Http\Response |
143
|
|
|
*/ |
144
|
|
|
public function getPostsByAuthor(Request $request, $author) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$validator = Validator::make([ |
147
|
|
|
'author' => $author, |
148
|
|
|
], [ |
149
|
|
|
'author' => 'string|max:255', |
150
|
|
|
]); |
151
|
|
|
|
152
|
|
|
if ($validator->fails()) { |
153
|
|
|
return response()->json([ |
|
|
|
|
154
|
|
|
'errors' => $validator->errors(), |
155
|
|
|
], Response::HTTP_UNPROCESSABLE_ENTITY); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$posts = Post::postsByAuthors($author)->get(); |
159
|
|
|
|
160
|
|
|
if (!$posts) { |
|
|
|
|
161
|
|
|
$posts = []; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return response()->json($posts, Response::HTTP_OK); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.