Initial baseline: LaraBlog core with plugin commerce surface.
Captures the current working tree after theme slots, ArticleAccess, and the payment / paid-content plugins so subsequent work has a reviewable git history.
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Domain\Blog\ArticleAccess;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Article;
|
||||
use App\Settings\BlogSettings;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ArticleController extends Controller
|
||||
{
|
||||
public function index(Request $request, BlogSettings $blog): JsonResponse
|
||||
{
|
||||
$perPage = max(1, min(50, $request->integer('per_page') ?: $blog->posts_per_page));
|
||||
|
||||
$articles = Article::query()
|
||||
->with(['category:id,name', 'user:id,name,username', 'tags:id,name'])
|
||||
->visible()
|
||||
->published()
|
||||
->orderByDesc('stick')
|
||||
->orderByDesc('published_at')
|
||||
->paginate($perPage);
|
||||
|
||||
return response()->json([
|
||||
'data' => $articles->getCollection()->map(fn (Article $article) => $this->summary($article))->values(),
|
||||
'meta' => [
|
||||
'current_page' => $articles->currentPage(),
|
||||
'last_page' => $articles->lastPage(),
|
||||
'per_page' => $articles->perPage(),
|
||||
'total' => $articles->total(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(int $id, ArticleAccess $access): JsonResponse
|
||||
{
|
||||
$article = Article::query()
|
||||
->with(['category:id,name', 'user:id,name,username', 'tags:id,name'])
|
||||
->visible()
|
||||
->published()
|
||||
->findOrFail($id);
|
||||
|
||||
// Anonymous API: never pass a user (purchased/admin full text is Web-only this phase).
|
||||
$decision = $access->resolve($article, null, []);
|
||||
|
||||
$payload = [
|
||||
...$this->summary($article),
|
||||
'content_format' => $article->content_format,
|
||||
'keywords' => $article->keywords,
|
||||
'access' => [
|
||||
'status' => $decision->status,
|
||||
'message' => $decision->message,
|
||||
'checkout_url' => $decision->checkoutUrl,
|
||||
],
|
||||
];
|
||||
|
||||
if ($decision->isAllow()) {
|
||||
$payload['content'] = $article->content;
|
||||
$payload['content_html'] = $article->renderedHtml();
|
||||
} else {
|
||||
$payload['content'] = null;
|
||||
$payload['content_html'] = $access->publicHtml($article, $decision);
|
||||
$payload['teaser_html'] = $payload['content_html'];
|
||||
}
|
||||
|
||||
return response()->json(['data' => $payload]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function summary(Article $article): array
|
||||
{
|
||||
return [
|
||||
'id' => $article->id,
|
||||
'title' => $article->title,
|
||||
'slug' => $article->slug,
|
||||
'description' => $article->description,
|
||||
'published_at' => optional($article->published_at)?->toIso8601String(),
|
||||
'views' => $article->views,
|
||||
'stick' => (bool) $article->stick,
|
||||
'url' => url('/show-'.$article->id.'.shtml'),
|
||||
'category' => $article->category ? [
|
||||
'id' => $article->category->id,
|
||||
'name' => $article->category->name,
|
||||
] : null,
|
||||
'author' => $article->user ? [
|
||||
'id' => $article->user->id,
|
||||
'name' => $article->user->name,
|
||||
'username' => $article->user->username,
|
||||
] : null,
|
||||
'tags' => $article->tags->map(fn ($tag) => [
|
||||
'id' => $tag->id,
|
||||
'name' => $tag->name,
|
||||
])->values(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Article;
|
||||
use App\Models\Category;
|
||||
use App\Settings\BlogSettings;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$categories = Category::query()->orderBy('display_order')->get(['id', 'name', 'articles_count', 'display_order']);
|
||||
|
||||
return response()->json([
|
||||
'data' => $categories,
|
||||
]);
|
||||
}
|
||||
|
||||
public function articles(Request $request, int $id, BlogSettings $blog): JsonResponse
|
||||
{
|
||||
Category::query()->findOrFail($id);
|
||||
$perPage = max(1, min(50, $request->integer('per_page') ?: $blog->posts_per_page));
|
||||
|
||||
$articles = Article::query()
|
||||
->with(['category:id,name', 'tags:id,name'])
|
||||
->visible()
|
||||
->published()
|
||||
->where('category_id', $id)
|
||||
->orderByDesc('published_at')
|
||||
->paginate($perPage);
|
||||
|
||||
return response()->json([
|
||||
'data' => $articles->items(),
|
||||
'meta' => [
|
||||
'current_page' => $articles->currentPage(),
|
||||
'last_page' => $articles->lastPage(),
|
||||
'per_page' => $articles->perPage(),
|
||||
'total' => $articles->total(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Settings\GeneralSettings;
|
||||
use App\Settings\SeoSettings;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class MetaController extends Controller
|
||||
{
|
||||
public function show(GeneralSettings $general, SeoSettings $seo): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'name' => $general->site_name,
|
||||
'url' => $general->site_url,
|
||||
'description' => $general->site_description,
|
||||
'theme' => $general->active_theme,
|
||||
'seo' => [
|
||||
'default_description' => $seo->default_description,
|
||||
'default_keywords' => $seo->default_keywords,
|
||||
'robots_index' => $seo->robots_index,
|
||||
],
|
||||
'api' => [
|
||||
'version' => 'v1',
|
||||
'openapi' => url('/docs/api/openapi.yaml'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Tag;
|
||||
use App\Settings\BlogSettings;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class TagController extends Controller
|
||||
{
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'data' => Tag::query()->orderByDesc('use_count')->get(['id', 'name', 'use_count']),
|
||||
]);
|
||||
}
|
||||
|
||||
public function articles(Request $request, string $name, BlogSettings $blog): JsonResponse
|
||||
{
|
||||
$tag = Tag::query()->where('name', $name)->firstOrFail();
|
||||
$perPage = max(1, min(50, $request->integer('per_page') ?: $blog->posts_per_page));
|
||||
|
||||
$articles = $tag->articles()
|
||||
->with(['category:id,name'])
|
||||
->visible()
|
||||
->published()
|
||||
->orderByDesc('published_at')
|
||||
->paginate($perPage);
|
||||
|
||||
return response()->json([
|
||||
'data' => $articles->items(),
|
||||
'meta' => [
|
||||
'tag' => ['id' => $tag->id, 'name' => $tag->name],
|
||||
'current_page' => $articles->currentPage(),
|
||||
'last_page' => $articles->lastPage(),
|
||||
'per_page' => $articles->perPage(),
|
||||
'total' => $articles->total(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user