Add visit count functionality to Navbar and Log page
This commit is contained in:
@@ -9,4 +9,4 @@ export const load: LayoutServerLoad = async (event) => {
|
||||
return {
|
||||
user: null,
|
||||
};
|
||||
};
|
||||
};
|
||||
36
src/routes/api/visitcount/+server.ts
Normal file
36
src/routes/api/visitcount/+server.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import type { RequestEvent } from "@sveltejs/kit";
|
||||
import { count } from 'drizzle-orm';
|
||||
import { eq } from "drizzle-orm";
|
||||
import { userVisitedAdventures } 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();
|
||||
|
||||
console.log(result[0].count);
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
visitCount: result[0].count,
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
import mapDrawing from "$lib/assets/adventure_map.svg";
|
||||
import EditModal from "$lib/components/EditModal.svelte";
|
||||
import { generateRandomString } from "$lib";
|
||||
import { visitCount } from "$lib/utils/stores/visitCountStore";
|
||||
|
||||
let newName = "";
|
||||
let newLocation = "";
|
||||
@@ -37,6 +38,11 @@
|
||||
adventures = data.result.adventures;
|
||||
});
|
||||
|
||||
let count = 0;
|
||||
visitCount.subscribe((value) => {
|
||||
count = value;
|
||||
});
|
||||
|
||||
function showToast(action: string) {
|
||||
toastAction = action;
|
||||
isShowingToast = true;
|
||||
@@ -80,6 +86,7 @@
|
||||
newName = ""; // Reset newName and newLocation after adding adventure
|
||||
newLocation = "";
|
||||
showToast("added");
|
||||
visitCount.update((n) => n + 1);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error:", error);
|
||||
@@ -183,6 +190,7 @@
|
||||
(adventure) => adventure.id !== event.detail,
|
||||
);
|
||||
showToast("removed");
|
||||
visitCount.update((n) => n - 1);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error:", error);
|
||||
|
||||
Reference in New Issue
Block a user