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' } } ); } };