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

Add PUT route to update existing adventure and refactor adventureService

This commit is contained in:
Sean Morley 2024-04-10 14:58:42 +00:00
parent ed9a579fc2
commit 45545fd8ce
3 changed files with 74 additions and 43 deletions

View file

@ -121,4 +121,48 @@ let res = await db
}, },
} }
); );
}
// put route to update existing adventure
export async function PUT(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 { id, name, location, created } = await event.request.json();
// update the adventure in the user's visited list
await db
.update(userVisitedAdventures)
.set({
adventureName: name,
location: location,
visitedDate: created,
})
.where(
and(
eq(userVisitedAdventures.userId, event.locals.user.id),
eq(userVisitedAdventures.adventureID, Number(id))
)
)
.execute();
return new Response(
JSON.stringify({
adventure: { id, name, location, created },
message: { message: "Adventure updated" },
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
} }

View file

@ -9,7 +9,6 @@
clearAdventures, clearAdventures,
getAdventures, getAdventures,
getNextId, getNextId,
saveEdit,
} from "../../services/adventureService"; } from "../../services/adventureService";
import { onMount } from "svelte"; import { onMount } from "svelte";
import { exportData } from "../../services/export"; import { exportData } from "../../services/export";
@ -67,10 +66,8 @@
}) })
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
console.log("Success:", data);
let newId = data.id; let newId = data.id;
console.log("New ID: " + newId); // add to local array for instant view update
console.log("New Name: " + newName);
adventures = [ adventures = [
...adventures, ...adventures,
{ {
@ -89,25 +86,37 @@
}); });
}; };
// function triggerRemoveAdventure(event: { detail: number }) {
// removeAdventure(event);
// showToast("removed");
// adventures = getAdventures();
// // remove from data.result.adventures
// data.result.adventures = data.result.adventures.filter(
// (adventure: Adventure) => adventure.id !== event.detail,
// );
// }
function saveAdventure(event: { detail: Adventure }) { function saveAdventure(event: { detail: Adventure }) {
console.log("Event" + event.detail); console.log("Event" + event.detail);
saveEdit(event.detail); // put request to /api/visits with id and advneture data
editId = NaN; fetch("/api/visits", {
editName = ""; method: "PUT",
editLocation = ""; headers: {
editCreated = ""; "Content-Type": "application/json",
adventures = getAdventures(); },
showToast("edited"); body: JSON.stringify({
id: event.detail.id,
name: event.detail.name,
location: event.detail.location,
created: event.detail.created,
}),
})
.then((response) => response.json())
.then((data) => {
console.log("Success:", data);
// update local array with new data
adventures = adventures.map((adventure) =>
adventure.id === event.detail.id ? event.detail : adventure,
);
editId = NaN;
editName = "";
editLocation = "";
editCreated = "";
showToast("edited");
})
.catch((error) => {
console.error("Error:", error);
});
} }
function editAdventure(event: { detail: number }) { function editAdventure(event: { detail: number }) {

View file

@ -31,28 +31,6 @@ export function getAdventures(): Adventure[] {
return adventures; return adventures;
} }
export function saveEdit(adventure: Adventure) {
let editId = adventure.id;
console.log("saving edit");
let editName = adventure.name;
let editLocation = adventure.location;
let editCreated = adventure.created;
let oldAdventure: Adventure | undefined = adventures.find(
(adventure) => adventure.id === editId
);
console.log("old" + oldAdventure);
if (oldAdventure) {
oldAdventure.name = editName;
oldAdventure.location = editLocation;
oldAdventure.created = editCreated;
}
editId = NaN;
console.log("done");
if (isBrowser) {
localStorage.setItem("adventures", JSON.stringify(adventures));
}
}
export function clearAdventures() { export function clearAdventures() {
adventures = []; adventures = [];
if (isBrowser) { if (isBrowser) {