mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-05 13:15:18 +02:00
Refactor error handling and add validation for adventure name in server and page files
This commit is contained in:
parent
d84c9f4d24
commit
88a2ac07e6
2 changed files with 27 additions and 20 deletions
|
@ -1,5 +1,5 @@
|
||||||
import { lucia } from "$lib/server/auth";
|
import { lucia } from "$lib/server/auth";
|
||||||
import type { RequestEvent } from "@sveltejs/kit";
|
import { error, type RequestEvent } from "@sveltejs/kit";
|
||||||
import { adventureTable } from "$lib/db/schema";
|
import { adventureTable } from "$lib/db/schema";
|
||||||
import { db } from "$lib/db/db.server";
|
import { db } from "$lib/db/db.server";
|
||||||
import { and, eq } from "drizzle-orm";
|
import { and, eq } from "drizzle-orm";
|
||||||
|
@ -88,38 +88,35 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
||||||
console.log(newAdventure);
|
console.log(newAdventure);
|
||||||
const { name, location, date, description } = newAdventure;
|
const { name, location, date, description } = newAdventure;
|
||||||
|
|
||||||
|
if (!name) {
|
||||||
|
return error(400, {
|
||||||
|
message: "Name field is required!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// insert the adventure to the user's visited list
|
// insert the adventure to the user's visited list
|
||||||
await db
|
let res = await db
|
||||||
.insert(adventureTable)
|
.insert(adventureTable)
|
||||||
.values({
|
.values({
|
||||||
userId: event.locals.user.id,
|
userId: event.locals.user.id,
|
||||||
type: "mylog",
|
type: "mylog",
|
||||||
name: name,
|
name: name,
|
||||||
location: location,
|
location: location || null,
|
||||||
date: date,
|
date: date || null,
|
||||||
description: description,
|
description: description || null,
|
||||||
})
|
})
|
||||||
|
.returning({ insertedId: adventureTable.id })
|
||||||
.execute();
|
.execute();
|
||||||
let res = await db
|
|
||||||
.select()
|
let insertedId = res[0].insertedId;
|
||||||
.from(adventureTable)
|
console.log(insertedId);
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(adventureTable.userId, event.locals.user.id),
|
|
||||||
eq(adventureTable.name, name),
|
|
||||||
eq(adventureTable.location, location),
|
|
||||||
eq(adventureTable.date, date),
|
|
||||||
eq(adventureTable.description, description)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
// return a response with the adventure object values
|
// return a response with the adventure object values
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
adventure: { name, location, date },
|
adventure: { name, location, date },
|
||||||
message: { message: "Adventure added" },
|
message: { message: "Adventure added" },
|
||||||
id: res[0].id,
|
id: insertedId,
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
status: 200,
|
status: 200,
|
||||||
|
|
|
@ -78,7 +78,16 @@
|
||||||
newAdventure,
|
newAdventure,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
.then((response) => response.json())
|
.then((response) => {
|
||||||
|
if (!response.ok) {
|
||||||
|
return response.json().then((data) => {
|
||||||
|
throw new Error(
|
||||||
|
data.error || `Failed to add adventure - ${data?.message}`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
let newId = data.id;
|
let newId = data.id;
|
||||||
// add to local array for instant view update
|
// add to local array for instant view update
|
||||||
|
@ -100,6 +109,7 @@
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error("Error:", error);
|
console.error("Error:", error);
|
||||||
|
showToast(error.message);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue