Новые правки в компоенты

This commit is contained in:
Web-serfer 2026-04-19 19:00:42 +05:00
parent 36a3d37ad3
commit 815986969a
19 changed files with 1703 additions and 143 deletions

View file

@ -0,0 +1,132 @@
import type { APIRoute } from "astro";
const POCKETBASE_URL = import.meta.env.POCKETBASE_URL || "http://localhost:8090";
export const POST: APIRoute = async ({ request, cookies }) => {
const pbAuthCookie = cookies.get("pb_auth")?.value;
const token = pbAuthCookie?.trim();
if (!token) {
return new Response(
JSON.stringify({ error: "Unauthorized" }),
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
try {
let userId: string | null = null;
const userResponse = await fetch(
`${POCKETBASE_URL}/api/collections/users/auth-refresh`,
{
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
}
);
if (userResponse.ok) {
const userData = await userResponse.json();
userId = userData.record?.id;
}
if (!userId) {
return new Response(
JSON.stringify({ error: "Unauthorized" }),
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
const body = await request.json();
const { name, surname, profession, rating, text } = body;
if (!name || !surname || !profession || !rating || !text) {
return new Response(
JSON.stringify({ error: "Все поля обязательны" }),
{ status: 400, headers: { "Content-Type": "application/json" } }
);
}
if (text.length < 50 || text.length > 2000) {
return new Response(
JSON.stringify({ error: "Текст отзыва должен быть от 50 до 2000 символов" }),
{ status: 400, headers: { "Content-Type": "application/json" } }
);
}
if (rating < 1 || rating > 5) {
return new Response(
JSON.stringify({ error: "Оценка должна быть от 1 до 5" }),
{ status: 400, headers: { "Content-Type": "application/json" } }
);
}
const response = await fetch(
`${POCKETBASE_URL}/api/collections/reviews/records`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
name,
surname,
profession,
rating,
text,
status: "pending",
votesCount: 0,
user: userId,
}),
}
);
const data = await response.json();
if (!response.ok) {
return new Response(JSON.stringify(data), {
status: response.status,
headers: { "Content-Type": "application/json" },
});
}
return new Response(JSON.stringify(data), {
status: 201,
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error("[Reviews API POST] Error:", error);
return new Response(
JSON.stringify({ error: "Failed to create review" }),
{ status: 500, headers: { "Content-Type": "application/json" } }
);
}
};
export const GET: APIRoute = async ({ url }) => {
const status = url.searchParams.get("status") || "published";
const expand = "user";
const sort = "-created";
let filter = `status = "${status}"`;
try {
const response = await fetch(
`${POCKETBASE_URL}/api/collections/reviews/records?expand=${expand}&filter=${encodeURIComponent(filter)}&sort=${sort}`,
{}
);
const data = await response.json();
return new Response(JSON.stringify(data), {
status: 200,
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error("[Reviews API GET] Error:", error);
return new Response(
JSON.stringify({ error: "Failed to fetch reviews" }),
{ status: 500, headers: { "Content-Type": "application/json" } }
);
}
};