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",
},
}
);
}