mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-30 10:19:37 +02:00
Started Migration
This commit is contained in:
parent
e197de9d39
commit
37f050d254
4 changed files with 54 additions and 81 deletions
|
@ -53,7 +53,7 @@
|
|||
<p class="ml-.5">{location}</p>
|
||||
</div>
|
||||
{/if}
|
||||
{#if date !== ""}
|
||||
{#if date && date !== ""}
|
||||
<div class="inline-flex items-center">
|
||||
<iconify-icon icon="mdi:calendar" class="text-xl"></iconify-icon>
|
||||
<p class="ml-1">{date}</p>
|
||||
|
@ -73,7 +73,7 @@
|
|||
>
|
||||
<div class="card-body">
|
||||
<h2 class="card-title overflow-ellipsis">{name}</h2>
|
||||
{#if location != ""}
|
||||
{#if location && location != ""}
|
||||
<div class="inline-flex items-center">
|
||||
<iconify-icon icon="mdi:map-marker" class="text-xl"></iconify-icon>
|
||||
<p class="ml-.5">{location}</p>
|
||||
|
@ -92,7 +92,7 @@
|
|||
>
|
||||
<div class="card-body">
|
||||
<h2 class="card-title overflow-ellipsis">{name}</h2>
|
||||
{#if location !== ""}
|
||||
{#if location && location !== ""}
|
||||
<div class="inline-flex items-center">
|
||||
<iconify-icon icon="mdi:map-marker" class="text-xl"></iconify-icon>
|
||||
<p class="ml-.5">{location}</p>
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -1,40 +1 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue