Api do wyszukiwania dostepności, korekty w powiązanych stronach

This commit is contained in:
dm
2025-11-24 08:40:05 +01:00
parent 20ef0d5293
commit e8881dd23b
22 changed files with 15600 additions and 225 deletions

View File

@@ -0,0 +1,18 @@
import type { APIRoute } from "astro";
import Database from "better-sqlite3";
type CityRow = { city: string };
export const GET: APIRoute = async () => {
const db = new Database("./src/data/ServicesRange.db", { readonly: true });
const rows = db
.prepare(`SELECT DISTINCT city FROM ranges ORDER BY city`)
.all() as CityRow[];
const cities = rows.map(r => r.city);
return new Response(JSON.stringify(cities), {
headers: { "Content-Type": "application/json" },
});
};

View File

@@ -0,0 +1,47 @@
import type { APIRoute } from "astro";
import Database from "better-sqlite3";
// 🔥 Funkcja normalizująca — identyczna jak w C#
function normalize(input: string): string {
return input
.normalize("NFD")
.replace(/\p{Diacritic}/gu, "") // usuwa ogonki
.toLowerCase();
}
type CityRow = {
city: string;
};
export const GET: APIRoute = async ({ request }) => {
const url = new URL(request.url);
const q = url.searchParams.get("q")?.trim() || "";
if (q.length < 2) {
return new Response(JSON.stringify([]), {
headers: { "Content-Type": "application/json" },
});
}
const db = new Database("./src/data/ServicesRange.db", { readonly: true });
// 🔥 Pobieramy wszystkie miasta (jest ich mało to działa błyskawicznie)
const stmt = db.prepare(`
SELECT DISTINCT city
FROM ranges
`);
const rows = stmt.all() as CityRow[];
// 🔥 Normalizujemy po stronie Node — ZERO problemów z kolacją SQLite
const pattern = normalize(q);
const filtered = rows
.filter((row) => normalize(row.city).includes(pattern))
.slice(0, 20) // LIMIT 20
.map((r) => r.city);
return new Response(JSON.stringify(filtered), {
headers: { "Content-Type": "application/json" },
});
};

52
src/pages/api/contact.ts Normal file
View File

@@ -0,0 +1,52 @@
import type { APIRoute } from "astro";
import nodemailer from "nodemailer";
import yaml from "js-yaml";
import fs from "fs";
export const POST: APIRoute = async ({ request }) => {
try {
const form = await request.json();
// Wczytanie YAML
const mailCfg = yaml.load(
fs.readFileSync("./src/content/contact/contact.yaml", "utf8")
) as any;
// Stworzenie transportera
const transporter = nodemailer.createTransport({
host: import.meta.env.SMTP_HOST,
port: Number(import.meta.env.SMTP_PORT),
secure: true,
auth: {
user: import.meta.env.SMTP_USER,
pass: import.meta.env.SMTP_PASS,
},
tls: {
rejectUnauthorized: false,
},
});
// Wysyłka
await transporter.sendMail({
from: `"${mailCfg.mail.from_name}" <${import.meta.env.SMTP_USER}>`,
to: mailCfg.mail.to,
subject: `Od ${form.firstName} ${form.lastName}`,
text: `
Imię: ${form.firstName}
Nazwisko: ${form.lastName}
Email: ${form.email}
Telefon: ${form.phone}
Temat: ${form.subject}
Wiadomość:
${form.message}
`,
});
return new Response(JSON.stringify({ ok: true }), { status: 200 });
} catch (error) {
console.error("MAIL ERROR:", error);
return new Response(JSON.stringify({ ok: false, error }), { status: 500 });
}
};

View File

@@ -0,0 +1,35 @@
import type { APIRoute } from "astro";
import Database from "better-sqlite3";
interface HasStreetsRow {
cnt: number;
}
export const GET: APIRoute = async ({ request }) => {
const url = new URL(request.url);
const city = url.searchParams.get("city")?.trim() || "";
if (!city) {
return new Response(JSON.stringify({ hasStreets: false }), {
headers: { "Content-Type": "application/json" },
});
}
const db = new Database("./src/data/ServicesRange.db", { readonly: true });
const stmt = db.prepare(`
SELECT COUNT(*) AS cnt
FROM ranges
WHERE LOWER(city) = LOWER(?)
AND TRIM(street) <> ''
`);
const row = stmt.get(city) as HasStreetsRow;
return new Response(
JSON.stringify({
hasStreets: row.cnt > 0,
}),
{ headers: { "Content-Type": "application/json" } }
);
};

