adds api gpx upload route

This commit is contained in:
Christian Beutel
2024-03-21 18:46:25 +01:00
parent 01dfea0a03
commit 2ab0334579
22 changed files with 625 additions and 35 deletions

View File

@@ -1,9 +1,16 @@
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { ClientResponseError } from "pocketbase";
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
let data;
try {
data = await event.request.json()
} catch (e) {
throw new ClientResponseError({ status: 400, response: { message: "Invalid json" } })
}
const r = await pb.collection('users').authWithPassword(data.email ?? data.username!, data.password);
return json(r);
} catch (e: any) {

View File

@@ -13,7 +13,6 @@ export async function GET(event: RequestEvent) {
} catch (e: any) {
throw error(e.status, e);
}
}
export async function PUT(event: RequestEvent) {

View File

@@ -2,6 +2,16 @@ import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import type { List } from "postcss/lib/list";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('lists')
.getOne<List>(event.params.id as string, { expand: "trails,trails.waypoints,trails.category" })
return json(r)
} catch (e: any) {
throw error(e.status, e);
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
@@ -16,7 +26,7 @@ export async function POST(event: RequestEvent) {
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('lists').delete(event.params.id as string)
return json(r);
return json({ 'acknowledged': r });
} catch (e: any) {
throw error(e.status, e)
}

View File

@@ -2,6 +2,15 @@ import type { SummitLog } from '$lib/models/summit_log';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
try {
const r: SummitLog[] = await pb.collection('summit_logs').getFullList<SummitLog>()
return json(r)
} catch (e: any) {
throw error(e.status, e);
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();

View File

@@ -3,6 +3,16 @@ import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('summit_logs')
.getOne<SummitLog>(event.params.id as string)
return json(r)
} catch (e: any) {
throw error(e.status, e);
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
@@ -16,7 +26,7 @@ export async function POST(event: RequestEvent) {
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('summit_logs').delete(event.params.id as string)
return json(r);
return json({ 'acknowledged': r });
} catch (e: any) {
throw error(e.status, e)
}

View File

@@ -28,7 +28,7 @@ export async function POST(event: RequestEvent) {
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('trails').delete(event.params.id as string)
return json(r);
return json({ 'acknowledged': r });
} catch (e: any) {
throw error(e.status, e)
}

View File

@@ -0,0 +1,33 @@
import type { Trail } from "$lib/models/trail";
import { trails_create } from "$lib/stores/trail_store";
import { gpx2trail } 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 gpx = await data.text();
if (!gpx.length) {
throw new ClientResponseError({ status: 400, response: { message: "Empty file" } })
}
let trail: Trail;
try {
trail = gpx2trail(gpx);
} catch (e: any) {
throw new ClientResponseError({ status: 400, response: { message: "Invalid GPX file" } })
}
try {
await trails_create(trail, [], data, event.fetch);
} catch(e: any) {
console.log(e);
throw e
}
return json(trail);
} catch (e: any) {
throw error(e.status, e)
}
}

View File

@@ -1,10 +1,15 @@
import { env } from '$env/dynamic/public';
import { pb } from '$lib/pocketbase';
import type { User } from '$lib/stores/user_store';
import { error, json, type RequestEvent } from '@sveltejs/kit'
import { ClientResponseError } from 'pocketbase';
export async function PUT(event: RequestEvent) {
const data = await event.request.json()
try {
if (env.PUBLIC_DISABLE_SIGNUP === "true") {
throw new ClientResponseError({ status: 401, response: { messgage: "Forbidden" } })
}
const r = await pb.collection('users').create<User>(data)
return json(r);
} catch (e: any) {

View File

@@ -2,6 +2,16 @@ import { pb } from '$lib/pocketbase';
import type { User } from '$lib/stores/user_store';
import { error, json, type RequestEvent } from '@sveltejs/kit'
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('users')
.getOne<User>(event.params.id as string)
return json(r)
} catch (e: any) {
throw error(e.status, e);
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
@@ -15,7 +25,7 @@ export async function POST(event: RequestEvent) {
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('users').delete(event.params.id as string)
return json(r);
return json({ 'acknowledged': r });
} catch (e: any) {
throw error(e.status, e)
}

View File

@@ -2,6 +2,15 @@ import type { Waypoint } from '$lib/models/waypoint';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
try {
const r: Waypoint[] = await pb.collection('waypoints').getFullList<Waypoint>()
return json(r)
} catch (e: any) {
throw error(e.status, e);
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();

View File

@@ -2,6 +2,15 @@ import type { Waypoint } from "$lib/models/waypoint";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('waypoints')
.getOne<Waypoint>(event.params.id as string)
return json(r)
} catch (e: any) {
throw error(e.status, e);
}
}
export async function POST(event: RequestEvent){
const data = await event.request.json()
@@ -16,7 +25,7 @@ export async function POST(event: RequestEvent){
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('waypoints').delete(event.params.id as string)
return json(r);
return json({ 'acknowledged': r });
} catch (e: any) {
throw error(e.status, e)
}