Aktueller Stand
This commit is contained in:
83
app/api/places/autocomplete/route.ts
Normal file
83
app/api/places/autocomplete/route.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "../../../../lib/prisma";
|
||||
import { requireSession } from "../../../../lib/auth-helpers";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { session } = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const input = searchParams.get("input") || "";
|
||||
const countries = searchParams.get("countries") || "de,fr,ch,at";
|
||||
const countryList = countries
|
||||
.split(",")
|
||||
.map((entry) => entry.trim().toLowerCase())
|
||||
.filter(Boolean)
|
||||
.slice(0, 5);
|
||||
const countryParam = countryList.join(",");
|
||||
|
||||
if (input.trim().length < 3) {
|
||||
return NextResponse.json({ predictions: [] });
|
||||
}
|
||||
|
||||
const apiKeySetting = await prisma.setting.findUnique({
|
||||
where: { key: "google_places_api_key" }
|
||||
});
|
||||
const providerSetting = await prisma.setting.findUnique({
|
||||
where: { key: "geocoding_provider" }
|
||||
});
|
||||
const provider =
|
||||
providerSetting?.value || (apiKeySetting?.value ? "google" : "osm");
|
||||
|
||||
if (provider === "google") {
|
||||
if (!apiKeySetting?.value) {
|
||||
return NextResponse.json({ predictions: [] });
|
||||
}
|
||||
|
||||
const apiUrl = new URL("https://maps.googleapis.com/maps/api/place/autocomplete/json");
|
||||
apiUrl.searchParams.set("input", input);
|
||||
apiUrl.searchParams.set("key", apiKeySetting.value);
|
||||
apiUrl.searchParams.set("language", "de");
|
||||
apiUrl.searchParams.set("types", "geocode");
|
||||
if (countryParam) {
|
||||
apiUrl.searchParams.set("components", `country:${countryParam}`);
|
||||
}
|
||||
|
||||
const response = await fetch(apiUrl.toString());
|
||||
if (!response.ok) {
|
||||
return NextResponse.json({ predictions: [] });
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
return NextResponse.json({ predictions: payload.predictions || [] });
|
||||
}
|
||||
|
||||
const apiUrl = new URL("https://nominatim.openstreetmap.org/search");
|
||||
apiUrl.searchParams.set("format", "jsonv2");
|
||||
apiUrl.searchParams.set("q", input);
|
||||
apiUrl.searchParams.set("addressdetails", "1");
|
||||
apiUrl.searchParams.set("limit", "5");
|
||||
apiUrl.searchParams.set("accept-language", "de");
|
||||
if (countryParam) {
|
||||
apiUrl.searchParams.set("countrycodes", countryParam);
|
||||
}
|
||||
|
||||
const userAgent = process.env.NOMINATIM_USER_AGENT || "vereinskalender/1.0";
|
||||
const response = await fetch(apiUrl.toString(), {
|
||||
headers: { "User-Agent": userAgent }
|
||||
});
|
||||
if (!response.ok) {
|
||||
return NextResponse.json({ predictions: [] });
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
const predictions = Array.isArray(payload)
|
||||
? payload.map((item: any) => ({
|
||||
description: item.display_name,
|
||||
place_id: String(item.place_id)
|
||||
}))
|
||||
: [];
|
||||
return NextResponse.json({ predictions });
|
||||
}
|
||||
71
app/api/places/details/route.ts
Normal file
71
app/api/places/details/route.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "../../../../lib/prisma";
|
||||
import { requireSession } from "../../../../lib/auth-helpers";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { session } = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const placeId = searchParams.get("placeId") || "";
|
||||
|
||||
if (!placeId) {
|
||||
return NextResponse.json({ error: "PlaceId erforderlich." }, { status: 400 });
|
||||
}
|
||||
|
||||
const apiKeySetting = await prisma.setting.findUnique({
|
||||
where: { key: "google_places_api_key" }
|
||||
});
|
||||
const providerSetting = await prisma.setting.findUnique({
|
||||
where: { key: "geocoding_provider" }
|
||||
});
|
||||
const provider =
|
||||
providerSetting?.value || (apiKeySetting?.value ? "google" : "osm");
|
||||
|
||||
if (provider === "google") {
|
||||
if (!apiKeySetting?.value) {
|
||||
return NextResponse.json({ error: "API-Key fehlt." }, { status: 400 });
|
||||
}
|
||||
|
||||
const apiUrl = new URL("https://maps.googleapis.com/maps/api/place/details/json");
|
||||
apiUrl.searchParams.set("place_id", placeId);
|
||||
apiUrl.searchParams.set("fields", "geometry/location");
|
||||
apiUrl.searchParams.set("key", apiKeySetting.value);
|
||||
|
||||
const response = await fetch(apiUrl.toString());
|
||||
if (!response.ok) {
|
||||
return NextResponse.json({ error: "Fehler beim Laden." }, { status: 400 });
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
const location = payload?.result?.geometry?.location;
|
||||
return NextResponse.json({
|
||||
lat: location?.lat ?? null,
|
||||
lng: location?.lng ?? null
|
||||
});
|
||||
}
|
||||
|
||||
const apiUrl = new URL("https://nominatim.openstreetmap.org/details");
|
||||
apiUrl.searchParams.set("place_id", placeId);
|
||||
apiUrl.searchParams.set("format", "json");
|
||||
|
||||
const userAgent = process.env.NOMINATIM_USER_AGENT || "vereinskalender/1.0";
|
||||
const response = await fetch(apiUrl.toString(), {
|
||||
headers: { "User-Agent": userAgent }
|
||||
});
|
||||
if (!response.ok) {
|
||||
return NextResponse.json({ error: "Fehler beim Laden." }, { status: 400 });
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
const coords = payload?.centroid?.coordinates;
|
||||
const lat = Array.isArray(coords) ? Number(coords[1]) : Number(payload?.lat);
|
||||
const lng = Array.isArray(coords) ? Number(coords[0]) : Number(payload?.lon);
|
||||
|
||||
return NextResponse.json({
|
||||
lat: Number.isFinite(lat) ? lat : null,
|
||||
lng: Number.isFinite(lng) ? lng : null
|
||||
});
|
||||
}
|
||||
70
app/api/places/reverse/route.ts
Normal file
70
app/api/places/reverse/route.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "../../../../lib/prisma";
|
||||
import { requireSession } from "../../../../lib/auth-helpers";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { session } = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const lat = searchParams.get("lat");
|
||||
const lng = searchParams.get("lng");
|
||||
|
||||
if (!lat || !lng) {
|
||||
return NextResponse.json({ error: "Koordinaten erforderlich." }, { status: 400 });
|
||||
}
|
||||
|
||||
const apiKeySetting = await prisma.setting.findUnique({
|
||||
where: { key: "google_places_api_key" }
|
||||
});
|
||||
const providerSetting = await prisma.setting.findUnique({
|
||||
where: { key: "geocoding_provider" }
|
||||
});
|
||||
const provider =
|
||||
providerSetting?.value || (apiKeySetting?.value ? "google" : "osm");
|
||||
|
||||
if (provider === "google") {
|
||||
if (!apiKeySetting?.value) {
|
||||
return NextResponse.json({ error: "API-Key fehlt." }, { status: 400 });
|
||||
}
|
||||
|
||||
const apiUrl = new URL("https://maps.googleapis.com/maps/api/geocode/json");
|
||||
apiUrl.searchParams.set("latlng", `${lat},${lng}`);
|
||||
apiUrl.searchParams.set("key", apiKeySetting.value);
|
||||
apiUrl.searchParams.set("language", "de");
|
||||
|
||||
const response = await fetch(apiUrl.toString());
|
||||
if (!response.ok) {
|
||||
return NextResponse.json({ error: "Fehler beim Laden." }, { status: 400 });
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
const result = payload?.results?.[0];
|
||||
return NextResponse.json({
|
||||
label: result?.formatted_address || null,
|
||||
placeId: result?.place_id || null
|
||||
});
|
||||
}
|
||||
|
||||
const apiUrl = new URL("https://nominatim.openstreetmap.org/reverse");
|
||||
apiUrl.searchParams.set("format", "jsonv2");
|
||||
apiUrl.searchParams.set("lat", lat);
|
||||
apiUrl.searchParams.set("lon", lng);
|
||||
apiUrl.searchParams.set("addressdetails", "1");
|
||||
|
||||
const userAgent = process.env.NOMINATIM_USER_AGENT || "vereinskalender/1.0";
|
||||
const response = await fetch(apiUrl.toString(), {
|
||||
headers: { "User-Agent": userAgent }
|
||||
});
|
||||
if (!response.ok) {
|
||||
return NextResponse.json({ error: "Fehler beim Laden." }, { status: 400 });
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
return NextResponse.json({
|
||||
label: payload?.display_name || null,
|
||||
placeId: payload?.place_id ? String(payload.place_id) : null
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user