Новые изменения в компоненты

This commit is contained in:
Web-serfer 2026-04-26 23:47:23 +05:00
parent faf02848ed
commit 735289481c
7 changed files with 151 additions and 3 deletions

View file

@ -0,0 +1,53 @@
import type { APIRoute } from 'astro';
const POCKETBASE_URL = import.meta.env.POCKETBASE_URL || 'http://127.0.0.1:8090';
const POCKETBASE_ID_REGEX = /^[a-z0-9]{15}$/;
export const POST: APIRoute = async ({ url }) => {
try {
const postId = url.searchParams.get('postId');
if (!postId || !POCKETBASE_ID_REGEX.test(postId)) {
return new Response(
JSON.stringify({ error: 'Некорректный postId' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
const postRes = await fetch(
`${POCKETBASE_URL}/api/collections/posts/records/${postId}`,
);
if (!postRes.ok) {
return new Response(
JSON.stringify({ error: 'Пост не найден' }),
{ status: 404, headers: { 'Content-Type': 'application/json' } }
);
}
const post = await postRes.json();
const newViews = (post.views || 0) + 1;
await fetch(
`${POCKETBASE_URL}/api/collections/posts/records/${postId}`,
{
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ views: newViews }),
}
);
return new Response(
JSON.stringify({ views: newViews }),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
} catch (error) {
console.error('[Increment Views API] Error:', error);
return new Response(
JSON.stringify({ error: 'Внутренняя ошибка сервера' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
};

View file

@ -4,7 +4,7 @@ import { SITE_URL } from '@constants';
import Comments from '@components/blog/comments/Comments.tsx';
import RelatedPosts from '@components/blog/RelatedPosts.astro';
import ArticleTableOfContents from '@components/blog/ArticleTableOfContents.astro';
import { getPostBySlug, getPosts, getPostImageUrl, getPostVotesStats } from '@lib/pb';
import { getPostBySlug, getPosts, getPostImageUrl, getPostVotesStats, getPostViews } from '@lib/pb';
import { marked } from 'marked';
export const prerender = false;
@ -22,6 +22,7 @@ if (!post) {
}
const { likes = 0, dislikes = 0 } = await getPostVotesStats(post.id).catch(() => ({ likes: 0, dislikes: 0 }));
const views = await getPostViews(post.id).catch(() => 0);
// Конвертируем markdown в HTML
const contentHtml = marked(post.content || '');
@ -74,6 +75,7 @@ const heroImage = getPostImageUrl(post);
postUrl={currentUrl}
initialLikes={likes}
initialDislikes={dislikes}
initialViews={views}
>
<!-- Содержимое статьи в article -->
<article class="article-content-wrapper" id="post-content">