Initial commit: Laravel Blog API
This commit is contained in:
144
app/Http/Controllers/Api/CategoryController.php
Normal file
144
app/Http/Controllers/Api/CategoryController.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\CategoryRequest;
|
||||
use App\Http\Resources\CategoryResource;
|
||||
use App\Models\Category;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
/**
|
||||
* List categories.
|
||||
*
|
||||
* @group Categories
|
||||
*
|
||||
* @queryParam search string Filter categories by name. Example: tecnologia
|
||||
* @queryParam per_page integer Number of items per page. Default: 15. Example: 15
|
||||
*/
|
||||
public function index(Request $request): AnonymousResourceCollection|JsonResponse
|
||||
{
|
||||
try {
|
||||
$categories = Category::query()
|
||||
->withCount('posts')
|
||||
->when($request->filled('search'), function ($query) use ($request): void {
|
||||
$query->where('name', 'like', '%' . $request->string('search') . '%');
|
||||
})
|
||||
->orderBy('name')
|
||||
->paginate((int) $request->integer('per_page', 15));
|
||||
|
||||
return CategoryResource::collection($categories);
|
||||
} catch (Throwable $throwable) {
|
||||
Log::error('Failed to list categories.', ['exception' => $throwable]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Não foi possível listar as categorias.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create category.
|
||||
*
|
||||
* @group Categories
|
||||
*
|
||||
* @bodyParam name string required Category name. Example: Tecnologia
|
||||
* @bodyParam slug string Optional custom slug. Example: tecnologia
|
||||
* @bodyParam description string Category description. Example: Notícias sobre tecnologia e inovação.
|
||||
*/
|
||||
public function store(CategoryRequest $request): CategoryResource|JsonResponse
|
||||
{
|
||||
try {
|
||||
$data = $request->validated();
|
||||
$category = Category::query()->create($data);
|
||||
|
||||
return (new CategoryResource($category))
|
||||
->additional(['message' => 'Categoria criada com sucesso.'])
|
||||
->response()
|
||||
->setStatusCode(201);
|
||||
} catch (Throwable $throwable) {
|
||||
Log::error('Failed to create category.', ['exception' => $throwable]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Não foi possível criar a categoria.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show category.
|
||||
*
|
||||
* @group Categories
|
||||
*/
|
||||
public function show(Category $category): CategoryResource
|
||||
{
|
||||
$category->loadCount('posts');
|
||||
|
||||
return new CategoryResource($category);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update category.
|
||||
*
|
||||
* @group Categories
|
||||
*
|
||||
* @bodyParam name string required Category name. Example: Tecnologia
|
||||
* @bodyParam slug string Optional custom slug. Example: tecnologia
|
||||
* @bodyParam description string Category description. Example: Notícias sobre tecnologia e inovação.
|
||||
*/
|
||||
public function update(CategoryRequest $request, Category $category): CategoryResource|JsonResponse
|
||||
{
|
||||
try {
|
||||
$data = $request->validated();
|
||||
|
||||
if ($request->filled('name') && ! $request->filled('slug')) {
|
||||
$data['slug'] = Category::generateUniqueSlug($data['name'], $category->id);
|
||||
}
|
||||
|
||||
$category->update($data);
|
||||
|
||||
return (new CategoryResource($category->refresh()->loadCount('posts')))
|
||||
->additional(['message' => 'Categoria atualizada com sucesso.']);
|
||||
} catch (Throwable $throwable) {
|
||||
Log::error('Failed to update category.', ['category_id' => $category->id, 'exception' => $throwable]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Não foi possível atualizar a categoria.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete category.
|
||||
*
|
||||
* @group Categories
|
||||
*/
|
||||
public function destroy(Category $category): JsonResponse
|
||||
{
|
||||
try {
|
||||
if ($category->posts()->exists()) {
|
||||
return response()->json([
|
||||
'message' => 'Não é possível remover uma categoria vinculada a posts.',
|
||||
], 409);
|
||||
}
|
||||
|
||||
$category->delete();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Categoria removida com sucesso.',
|
||||
]);
|
||||
} catch (Throwable $throwable) {
|
||||
Log::error('Failed to delete category.', ['category_id' => $category->id, 'exception' => $throwable]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Não foi possível remover a categoria.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
171
app/Http/Controllers/Api/PostController.php
Normal file
171
app/Http/Controllers/Api/PostController.php
Normal file
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\PostRequest;
|
||||
use App\Http\Resources\PostResource;
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class PostController extends Controller
|
||||
{
|
||||
/**
|
||||
* List posts.
|
||||
*
|
||||
* @group Posts
|
||||
*
|
||||
* @queryParam search string Filter posts by title. Example: Laravel
|
||||
* @queryParam category_id integer Filter posts by category ID. Example: 1
|
||||
* @queryParam category_slug string Filter posts by category slug. Example: tecnologia
|
||||
* @queryParam is_featured boolean Filter featured posts. Example: true
|
||||
* @queryParam status string Filter by status: draft or published. Example: published
|
||||
* @queryParam per_page integer Number of items per page. Default: 15. Example: 15
|
||||
*/
|
||||
public function index(Request $request): AnonymousResourceCollection|JsonResponse
|
||||
{
|
||||
try {
|
||||
$posts = Post::query()
|
||||
->with('category')
|
||||
->when($request->filled('search'), function ($query) use ($request): void {
|
||||
$query->where('title', 'like', '%' . $request->string('search') . '%');
|
||||
})
|
||||
->when($request->filled('category_id'), function ($query) use ($request): void {
|
||||
$query->where('category_id', $request->integer('category_id'));
|
||||
})
|
||||
->when($request->filled('category_slug'), function ($query) use ($request): void {
|
||||
$query->whereHas('category', function ($categoryQuery) use ($request): void {
|
||||
$categoryQuery->where('slug', $request->string('category_slug'));
|
||||
});
|
||||
})
|
||||
->when($request->has('is_featured'), function ($query) use ($request): void {
|
||||
$query->where('is_featured', $request->boolean('is_featured'));
|
||||
})
|
||||
->when($request->filled('status'), function ($query) use ($request): void {
|
||||
$query->where('status', $request->string('status'));
|
||||
})
|
||||
->orderByDesc('published_at')
|
||||
->orderByDesc('created_at')
|
||||
->paginate((int) $request->integer('per_page', 15));
|
||||
|
||||
return PostResource::collection($posts);
|
||||
} catch (Throwable $throwable) {
|
||||
Log::error('Failed to list posts.', ['exception' => $throwable]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Não foi possível listar os posts.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create post.
|
||||
*
|
||||
* @group Posts
|
||||
*
|
||||
* @bodyParam title string required Post title. Example: Laravel 12 em Microsserviços
|
||||
* @bodyParam slug string Optional custom slug. Example: laravel-12-em-microsservicos
|
||||
* @bodyParam content string required Full post content. Example: Conteúdo completo da notícia.
|
||||
* @bodyParam excerpt string Short post excerpt. Example: Resumo da notícia.
|
||||
* @bodyParam category_id integer required Category ID. Example: 1
|
||||
* @bodyParam featured_image string URL of featured image. Example: https://cdn.example.com/image.jpg
|
||||
* @bodyParam is_featured boolean Whether the post is featured. Example: true
|
||||
* @bodyParam status string required draft or published. Example: published
|
||||
* @bodyParam published_at datetime Publication date. Example: 2026-05-28 10:00:00
|
||||
*/
|
||||
public function store(PostRequest $request): PostResource|JsonResponse
|
||||
{
|
||||
try {
|
||||
$data = $request->validated();
|
||||
$post = Post::query()->create($data);
|
||||
$post->load('category');
|
||||
|
||||
return (new PostResource($post))
|
||||
->additional(['message' => 'Post criado com sucesso.'])
|
||||
->response()
|
||||
->setStatusCode(201);
|
||||
} catch (Throwable $throwable) {
|
||||
Log::error('Failed to create post.', ['exception' => $throwable]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Não foi possível criar o post.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show post.
|
||||
*
|
||||
* @group Posts
|
||||
*/
|
||||
public function show(Post $post): PostResource
|
||||
{
|
||||
$post->load('category');
|
||||
|
||||
return new PostResource($post);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update post.
|
||||
*
|
||||
* @group Posts
|
||||
*
|
||||
* @bodyParam title string required Post title. Example: Laravel 12 em Microsserviços
|
||||
* @bodyParam slug string Optional custom slug. Example: laravel-12-em-microsservicos
|
||||
* @bodyParam content string required Full post content. Example: Conteúdo completo da notícia.
|
||||
* @bodyParam excerpt string Short post excerpt. Example: Resumo da notícia.
|
||||
* @bodyParam category_id integer required Category ID. Example: 1
|
||||
* @bodyParam featured_image string URL of featured image. Example: https://cdn.example.com/image.jpg
|
||||
* @bodyParam is_featured boolean Whether the post is featured. Example: true
|
||||
* @bodyParam status string required draft or published. Example: published
|
||||
* @bodyParam published_at datetime Publication date. Example: 2026-05-28 10:00:00
|
||||
*/
|
||||
public function update(PostRequest $request, Post $post): PostResource|JsonResponse
|
||||
{
|
||||
try {
|
||||
$data = $request->validated();
|
||||
|
||||
if ($request->filled('title') && ! $request->filled('slug')) {
|
||||
$data['slug'] = Post::generateUniqueSlug($data['title'], $post->id);
|
||||
}
|
||||
|
||||
$post->update($data);
|
||||
$post->refresh()->load('category');
|
||||
|
||||
return (new PostResource($post))
|
||||
->additional(['message' => 'Post atualizado com sucesso.']);
|
||||
} catch (Throwable $throwable) {
|
||||
Log::error('Failed to update post.', ['post_id' => $post->id, 'exception' => $throwable]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Não foi possível atualizar o post.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete post.
|
||||
*
|
||||
* @group Posts
|
||||
*/
|
||||
public function destroy(Post $post): JsonResponse
|
||||
{
|
||||
try {
|
||||
$post->delete();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Post removido com sucesso.',
|
||||
]);
|
||||
} catch (Throwable $throwable) {
|
||||
Log::error('Failed to delete post.', ['post_id' => $post->id, 'exception' => $throwable]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Não foi possível remover o post.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user