mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-04 20:55:19 +02:00
Add GET request handler for retrieving an adventure by ID
This commit is contained in:
parent
7327fefe15
commit
ba84fbdcf3
2 changed files with 44 additions and 4 deletions
44
src/routes/api/adventure/+server.ts
Normal file
44
src/routes/api/adventure/+server.ts
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import { db } from "$lib/db/db.server";
|
||||||
|
import { adventureTable } from "$lib/db/schema";
|
||||||
|
import { json, type RequestEvent, type RequestHandler } from "@sveltejs/kit";
|
||||||
|
import { and, eq } from "drizzle-orm";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the GET request for retrieving an adventure by ID.
|
||||||
|
* @param {Request} request - The request object.
|
||||||
|
* @param {Response} response - The response object.
|
||||||
|
* @returns {Promise<void>} - A promise that resolves when the request is handled.
|
||||||
|
*/
|
||||||
|
export const GET: RequestHandler = async ({ url, locals }) => {
|
||||||
|
const id = url.searchParams.get("id");
|
||||||
|
const user = locals.user;
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return json({ error: "Unauthorized" }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id) {
|
||||||
|
return json({ error: "Missing adventure ID" }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const adventure = await db
|
||||||
|
.select()
|
||||||
|
.from(adventureTable)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(adventureTable.id, Number(id)), // Convert id to number
|
||||||
|
eq(adventureTable.userId, user.id)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.limit(1)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
if (adventure.length === 0) {
|
||||||
|
return json({ error: "Adventure not found" }, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
// console.log("GET /api/adventure?id=", id);
|
||||||
|
// console.log("User:", user);
|
||||||
|
|
||||||
|
return json({ adventure }, { status: 200 });
|
||||||
|
};
|
|
@ -17,10 +17,6 @@
|
||||||
let newName = "";
|
let newName = "";
|
||||||
let newLocation = "";
|
let newLocation = "";
|
||||||
|
|
||||||
// let editId: number = NaN;
|
|
||||||
// let editName: string = "";
|
|
||||||
// let editLocation: string = "";
|
|
||||||
// let editdate: string = "";
|
|
||||||
let adventureToEdit: Adventure | undefined;
|
let adventureToEdit: Adventure | undefined;
|
||||||
|
|
||||||
let isShowingToast: boolean = false;
|
let isShowingToast: boolean = false;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue