Remove userVisitedAdventuresTable

This commit is contained in:
Sean Morley
2024-04-26 23:04:41 +00:00
parent db8fc061f2
commit 08579289a6
36 changed files with 132 additions and 6593 deletions

View File

@@ -1,6 +1,6 @@
import { lucia } from "$lib/server/auth";
import { redirect, type Handle } from "@sveltejs/kit";
import { sequence } from '@sveltejs/kit/hooks';
import { sequence } from "@sveltejs/kit/hooks";
import { db } from "$lib/db/db.server";
import { userTable } from "$lib/db/schema";
import { eq } from "drizzle-orm";
@@ -24,7 +24,7 @@ export const authHook: Handle = async ({ event, resolve }) => {
});
}
if (!session) {
const sessionCookie:any = lucia.createBlankSessionCookie();
const sessionCookie: any = lucia.createBlankSessionCookie();
event.cookies.set(sessionCookie.name, sessionCookie.value, {
path: ".",
...sessionCookie.attributes,
@@ -36,12 +36,12 @@ export const authHook: Handle = async ({ event, resolve }) => {
};
export const themeHook: Handle = async ({ event, resolve }) => {
let theme:String | null = null;
let theme: String | null = null;
const newTheme = event.url.searchParams.get("theme");
const cookieTheme = event.cookies.get("colortheme");
if(newTheme) {
if (newTheme) {
theme = newTheme;
} else if (cookieTheme) {
theme = cookieTheme;
@@ -49,25 +49,20 @@ export const themeHook: Handle = async ({ event, resolve }) => {
if (theme) {
return await resolve(event, {
transformPageChunk: ({ html }) =>
html.replace('data-theme=""', `data-theme="${theme}"`)
})
html.replace('data-theme=""', `data-theme="${theme}"`),
});
}
return await resolve(event);
}
};
export const setupAdminUser: Handle = async ({ event, resolve }) => {
// Check if an admin user exists
/* let result = await db
.select()
.from(userVisitedAdventures)
.where(eq(userVisitedAdventures.userId, event.locals.user.id))
.execute();*/
let adminUser = await db
.select()
.from(userTable)
.where(eq(userTable.role, 'admin'))
.execute();
.select()
.from(userTable)
.where(eq(userTable.role, "admin"))
.execute();
// If an admin user exists, return the resolved event
if (adminUser != null && adminUser.length > 0) {
event.locals.isServerSetup = true;

View File

@@ -53,16 +53,6 @@ export const sessionTable = pgTable("session", {
}).notNull(),
});
export const userVisitedAdventures = pgTable("userVisitedAdventures", {
adventureID: serial("adventure_id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => userTable.id),
adventureName: text("adventure_name").notNull(),
location: text("location"),
visitedDate: text("visited_date"),
});
export const worldTravelCountries = pgTable("worldTravelCountries", {
id: serial("id").primaryKey(),
name: text("name").notNull(),

View File

@@ -32,3 +32,11 @@
</div>
</div>
</div>
<svelte:head>
<title>Home | AdventureLog</title>
<meta
name="description"
content="AdventureLog is a platform to log your adventures."
/>
</svelte:head>

View File

@@ -1,27 +1,25 @@
import type { RequestEvent } from "@sveltejs/kit";
import { db } from "$lib/db/db.server";
import { eq } from "drizzle-orm";
import { userVisitedAdventures } from "$lib/db/schema";
import { adventureTable } from "$lib/db/schema";
export async function DELETE(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "No user found" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
let res = await db
.delete(userVisitedAdventures)
.where(
eq(userVisitedAdventures.userId, event.locals.user.id),
)
.execute();
return new Response(JSON.stringify({ res: res }), {
status: 200,
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "No user found" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
}
let res = await db
.delete(adventureTable)
.where(eq(adventureTable.userId, event.locals.user.id))
.execute();
return new Response(JSON.stringify({ res: res }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}

View File

@@ -1,78 +1,78 @@
import type { RequestEvent } from "@sveltejs/kit";
import { db } from "$lib/db/db.server";
import { userVisitedAdventures, userVisitedWorldTravel } from "$lib/db/schema";
import { userVisitedWorldTravel } from "$lib/db/schema";
import { and, eq } from "drizzle-orm";
export async function POST(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
let body = await event.request.json();
let res = await db
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
let body = await event.request.json();
let res = await db
.insert(userVisitedWorldTravel)
.values({
userId: event.locals.user.id,
region_id: body.region_id,
country_code: body.country_code,
userId: event.locals.user.id,
region_id: body.region_id,
country_code: body.country_code,
})
.execute();
return new Response(JSON.stringify({ res: res }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
return new Response(JSON.stringify({ res: res }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}
export async function GET(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
let res = await db
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
let res = await db
.select()
.from(userVisitedWorldTravel)
.where(eq(userVisitedWorldTravel.userId,event.locals.user.id));
return new Response(JSON.stringify({ res: res }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
.where(eq(userVisitedWorldTravel.userId, event.locals.user.id));
return new Response(JSON.stringify({ res: res }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}
export async function DELETE(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
let body = await event.request.json();
let res = await db
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
let body = await event.request.json();
let res = await db
.delete(userVisitedWorldTravel)
.where(
and(
eq(userVisitedWorldTravel.userId, event.locals.user.id),
eq(userVisitedWorldTravel.region_id, body.region_id),
)
and(
eq(userVisitedWorldTravel.userId, event.locals.user.id),
eq(userVisitedWorldTravel.region_id, body.region_id)
)
)
.execute();
return new Response(JSON.stringify({ res: res }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}
return new Response(JSON.stringify({ res: res }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}

View File

@@ -1,34 +1,34 @@
import type { RequestEvent } from "@sveltejs/kit";
import { count } from 'drizzle-orm';
import { count } from "drizzle-orm";
import { eq } from "drizzle-orm";
import { userVisitedAdventures } from "$lib/db/schema";
import { adventureTable } from "$lib/db/schema";
import { db } from "$lib/db/db.server";
export async function GET(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 the count of the number of adventures the user has visited
let result = await db
.select({ count: count() })
.from(userVisitedAdventures)
.where(eq(userVisitedAdventures.userId,event.locals.user.id))
.execute();
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "No user found" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
// get the count of the number of adventures the user has visited
let result = await db
.select({ count: count() })
.from(adventureTable)
.where(eq(adventureTable.userId, event.locals.user.id))
.execute();
return new Response(
JSON.stringify({
visitCount: result[0].count,
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
}
return new Response(
JSON.stringify({
visitCount: result[0].count,
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
}

View File

@@ -1,6 +1,6 @@
import { lucia } from "$lib/server/auth";
import type { RequestEvent } from "@sveltejs/kit";
import { adventureTable, userVisitedAdventures } from "$lib/db/schema";
import { adventureTable } from "$lib/db/schema";
import { db } from "$lib/db/db.server";
import { and, eq } from "drizzle-orm";
import type { Adventure } from "$lib/utils/types";

View File

@@ -11,15 +11,21 @@
});
async function add(event: CustomEvent<{ name: string; location: string }>) {
let newAdventure: Adventure = {
name: event.detail.name,
location: event.detail.location,
date: "",
type: "mylog",
id: -1,
};
const response = await fetch("/api/visits", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: event.detail.name,
location: event.detail.location,
date: "",
newAdventure,
}),
});

View File

@@ -2,9 +2,9 @@ import { error, redirect, type Actions, type Handle } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
import { db } from "$lib/db/db.server";
import {
adventureTable,
sessionTable,
userTable,
userVisitedAdventures,
userVisitedWorldTravel,
} from "$lib/db/schema";
import type { DatabaseUser } from "$lib/server/auth";
@@ -25,7 +25,7 @@ export const load: PageServerLoad = async (event) => {
users = (await db.select().from(userTable).execute()) as DatabaseUser[];
visitCount = (await db
.select({ count: count() })
.from(userVisitedAdventures)
.from(adventureTable)
.execute()) as unknown as number;
userCount = (await db
.select({ count: count() })