Started Migration
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { lucia } from "$lib/server/auth";
|
||||
import type { RequestEvent } from "@sveltejs/kit";
|
||||
import { userVisitedAdventures } from "$lib/db/schema";
|
||||
import { adventureTable, userVisitedAdventures } from "$lib/db/schema";
|
||||
import { db } from "$lib/db/db.server";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import type { Adventure } from "$lib/utils/types";
|
||||
@@ -17,18 +17,12 @@ export async function GET(event: RequestEvent): Promise<Response> {
|
||||
}
|
||||
let result = await db
|
||||
.select()
|
||||
.from(userVisitedAdventures)
|
||||
.where(eq(userVisitedAdventures.userId, event.locals.user.id))
|
||||
.from(adventureTable)
|
||||
.where(eq(adventureTable.userId, event.locals.user.id))
|
||||
.execute();
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
adventures: result.map((item) => ({
|
||||
id: item.adventureID,
|
||||
name: item.adventureName,
|
||||
location: item.location,
|
||||
date: item.visitedDate,
|
||||
})),
|
||||
}),
|
||||
// turn the result into an Adventure object array
|
||||
JSON.stringify(result.map((r) => r as Adventure)),
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
@@ -62,11 +56,11 @@ export async function DELETE(event: RequestEvent): Promise<Response> {
|
||||
}
|
||||
|
||||
let res = await db
|
||||
.delete(userVisitedAdventures)
|
||||
.delete(adventureTable)
|
||||
.where(
|
||||
and(
|
||||
eq(userVisitedAdventures.userId, event.locals.user.id),
|
||||
eq(userVisitedAdventures.adventureID, Number(id))
|
||||
eq(adventureTable.userId, event.locals.user.id),
|
||||
eq(adventureTable.id, Number(id))
|
||||
)
|
||||
)
|
||||
.execute();
|
||||
@@ -90,28 +84,30 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
||||
});
|
||||
}
|
||||
|
||||
// get properties from the body
|
||||
const { name, location, date } = await event.request.json();
|
||||
const { newAdventure } = await event.request.json();
|
||||
console.log(newAdventure);
|
||||
const { name, location, date } = newAdventure;
|
||||
|
||||
// insert the adventure to the user's visited list
|
||||
await db
|
||||
.insert(userVisitedAdventures)
|
||||
.insert(adventureTable)
|
||||
.values({
|
||||
userId: event.locals.user.id,
|
||||
adventureName: name,
|
||||
type: "mylog",
|
||||
name: name,
|
||||
location: location,
|
||||
visitedDate: date,
|
||||
date: date,
|
||||
})
|
||||
.execute();
|
||||
let res = await db
|
||||
.select()
|
||||
.from(userVisitedAdventures)
|
||||
.from(adventureTable)
|
||||
.where(
|
||||
and(
|
||||
eq(userVisitedAdventures.userId, event.locals.user.id),
|
||||
eq(userVisitedAdventures.adventureName, name),
|
||||
eq(userVisitedAdventures.location, location),
|
||||
eq(userVisitedAdventures.visitedDate, date)
|
||||
eq(adventureTable.userId, event.locals.user.id),
|
||||
eq(adventureTable.name, name),
|
||||
eq(adventureTable.location, location),
|
||||
eq(adventureTable.date, date)
|
||||
)
|
||||
)
|
||||
.execute();
|
||||
@@ -121,7 +117,7 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
||||
JSON.stringify({
|
||||
adventure: { name, location, date },
|
||||
message: { message: "Adventure added" },
|
||||
id: res[0].adventureID,
|
||||
id: res[0].id,
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
@@ -144,20 +140,22 @@ export async function PUT(event: RequestEvent): Promise<Response> {
|
||||
}
|
||||
|
||||
// get properties from the body
|
||||
const { id, name, location, date } = await event.request.json();
|
||||
const { newAdventure } = await event.request.json();
|
||||
console.log(newAdventure);
|
||||
const { name, location, date, id } = newAdventure;
|
||||
|
||||
// update the adventure in the user's visited list
|
||||
await db
|
||||
.update(userVisitedAdventures)
|
||||
.update(adventureTable)
|
||||
.set({
|
||||
adventureName: name,
|
||||
name: name,
|
||||
location: location,
|
||||
visitedDate: date,
|
||||
date: date,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(userVisitedAdventures.userId, event.locals.user.id),
|
||||
eq(userVisitedAdventures.adventureID, Number(id))
|
||||
eq(adventureTable.userId, event.locals.user.id),
|
||||
eq(adventureTable.id, Number(id))
|
||||
)
|
||||
)
|
||||
.execute();
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
// Sets the adventures array to the data from the server
|
||||
onMount(async () => {
|
||||
console.log(data);
|
||||
adventures = data.result.adventures;
|
||||
adventures = data.result;
|
||||
isLoading = false;
|
||||
});
|
||||
|
||||
@@ -63,15 +63,22 @@
|
||||
let currentDate = new Date();
|
||||
let dateString = currentDate.toISOString().slice(0, 10); // Get date in "yyyy-mm-dd" format
|
||||
// post to /api/visits
|
||||
|
||||
let newAdventure: Adventure = {
|
||||
type: "mylog",
|
||||
name: newName,
|
||||
location: newLocation,
|
||||
date: dateString,
|
||||
id: -1,
|
||||
};
|
||||
|
||||
fetch("/api/visits", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: newName,
|
||||
location: newLocation,
|
||||
date: dateString,
|
||||
newAdventure,
|
||||
}),
|
||||
})
|
||||
.then((response) => response.json())
|
||||
@@ -82,6 +89,7 @@
|
||||
...adventures,
|
||||
{
|
||||
id: newId,
|
||||
type: "mylog",
|
||||
name: newName,
|
||||
location: newLocation,
|
||||
date: dateString,
|
||||
@@ -99,6 +107,15 @@
|
||||
|
||||
function saveAdventure(event: { detail: Adventure }) {
|
||||
console.log("Event" + event.detail);
|
||||
|
||||
let newAdventure: Adventure = {
|
||||
type: "mylog",
|
||||
name: event.detail.name,
|
||||
location: event.detail.location,
|
||||
date: event.detail.date,
|
||||
id: event.detail.id,
|
||||
};
|
||||
|
||||
// put request to /api/visits with id and advneture data
|
||||
fetch("/api/visits", {
|
||||
method: "PUT",
|
||||
@@ -106,10 +123,7 @@
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
id: event.detail.id,
|
||||
name: event.detail.name,
|
||||
location: event.detail.location,
|
||||
date: event.detail.date,
|
||||
newAdventure,
|
||||
}),
|
||||
})
|
||||
.then((response) => response.json())
|
||||
|
||||
Reference in New Issue
Block a user