1.2: restructure controllers to Api/ prefix and update routes
Deploy Backend / deploy (push) Has been cancelled
Deploy Backend / deploy (push) Has been cancelled
This commit is contained in:
1 parent
c740b69675
commit
299a98d506
11 files changed
+137
-271
No files matched your search
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Blog;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Blog\StoreCategoryRequest;
|
||||
use App\Http\Requests\Blog\UpdateCategoryRequest;
|
||||
use App\Http\Resources\CategoryResource;
|
||||
use App\Http\Resources\PostResource;
|
||||
use App\Models\Blog\Category;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$filter = $request->input('filter');
|
||||
$sortBy = $request->input('sort_by', 'created_at');
|
||||
$sortDirection = $request->input('sort_direction', 'desc');
|
||||
$perPage = $request->input('per_page', 10);
|
||||
|
||||
$query = Category::with('posts');
|
||||
|
||||
if (!empty($filter)) {
|
||||
$query->where('name', 'like', '%' . $filter . '%');
|
||||
}
|
||||
|
||||
$query->orderBy($sortBy, $sortDirection);
|
||||
|
||||
$categories = $query->paginate($perPage);
|
||||
|
||||
return CategoryResource::collection($categories);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$category = Category::with('posts')->findOrFail($id);
|
||||
return new CategoryResource($category);
|
||||
}
|
||||
|
||||
public function getCategoryWithPosts(Request $request, $slug)
|
||||
{
|
||||
$category = Category::where('slug', $slug)->first();
|
||||
|
||||
if (!$category) {
|
||||
return response()->json([
|
||||
'message' => 'Category not found.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$perPage = $request->input('per_page', 10);
|
||||
$filter = $request->input('filter');
|
||||
$sortBy = $request->input('sort_by', 'published_at');
|
||||
$sortDirection = $request->input('sort_direction', 'desc');
|
||||
|
||||
$postsQuery = $category->posts()->with(['category', 'tags', 'author']);
|
||||
|
||||
if (!empty($filter)) {
|
||||
$postsQuery->where('title', 'like', '%' . $filter . '%');
|
||||
}
|
||||
|
||||
$postsQuery->orderBy($sortBy, $sortDirection);
|
||||
|
||||
$posts = $postsQuery->paginate($perPage);
|
||||
|
||||
$meta = [
|
||||
'total' => $posts->total(),
|
||||
'per_page' => $posts->perPage(),
|
||||
'current_page' => $posts->currentPage(),
|
||||
'last_page' => $posts->lastPage(),
|
||||
'from' => $posts->firstItem(),
|
||||
'to' => $posts->lastItem(),
|
||||
];
|
||||
|
||||
$links = [
|
||||
'first' => $posts->url(1),
|
||||
'last' => $posts->url($posts->lastPage()),
|
||||
'prev' => $posts->previousPageUrl(),
|
||||
'next' => $posts->nextPageUrl(),
|
||||
];
|
||||
|
||||
return response()->json([
|
||||
'category' => new CategoryResource($category),
|
||||
'posts' => PostResource::collection($posts),
|
||||
'meta' => $meta,
|
||||
'links' => $links,
|
||||
], 200);
|
||||
}
|
||||
|
||||
public function store(StoreCategoryRequest $request)
|
||||
{
|
||||
$category = Category::create($request->validated());
|
||||
return new CategoryResource($category);
|
||||
}
|
||||
|
||||
public function update(UpdateCategoryRequest $request, $id)
|
||||
{
|
||||
$category = Category::findOrFail($id);
|
||||
$category->update($request->validated());
|
||||
return new CategoryResource($category);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$category = Category::findOrFail($id);
|
||||
$category->delete();
|
||||
return response()->json(['message' => 'Category deleted successfully'], 200);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Blog;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\CategoryResource;
|
||||
use App\Http\Resources\PostResource;
|
||||
use App\Models\Blog\Category;
|
||||
use App\Models\Blog\Post;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$latestPosts = Post::with(['category', 'tags', 'author'])
|
||||
->orderBy('published_at', 'desc')
|
||||
->take(5)
|
||||
->get();
|
||||
|
||||
$latestCategories = Category::orderBy('created_at', 'desc')
|
||||
->take(5)
|
||||
->get();
|
||||
|
||||
$mostViewedPosts = Post::with(['category', 'tags', 'author'])
|
||||
->orderBy('views', 'desc')
|
||||
->take(5)
|
||||
->get();
|
||||
|
||||
$randomPosts = Post::with(['category', 'tags', 'author'])
|
||||
->inRandomOrder()
|
||||
->take(4)
|
||||
->get();
|
||||
|
||||
return response()->json([
|
||||
'latest_posts' => PostResource::collection($latestPosts),
|
||||
'latest_categories' => CategoryResource::collection($latestCategories),
|
||||
'most_viewed_posts' => PostResource::collection($mostViewedPosts),
|
||||
'random_posts' => PostResource::collection($randomPosts),
|
||||
], 200);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Blog;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Blog\StorePostRequest;
|
||||
use App\Http\Requests\Blog\UpdatePostRequest;
|
||||
use App\Http\Resources\PostResource;
|
||||
use App\Models\Blog\Post;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class PostController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$filters = $request->only(['category', 'tag', 'search', 'author', 'published']);
|
||||
|
||||
$sortField = $request->get('sort_field', 'published_at');
|
||||
$sortDirection = $request->get('sort_direction', 'desc');
|
||||
|
||||
$perPage = $request->get('per_page', 10);
|
||||
|
||||
$query = Post::with(['category', 'tags', 'author'])
|
||||
->filter($filters)
|
||||
->sort($sortField, $sortDirection);
|
||||
|
||||
$posts = $query->paginate($perPage);
|
||||
|
||||
return PostResource::collection($posts);
|
||||
}
|
||||
|
||||
public function show($slug)
|
||||
{
|
||||
$post = Post::with(['category', 'tags', 'author', 'relatedPosts.category', 'relatedPosts.tags', 'relatedPosts.author'])
|
||||
->where('slug', $slug)
|
||||
->firstOrFail();
|
||||
|
||||
return response()->json([
|
||||
'post' => new PostResource($post),
|
||||
'related_posts' => PostResource::collection($post->relatedPosts->sortByDesc('published_at')->take(5)),
|
||||
], 200);
|
||||
}
|
||||
|
||||
public function store(StorePostRequest $request)
|
||||
{
|
||||
$post = Post::create($request->validated());
|
||||
|
||||
if ($request->has('tags')) {
|
||||
$post->tags()->attach($request->tags);
|
||||
}
|
||||
|
||||
return new PostResource($post->load(['category', 'tags', 'author']));
|
||||
}
|
||||
|
||||
public function update(UpdatePostRequest $request, $id)
|
||||
{
|
||||
$post = Post::findOrFail($id);
|
||||
$post->update($request->validated());
|
||||
|
||||
if ($request->has('tags')) {
|
||||
$post->tags()->sync($request->tags);
|
||||
}
|
||||
|
||||
return new PostResource($post->load(['category', 'tags', 'author']));
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$post = Post::findOrFail($id);
|
||||
$post->tags()->detach();
|
||||
$post->relatedPosts()->detach();
|
||||
$post->delete();
|
||||
|
||||
return response()->json(['message' => 'Post deleted successfully'], 200);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Blog;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Blog\StoreTagRequest;
|
||||
use App\Http\Requests\Blog\UpdateTagRequest;
|
||||
use App\Http\Resources\TagResource;
|
||||
use App\Models\Blog\Tag;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class TagController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$tags = Tag::with('posts')->paginate(10);
|
||||
return TagResource::collection($tags);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$tag = Tag::with('posts')->findOrFail($id);
|
||||
return new TagResource($tag);
|
||||
}
|
||||
|
||||
public function store(StoreTagRequest $request)
|
||||
{
|
||||
$tag = Tag::create($request->validated());
|
||||
return new TagResource($tag);
|
||||
}
|
||||
|
||||
public function update(UpdateTagRequest $request, $id)
|
||||
{
|
||||
$tag = Tag::findOrFail($id);
|
||||
$tag->update($request->validated());
|
||||
return new TagResource($tag);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$tag = Tag::findOrFail($id);
|
||||
$tag->posts()->detach();
|
||||
$tag->delete();
|
||||
return response()->json(['message' => 'Tag deleted successfully'], 200);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user