1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-30 10:19:37 +02:00

Started Migration

This commit is contained in:
Sean Morley 2024-04-26 01:14:13 +00:00
parent e197de9d39
commit 37f050d254
4 changed files with 54 additions and 81 deletions

View file

@ -53,7 +53,7 @@
<p class="ml-.5">{location}</p> <p class="ml-.5">{location}</p>
</div> </div>
{/if} {/if}
{#if date !== ""} {#if date && date !== ""}
<div class="inline-flex items-center"> <div class="inline-flex items-center">
<iconify-icon icon="mdi:calendar" class="text-xl"></iconify-icon> <iconify-icon icon="mdi:calendar" class="text-xl"></iconify-icon>
<p class="ml-1">{date}</p> <p class="ml-1">{date}</p>
@ -73,7 +73,7 @@
> >
<div class="card-body"> <div class="card-body">
<h2 class="card-title overflow-ellipsis">{name}</h2> <h2 class="card-title overflow-ellipsis">{name}</h2>
{#if location != ""} {#if location && location != ""}
<div class="inline-flex items-center"> <div class="inline-flex items-center">
<iconify-icon icon="mdi:map-marker" class="text-xl"></iconify-icon> <iconify-icon icon="mdi:map-marker" class="text-xl"></iconify-icon>
<p class="ml-.5">{location}</p> <p class="ml-.5">{location}</p>
@ -92,7 +92,7 @@
> >
<div class="card-body"> <div class="card-body">
<h2 class="card-title overflow-ellipsis">{name}</h2> <h2 class="card-title overflow-ellipsis">{name}</h2>
{#if location !== ""} {#if location && location !== ""}
<div class="inline-flex items-center"> <div class="inline-flex items-center">
<iconify-icon icon="mdi:map-marker" class="text-xl"></iconify-icon> <iconify-icon icon="mdi:map-marker" class="text-xl"></iconify-icon>
<p class="ml-.5">{location}</p> <p class="ml-.5">{location}</p>

View file

@ -1,6 +1,6 @@
import { lucia } from "$lib/server/auth"; import { lucia } from "$lib/server/auth";
import type { RequestEvent } from "@sveltejs/kit"; 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 { db } from "$lib/db/db.server";
import { and, eq } from "drizzle-orm"; import { and, eq } from "drizzle-orm";
import type { Adventure } from "$lib/utils/types"; import type { Adventure } from "$lib/utils/types";
@ -17,18 +17,12 @@ export async function GET(event: RequestEvent): Promise<Response> {
} }
let result = await db let result = await db
.select() .select()
.from(userVisitedAdventures) .from(adventureTable)
.where(eq(userVisitedAdventures.userId, event.locals.user.id)) .where(eq(adventureTable.userId, event.locals.user.id))
.execute(); .execute();
return new Response( return new Response(
JSON.stringify({ // turn the result into an Adventure object array
adventures: result.map((item) => ({ JSON.stringify(result.map((r) => r as Adventure)),
id: item.adventureID,
name: item.adventureName,
location: item.location,
date: item.visitedDate,
})),
}),
{ {
status: 200, status: 200,
headers: { headers: {
@ -62,11 +56,11 @@ export async function DELETE(event: RequestEvent): Promise<Response> {
} }
let res = await db let res = await db
.delete(userVisitedAdventures) .delete(adventureTable)
.where( .where(
and( and(
eq(userVisitedAdventures.userId, event.locals.user.id), eq(adventureTable.userId, event.locals.user.id),
eq(userVisitedAdventures.adventureID, Number(id)) eq(adventureTable.id, Number(id))
) )
) )
.execute(); .execute();
@ -90,28 +84,30 @@ export async function POST(event: RequestEvent): Promise<Response> {
}); });
} }
// get properties from the body const { newAdventure } = await event.request.json();
const { name, location, date } = await event.request.json(); console.log(newAdventure);
const { name, location, date } = newAdventure;
// insert the adventure to the user's visited list // insert the adventure to the user's visited list
await db await db
.insert(userVisitedAdventures) .insert(adventureTable)
.values({ .values({
userId: event.locals.user.id, userId: event.locals.user.id,
adventureName: name, type: "mylog",
name: name,
location: location, location: location,
visitedDate: date, date: date,
}) })
.execute(); .execute();
let res = await db let res = await db
.select() .select()
.from(userVisitedAdventures) .from(adventureTable)
.where( .where(
and( and(
eq(userVisitedAdventures.userId, event.locals.user.id), eq(adventureTable.userId, event.locals.user.id),
eq(userVisitedAdventures.adventureName, name), eq(adventureTable.name, name),
eq(userVisitedAdventures.location, location), eq(adventureTable.location, location),
eq(userVisitedAdventures.visitedDate, date) eq(adventureTable.date, date)
) )
) )
.execute(); .execute();
@ -121,7 +117,7 @@ export async function POST(event: RequestEvent): Promise<Response> {
JSON.stringify({ JSON.stringify({
adventure: { name, location, date }, adventure: { name, location, date },
message: { message: "Adventure added" }, message: { message: "Adventure added" },
id: res[0].adventureID, id: res[0].id,
}), }),
{ {
status: 200, status: 200,
@ -144,20 +140,22 @@ export async function PUT(event: RequestEvent): Promise<Response> {
} }
// get properties from the body // 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 // update the adventure in the user's visited list
await db await db
.update(userVisitedAdventures) .update(adventureTable)
.set({ .set({
adventureName: name, name: name,
location: location, location: location,
visitedDate: date, date: date,
}) })
.where( .where(
and( and(
eq(userVisitedAdventures.userId, event.locals.user.id), eq(adventureTable.userId, event.locals.user.id),
eq(userVisitedAdventures.adventureID, Number(id)) eq(adventureTable.id, Number(id))
) )
) )
.execute(); .execute();

View file

@ -28,7 +28,7 @@
// Sets the adventures array to the data from the server // Sets the adventures array to the data from the server
onMount(async () => { onMount(async () => {
console.log(data); console.log(data);
adventures = data.result.adventures; adventures = data.result;
isLoading = false; isLoading = false;
}); });
@ -63,15 +63,22 @@
let currentDate = new Date(); let currentDate = new Date();
let dateString = currentDate.toISOString().slice(0, 10); // Get date in "yyyy-mm-dd" format let dateString = currentDate.toISOString().slice(0, 10); // Get date in "yyyy-mm-dd" format
// post to /api/visits // post to /api/visits
let newAdventure: Adventure = {
type: "mylog",
name: newName,
location: newLocation,
date: dateString,
id: -1,
};
fetch("/api/visits", { fetch("/api/visits", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
name: newName, newAdventure,
location: newLocation,
date: dateString,
}), }),
}) })
.then((response) => response.json()) .then((response) => response.json())
@ -82,6 +89,7 @@
...adventures, ...adventures,
{ {
id: newId, id: newId,
type: "mylog",
name: newName, name: newName,
location: newLocation, location: newLocation,
date: dateString, date: dateString,
@ -99,6 +107,15 @@
function saveAdventure(event: { detail: Adventure }) { function saveAdventure(event: { detail: Adventure }) {
console.log("Event" + event.detail); 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 // put request to /api/visits with id and advneture data
fetch("/api/visits", { fetch("/api/visits", {
method: "PUT", method: "PUT",
@ -106,10 +123,7 @@
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
id: event.detail.id, newAdventure,
name: event.detail.name,
location: event.detail.location,
date: event.detail.date,
}), }),
}) })
.then((response) => response.json()) .then((response) => response.json())

View file

@ -1,40 +1 @@
import type { Adventure } from "$lib/utils/types"; import type { Adventure } from "$lib/utils/types";
let adventures: Adventure[] = [];
import { visitCount } from "$lib/utils/stores/visitCountStore";
// Check if localStorage is available (browser environment)
const isBrowser = typeof window !== "undefined";
//
export function getNextId() {
let nextId = Math.max(0, ...adventures.map((adventure) => adventure.id)) + 1;
return nextId;
}
export function setAdventures(importArray: Adventure[]) {
adventures = importArray;
}
export function addAdventure(adventure: Adventure) {
adventures = [...adventures, adventure];
if (isBrowser) {
localStorage.setItem("adventures", JSON.stringify(adventures));
visitCount.update((n) => n + 1);
}
console.log(adventures);
}
export function getAdventures(): Adventure[] {
return adventures;
}
export function clearAdventures() {
adventures = [];
if (isBrowser) {
localStorage.setItem("adventures", JSON.stringify(adventures));
visitCount.set(0);
}
}