blog api created
This commit is contained in:
1 parent
4f101885a8
commit
d98bc7bbb0
27 files changed
+1022
-44
No files matched your search
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Blog;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\CategoryResource;
|
||||
use App\Http\Resources\PostResource;
|
||||
use App\Models\Blog\Category;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
// نمایش لیست دستهبندیها با صفحهبندی
|
||||
public function index(Request $request)
|
||||
{
|
||||
$categories = Category::with('posts')->paginate(10);
|
||||
return CategoryResource::collection($categories);
|
||||
}
|
||||
|
||||
// نمایش یک دستهبندی خاص
|
||||
public function show($id)
|
||||
{
|
||||
$category = Category::with('posts')->findOrFail($id);
|
||||
return new CategoryResource($category);
|
||||
}
|
||||
|
||||
/**
|
||||
* نمایش دستهبندی و پستهای مرتبط با آن بر اساس slug
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param string $slug
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function getCategoryWithPosts(Request $request, $slug)
|
||||
{
|
||||
// یافتن دستهبندی بر اساس slug
|
||||
$category = Category::where('slug', $slug)->first();
|
||||
|
||||
if (!$category) {
|
||||
return response()->json([
|
||||
'message' => 'دستهبندی مورد نظر یافت نشد.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
// دریافت تعداد پستها در هر صفحه (پیشفرض: ۱۰)
|
||||
$perPage = $request->input('per_page', 10);
|
||||
|
||||
// دریافت پستهای مربوط به این دستهبندی به ترتیب جدیدترینها
|
||||
$posts = $category->posts()->with(['category', 'tags', 'author'])
|
||||
->orderBy('published_at', 'desc')
|
||||
->paginate($perPage);
|
||||
|
||||
// بازگشت اطلاعات دستهبندی و پستها
|
||||
return response()->json([
|
||||
'category' => new CategoryResource($category),
|
||||
'posts' => PostResource::collection($posts),
|
||||
'pagination' => [
|
||||
'total' => $posts->total(),
|
||||
'per_page' => $posts->perPage(),
|
||||
'current_page' => $posts->currentPage(),
|
||||
'last_page' => $posts->lastPage(),
|
||||
'from' => $posts->firstItem(),
|
||||
'to' => $posts->lastItem(),
|
||||
],
|
||||
], 200);
|
||||
}
|
||||
|
||||
// ذخیره یک دستهبندی جدید
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required|string|max:255|unique:categories,name',
|
||||
'slug' => 'required|string|unique:categories,slug',
|
||||
'description' => 'required|string',
|
||||
'short_description' => 'nullable|string|max:500',
|
||||
'image' => 'nullable|url',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json($validator->errors(), 422);
|
||||
}
|
||||
|
||||
$category = Category::create($validator->validated());
|
||||
|
||||
return new CategoryResource($category);
|
||||
}
|
||||
|
||||
// بهروزرسانی یک دستهبندی خاص
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$category = Category::findOrFail($id);
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'sometimes|required|string|max:255|unique:categories,name,' . $category->id,
|
||||
'slug' => 'sometimes|required|string|unique:categories,slug,' . $category->id,
|
||||
'description' => 'sometimes|required|string',
|
||||
'short_description' => 'nullable|string|max:500',
|
||||
'image' => 'nullable|url',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json($validator->errors(), 422);
|
||||
}
|
||||
|
||||
$category->update($validator->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,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\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;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
/**
|
||||
* برگرداندن دادههای مورد نیاز برای صفحهی اصلی
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
// 1. 5 پست آخر
|
||||
$latestPosts = Post::with(['category', 'tags', 'author'])
|
||||
->orderBy('published_at', 'desc')
|
||||
->take(5)
|
||||
->get();
|
||||
|
||||
// 2. 5 دستهبندی آخر
|
||||
$latestCategories = Category::orderBy('created_at', 'desc')
|
||||
->take(5)
|
||||
->get();
|
||||
|
||||
// 3. 5 پست با بیشترین بازدید
|
||||
$mostViewedPosts = Post::with(['category', 'tags', 'author'])
|
||||
->orderBy('views', 'desc')
|
||||
->take(5)
|
||||
->get();
|
||||
|
||||
// 4. 4 پست تصادفی
|
||||
$randomPosts = Post::with(['category', 'tags', 'author'])
|
||||
->inRandomOrder()
|
||||
->take(4)
|
||||
->get();
|
||||
|
||||
// بازگشت به صورت JSON با استفاده از منابع (Resources)
|
||||
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,127 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Blog;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\PostResource;
|
||||
use App\Models\Blog\Post;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class PostController extends Controller
|
||||
{
|
||||
// نمایش لیست پستها با فیلتر و صفحهبندی
|
||||
public function index(Request $request)
|
||||
{
|
||||
// دریافت پارامترهای فیلتر از درخواست
|
||||
$filters = $request->only(['category', 'tag', 'search', 'author', 'published']);
|
||||
|
||||
// دریافت پارامترهای مرتبسازی از درخواست
|
||||
$sortField = $request->get('sort_field', 'published_at'); // فیلد پیشفرض: published_at
|
||||
$sortDirection = $request->get('sort_direction', 'desc'); // جهت پیشفرض: نزولی
|
||||
|
||||
// دریافت پارامترهای صفحهبندی از درخواست
|
||||
$perPage = $request->get('per_page', 10); // تعداد آیتمها در هر صفحه (پیشفرض: ۱۰)
|
||||
|
||||
// ساخت Query با اعمال فیلترها و مرتبسازی
|
||||
$query = Post::with(['category', 'tags', 'author'])
|
||||
->filter($filters)
|
||||
->sort($sortField, $sortDirection);
|
||||
|
||||
// اعمال صفحهبندی
|
||||
$posts = $query->paginate($perPage);
|
||||
|
||||
// بازگشت نتایج به صورت JSON با استفاده از Resource
|
||||
return PostResource::collection($posts);
|
||||
}
|
||||
|
||||
// نمایش یک پست خاص
|
||||
public function show($slug)
|
||||
{
|
||||
// یافتن پست بر اساس slug و بارگذاری روابط مرتبط
|
||||
$post = Post::with(['category', 'tags', 'author', 'relatedPosts.category', 'relatedPosts.tags', 'relatedPosts.author'])
|
||||
->where('slug', $slug)
|
||||
->firstOrFail(); // در صورت عدم وجود پست، خطای 404 برمیگرداند
|
||||
|
||||
// بازگشت پست و پستهای مرتبط به صورت JSON با استفاده از منابع
|
||||
return response()->json([
|
||||
'post' => new PostResource($post),
|
||||
'related_posts' => PostResource::collection($post->relatedPosts->sortByDesc('published_at')->take(5)), // بازگشت ۵ پست مرتبط جدیدترین
|
||||
], 200);
|
||||
}
|
||||
|
||||
// ذخیره یک پست جدید
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'title' => 'required|string|max:255',
|
||||
'body' => 'required|string',
|
||||
'excerpt' => 'nullable|string|max:500',
|
||||
'slug' => 'required|string|unique:posts,slug',
|
||||
'author_id' => 'required|exists:users,id',
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
'featured_image' => 'nullable|url',
|
||||
'views' => 'nullable|integer',
|
||||
'likes' => 'nullable|integer',
|
||||
'published_at' => 'nullable|date',
|
||||
'tags' => 'nullable|array',
|
||||
'tags.*' => 'exists:tags,id',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json($validator->errors(), 422);
|
||||
}
|
||||
|
||||
$post = Post::create($validator->validated());
|
||||
|
||||
if ($request->has('tags')) {
|
||||
$post->tags()->attach($request->tags);
|
||||
}
|
||||
|
||||
return new PostResource($post->load(['category', 'tags', 'author']));
|
||||
}
|
||||
|
||||
// بهروزرسانی یک پست خاص
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$post = Post::findOrFail($id);
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'title' => 'sometimes|required|string|max:255',
|
||||
'body' => 'sometimes|required|string',
|
||||
'excerpt' => 'nullable|string|max:500',
|
||||
'slug' => 'sometimes|required|string|unique:posts,slug,' . $post->id,
|
||||
'author_id' => 'sometimes|required|exists:users,id',
|
||||
'category_id' => 'sometimes|required|exists:categories,id',
|
||||
'featured_image' => 'nullable|url',
|
||||
'views' => 'nullable|integer',
|
||||
'likes' => 'nullable|integer',
|
||||
'published_at' => 'nullable|date',
|
||||
'tags' => 'nullable|array',
|
||||
'tags.*' => 'exists:tags,id',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json($validator->errors(), 422);
|
||||
}
|
||||
|
||||
$post->update($validator->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,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Blog;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\TagResource;
|
||||
use App\Models\Blog\Tag;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
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(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required|string|max:255|unique:tags,name',
|
||||
'slug' => 'required|string|unique:tags,slug',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json($validator->errors(), 422);
|
||||
}
|
||||
|
||||
$tag = Tag::create($validator->validated());
|
||||
|
||||
return new TagResource($tag);
|
||||
}
|
||||
|
||||
// بهروزرسانی یک تگ خاص
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$tag = Tag::findOrFail($id);
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'sometimes|required|string|max:255|unique:tags,name,' . $tag->id,
|
||||
'slug' => 'sometimes|required|string|unique:tags,slug,' . $tag->id,
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json($validator->errors(), 422);
|
||||
}
|
||||
|
||||
$tag->update($validator->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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CategoryResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'description' => $this->description,
|
||||
'short_description' => $this->short_description,
|
||||
'image' => $this->image,
|
||||
'posts_count' => $this->whenLoaded('posts', function () {
|
||||
return $this->posts->count();
|
||||
}),
|
||||
'created_at' => Carbon::parse($this->created_at)->format('M d Y'),
|
||||
'updated_at' => Carbon::parse($this->updated_at)->format('M d Y'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class PostResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'slug' => $this->slug,
|
||||
'excerpt' => $this->excerpt,
|
||||
'body' => $this->body,
|
||||
'author' => new UserResource($this->author),
|
||||
'category' => new CategoryResource($this->category),
|
||||
'tags' => TagResource::collection($this->tags),
|
||||
'featured_image' => $this->featured_image,
|
||||
'views' => $this->views,
|
||||
'likes' => $this->likes,
|
||||
'published_at' => $this->published_at,
|
||||
'created_at' => Carbon::parse($this->created_at)->format('M d Y'),
|
||||
'updated_at' => Carbon::parse($this->updated_at)->format('M d Y'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class TagResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'posts_count' => $this->whenLoaded('posts', function () {
|
||||
return $this->posts->count();
|
||||
}),
|
||||
'created_at' => Carbon::parse($this->created_at)->format('M d Y'),
|
||||
'updated_at' => Carbon::parse($this->updated_at)->format('M d Y'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,12 @@ class User extends ResourceCollection
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return parent::toArray($request);
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
// اضافه کردن سایر فیلدهای مورد نیاز
|
||||
'email' => $this->email,
|
||||
'created_at' => $this->created_at,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
@@ -18,8 +19,8 @@ class UserResource extends JsonResource
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'email' => $this->email,
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
'created_at' => Carbon::parse($this->created_at)->format('M d Y'),
|
||||
'updated_at' => Carbon::parse($this->updated_at)->format('M d Y'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user