1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-05 05:05:17 +02:00

Add POST endpoint to create new adventure and update adventureService

This commit is contained in:
Sean Morley 2024-04-10 14:51:51 +00:00
parent 7ba879f7d9
commit ed9a579fc2
3 changed files with 88 additions and 30 deletions

View file

@ -62,14 +62,63 @@ export async function DELETE(event: RequestEvent): Promise<Response> {
)
.execute();
console.log(res);
console.log(id);
console.log(event.locals.user.id);
return new Response(JSON.stringify({ id: id, res: res }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}
// add the adventure to the user's visited list
export async function POST(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "No user found" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
// get properties from the body
const { name, location, created } = await event.request.json();
// insert the adventure to the user's visited list
await db
.insert(userVisitedAdventures)
.values({
userId: event.locals.user.id,
adventureName: name,
location: location,
visitedDate: created,
})
.execute();
let res = await db
.select()
.from(userVisitedAdventures)
.where(
and(
eq(userVisitedAdventures.userId, event.locals.user.id),
eq(userVisitedAdventures.adventureName, name),
eq(userVisitedAdventures.location, location),
eq(userVisitedAdventures.visitedDate, created)
)
)
.execute();
// return a response with the adventure object values
return new Response(
JSON.stringify({
adventure: { name, location, created },
message: { message: "Adventure added" },
id: res[0].adventureID
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
}

View file

@ -53,17 +53,40 @@
const createNewAdventure = () => {
let currentDate = new Date();
let dateString = currentDate.toISOString().slice(0, 10); // Get date in "yyyy-mm-dd" format
const newAdventure: Adventure = {
id: getNextId(),
name: newName,
location: newLocation,
created: dateString,
};
addAdventure(newAdventure);
newName = ""; // Reset newName and newLocation after adding adventure
newLocation = "";
adventures = getAdventures(); // add to local array
showToast("added");
// post to /api/visits
fetch("/api/visits", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: newName,
location: newLocation,
created: dateString,
}),
})
.then((response) => response.json())
.then((data) => {
console.log("Success:", data);
let newId = data.id;
console.log("New ID: " + newId);
console.log("New Name: " + newName);
adventures = [
...adventures,
{
id: newId,
name: newName,
location: newLocation,
created: dateString,
},
];
newName = ""; // Reset newName and newLocation after adding adventure
newLocation = "";
showToast("added");
})
.catch((error) => {
console.error("Error:", error);
});
};
// function triggerRemoveAdventure(event: { detail: number }) {

View file

@ -7,13 +7,7 @@ import { visitCount } from "$lib/utils/stores/visitCountStore";
// Check if localStorage is available (browser environment)
const isBrowser = typeof window !== "undefined";
// // Load adventures from localStorage on startup (only in the browser)
// if (isBrowser) {
// const storedAdventures = localStorage.getItem("adventures");
// if (storedAdventures) {
// adventures = JSON.parse(storedAdventures);
// }
// }
//
export function getNextId() {
let nextId = Math.max(0, ...adventures.map((adventure) => adventure.id)) + 1;
@ -37,14 +31,6 @@ export function getAdventures(): Adventure[] {
return adventures;
}
export function removeAdventure(event: { detail: number }) {
adventures = adventures.filter((adventure) => adventure.id !== event.detail);
if (isBrowser) {
localStorage.setItem("adventures", JSON.stringify(adventures));
visitCount.update((n) => n - 1);
}
}
export function saveEdit(adventure: Adventure) {
let editId = adventure.id;
console.log("saving edit");