85
src/pages/api/search.ts Normal file
View File

@@ -0,0 +1,85 @@
import type { APIRoute } from "astro";
import Database from "better-sqlite3";
interface RangeRow {
city: string;
street: string;
number: string;
availableFiber: number;
availableRadio: number;
lat: number;
lon: number;
}
export const POST: APIRoute = async ({ request }) => {
try {
const body = await request.json();
const city = body.city?.trim() || "";
const street = body.street?.trim() || "";
const number = body.number?.trim() || "";
if (!city || !number) {
return new Response(JSON.stringify({ ok: false }), {
headers: { "Content-Type": "application/json" },
});
}
const db = new Database("./src/data/ServicesRange.db", { readonly: true });
const stmtHas = db.prepare(`
SELECT COUNT(*) AS cnt
FROM ranges
WHERE LOWER(city) = LOWER(?) AND TRIM(street) <> ''
`);
const has = (stmtHas.get(city) as { cnt: number }).cnt > 0;
let row: RangeRow | undefined;
if (!has) {
const stmt = db.prepare(`
SELECT *
FROM ranges
WHERE LOWER(city) = LOWER(?)
AND TRIM(number) = ?
LIMIT 1
`);
row = stmt.get(city, number) as RangeRow | undefined;
} else if (street === "") {
const stmt = db.prepare(`
SELECT *
FROM ranges
WHERE LOWER(city) = LOWER(?)
AND TRIM(number) = ?
LIMIT 1
`);
row = stmt.get(city, number) as RangeRow | undefined;
} else {
const stmt = db.prepare(`
SELECT *
FROM ranges
WHERE LOWER(city) = LOWER(?)
AND LOWER(street) = LOWER(?)
AND TRIM(number) = ?
LIMIT 1
`);
row = stmt.get(city, street, number) as RangeRow | undefined;
}
if (!row) {
return new Response(JSON.stringify({ ok: false }), {
headers: { "Content-Type": "application/json" },
});
}
return new Response(
JSON.stringify({ ok: true, result: row }),
{ headers: { "Content-Type": "application/json" } }
);
} catch (err) {
console.error("SEARCH API ERROR:", err);
return new Response(JSON.stringify({ ok: false }), { status: 500 });
}
};

View File

@@ -0,0 +1,51 @@
import type { APIRoute } from "astro";
import Database from "better-sqlite3";
type StreetRow = {
street: string;
};
// 🔥 Funkcja normalizująca — taka sama jak dla miast
function normalize(input: string): string {
return input
.normalize("NFD")
.replace(/\p{Diacritic}/gu, "") // usuwa ogonki
.toLowerCase();
}
export const GET: APIRoute = async ({ request }) => {
const url = new URL(request.url);
const city = url.searchParams.get("city")?.trim() || "";
const q = url.searchParams.get("q")?.trim() || "";
if (!city || q.length < 1) {
return new Response(JSON.stringify([]), {
headers: { "Content-Type": "application/json" },
});
}
const db = new Database("./src/data/ServicesRange.db", { readonly: true });
// 🔥 Pobieramy WSZYSTKIE ulice tego miasta (jest ich mało)
const stmt = db.prepare(`
SELECT DISTINCT street
FROM ranges
WHERE city = ?
ORDER BY street
`);
const rows = stmt.all(city) as StreetRow[];
// 🔥 Normalizujemy porównanie — SQLite LIKE NIE JEST UŻYWANE
const pattern = normalize(q);
const filtered = rows
.filter((row) => normalize(row.street).includes(pattern))
.slice(0, 20)
.map((r) => r.street);
return new Response(JSON.stringify(filtered), {
headers: { "Content-Type": "application/json" },
});
};