Насторил работу блога через Backend
This commit is contained in:
parent
014439d565
commit
edd730b438
33 changed files with 1019 additions and 200 deletions
54
frontend/src/pages/api/posts/index.ts
Normal file
54
frontend/src/pages/api/posts/index.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import type { APIRoute } from 'astro';
|
||||
import PocketBase from 'pocketbase';
|
||||
|
||||
export const GET: APIRoute = async ({ url }) => {
|
||||
try {
|
||||
const pb = new PocketBase(import.meta.env.POCKETBASE_URL);
|
||||
|
||||
const page = parseInt(url.searchParams.get('page') || '1');
|
||||
const perPage = parseInt(url.searchParams.get('per_page') || '10');
|
||||
const category = url.searchParams.get('category');
|
||||
const search = url.searchParams.get('search');
|
||||
|
||||
const filter: string[] = ['draft = false'];
|
||||
|
||||
if (category) {
|
||||
filter.push(`category = "${category}"`);
|
||||
}
|
||||
|
||||
if (search) {
|
||||
filter.push(`(title ~ "${search}" || description ~ "${search}")`);
|
||||
}
|
||||
|
||||
const result = await pb.collection('posts').getList(page, perPage, {
|
||||
filter: filter.join(' && '),
|
||||
sort: '-date',
|
||||
});
|
||||
|
||||
return new Response(JSON.stringify({
|
||||
posts: result.items.map(post => ({
|
||||
id: post.id,
|
||||
slug: post.slug,
|
||||
title: post.title,
|
||||
description: post.description,
|
||||
author: post.author,
|
||||
category: post.category,
|
||||
categoryColor: post.categoryColor,
|
||||
date: post.date,
|
||||
readTime: post.readTime,
|
||||
imageUrl: post.imageUrl,
|
||||
})),
|
||||
total: result.totalItems,
|
||||
page: result.pageInfo.page,
|
||||
perPage: result.pageInfo.perPage,
|
||||
totalPages: result.totalPages,
|
||||
}), { status: 200 });
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('API posts error:', error);
|
||||
|
||||
return new Response(JSON.stringify({
|
||||
error: error.message || 'Ошибка при получении постов'
|
||||
}), { status: 500 });
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue