full settings overhaul
This commit is contained in:
@@ -10,10 +10,19 @@ export async function GET(event: RequestEvent) {
|
||||
const filter = event.url.searchParams.get("filter") ?? "";
|
||||
|
||||
try {
|
||||
const r = await pb.collection('trails')
|
||||
.getList<Trail>(parseInt(page), parseInt(perPage), { expand: expand ?? "", sort: sort ?? "", filter: filter ?? "" })
|
||||
let r;
|
||||
if (parseInt(perPage) < 0) {
|
||||
r = {
|
||||
items: await pb.collection('trails')
|
||||
.getFullList<Trail>({ expand: expand, sort: sort, filter: filter })
|
||||
}
|
||||
} else {
|
||||
r = await pb.collection('trails')
|
||||
.getList<Trail>(parseInt(page), parseInt(perPage), { expand: expand ?? "", sort: sort ?? "", filter: filter ?? "" })
|
||||
}
|
||||
|
||||
return json(r)
|
||||
} catch (e: any) {
|
||||
} catch (e: any) {
|
||||
throw error(e.status, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,48 @@
|
||||
import type { Trail } from "$lib/models/trail";
|
||||
import { trails_create } from "$lib/stores/trail_store";
|
||||
import { gpx2trail } from "$lib/util/gpx_util";
|
||||
import { fromFIT, fromKML, fromTCX, gpx2trail, isFITFile } from "$lib/util/gpx_util";
|
||||
import { error, json, type RequestEvent } from "@sveltejs/kit";
|
||||
import { ClientResponseError } from "pocketbase";
|
||||
|
||||
export async function PUT(event: RequestEvent) {
|
||||
try {
|
||||
const data = await event.request.blob();
|
||||
const gpxString = await data.text();
|
||||
if (!gpxString.length) {
|
||||
const fileBuffer = await data.arrayBuffer();
|
||||
const fileContent = await data.text();
|
||||
let gpxData = ""
|
||||
let gpxFile: Blob;
|
||||
if (isFITFile(fileBuffer)) {
|
||||
gpxData = await fromFIT(fileBuffer);
|
||||
gpxFile = new Blob([gpxData], {
|
||||
type: "application/gpx+xml",
|
||||
});
|
||||
}
|
||||
else if (fileContent.includes("http://www.opengis.net/kml")) {
|
||||
gpxData = fromKML(fileContent);
|
||||
gpxFile = new Blob([gpxData], {
|
||||
type: "application/gpx+xml",
|
||||
});
|
||||
} else if (fileContent.includes("TrainingCenterDatabase")) {
|
||||
gpxData = fromTCX(fileContent);
|
||||
gpxFile = new Blob([gpxData], {
|
||||
type: "application/gpx+xml",
|
||||
});
|
||||
} else {
|
||||
gpxData = fileContent;
|
||||
gpxFile = data
|
||||
}
|
||||
if (!gpxData.length) {
|
||||
throw new ClientResponseError({ status: 400, response: { message: "Empty file" } })
|
||||
}
|
||||
let trail: Trail;
|
||||
try {
|
||||
trail = (await gpx2trail(gpxString)).trail;
|
||||
trail = (await gpx2trail(gpxData)).trail;
|
||||
} catch (e: any) {
|
||||
throw new ClientResponseError({ status: 400, response: { message: "Invalid GPX file" } })
|
||||
throw new ClientResponseError({ status: 400, response: { message: "Invalid file" } })
|
||||
}
|
||||
|
||||
try {
|
||||
trail = await trails_create(trail, [], data, event.fetch);
|
||||
trail = await trails_create(trail, [], gpxFile, event.fetch);
|
||||
} catch (e: any) {
|
||||
console.log(e);
|
||||
|
||||
@@ -28,6 +51,8 @@ export async function PUT(event: RequestEvent) {
|
||||
return json(trail);
|
||||
|
||||
} catch (e: any) {
|
||||
console.log(e);
|
||||
|
||||
throw error(e.status, e)
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,17 @@ export async function GET(event: RequestEvent) {
|
||||
export async function POST(event: RequestEvent) {
|
||||
const data = await event.request.json()
|
||||
try {
|
||||
if (data.email != pb.authStore.model!.email) {
|
||||
const r = await pb.collection('users').requestEmailChange(data.email);
|
||||
pb.authStore.model!.email = data.email;
|
||||
}
|
||||
const r = await pb.collection('users').update<User>(event.params.id as string, data)
|
||||
return json(r);
|
||||
if (data.password) {
|
||||
const r = await pb.collection('users').authWithPassword(data.email ?? data.username!, data.password);
|
||||
return json(r.record)
|
||||
} else {
|
||||
return json(r);
|
||||
}
|
||||
} catch (e: any) {
|
||||
throw error(e.status, e)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user