|
|
|
@ -10,51 +10,72 @@ class PostController extends Controller
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public function index(Request $request): Response
|
|
|
|
public function index(Request $request): Response
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$lastPost = 0;
|
|
|
|
$request->validate(
|
|
|
|
|
|
|
|
[
|
|
|
|
|
|
|
|
'lastPost' => 'required|integer',
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if ($request->lastPost) {
|
|
|
|
$lastPost = $request->lastPost;
|
|
|
|
$lastPost = $request->lastPost;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$posts = Post::query()
|
|
|
|
$posts = Post::query()
|
|
|
|
// ->select([
|
|
|
|
|
|
|
|
// 'title',
|
|
|
|
|
|
|
|
// 'description',
|
|
|
|
|
|
|
|
// ])
|
|
|
|
|
|
|
|
->where(column: 'sequence', operator: '>', value: $lastPost)
|
|
|
|
->where(column: 'sequence', operator: '>', value: $lastPost)
|
|
|
|
->take(10)
|
|
|
|
->take(10)
|
|
|
|
->get();
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
|
|
return response([
|
|
|
|
return response(
|
|
|
|
'status' => 200,
|
|
|
|
[
|
|
|
|
'data' => $posts,
|
|
|
|
'status' => 200,
|
|
|
|
]);
|
|
|
|
'data' => $posts,
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function store(Request $request): Response
|
|
|
|
public function store(Request $request): Response
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$postId = Post::query()->max(column: 'sequence');
|
|
|
|
$request->validate(
|
|
|
|
|
|
|
|
[
|
|
|
|
|
|
|
|
'title' => 'required|string',
|
|
|
|
|
|
|
|
'description' => 'required|string',
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$user = $request->user();
|
|
|
|
|
|
|
|
|
|
|
|
$postId = ! $postId ? 1 : $postId + 1;
|
|
|
|
if (! ($user instanceof User)) {
|
|
|
|
|
|
|
|
return response(
|
|
|
|
|
|
|
|
[
|
|
|
|
|
|
|
|
'status' => 400,
|
|
|
|
|
|
|
|
'message' => 'Invalid user',
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Post::query()->create([
|
|
|
|
$user->posts()->create(
|
|
|
|
'title' => "This is Post {$postId}",
|
|
|
|
[
|
|
|
|
'description' => "post {$postId} description",
|
|
|
|
'title' => $request->title,
|
|
|
|
]);
|
|
|
|
'description' => $request->description,
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return response([
|
|
|
|
return response(
|
|
|
|
'status' => 200,
|
|
|
|
[
|
|
|
|
'message' => "Create Post {$postId} Success!",
|
|
|
|
'status' => 200,
|
|
|
|
]);
|
|
|
|
'message' => 'Create Post Success!',
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function show(string $id): Response
|
|
|
|
public function show(string $id): Response
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$post = Post::query()->find($id);
|
|
|
|
$post = Post::query()->find($id);
|
|
|
|
|
|
|
|
|
|
|
|
return response([
|
|
|
|
return response(
|
|
|
|
'status' => 200,
|
|
|
|
[
|
|
|
|
'data' => $post,
|
|
|
|
'status' => 200,
|
|
|
|
]);
|
|
|
|
'data' => $post,
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|