1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-05 05:05:17 +02:00

Merge pull request #52 from seanmorley15/development

Development
This commit is contained in:
Sean Morley 2024-04-30 09:59:42 -04:00 committed by GitHub
commit 9d7a7ce35b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 24 deletions

View file

@ -85,7 +85,8 @@
class="input input-bordered w-full max-w-xs"
/>
</div>
<button class="btn btn-primary mr-4 mt-4" on:click={create}>Save</button
<button class="btn btn-primary mr-4 mt-4" on:click={create}
>Create</button
>
<!-- if there is a button in form, it will close the modal -->
<button class="btn mt-4" on:click={close}>Close</button>

View file

@ -1,5 +1,5 @@
import { lucia } from "$lib/server/auth";
import type { RequestEvent } from "@sveltejs/kit";
import { error, type RequestEvent } from "@sveltejs/kit";
import { adventureTable } from "$lib/db/schema";
import { db } from "$lib/db/db.server";
import { and, eq } from "drizzle-orm";
@ -88,38 +88,35 @@ export async function POST(event: RequestEvent): Promise<Response> {
console.log(newAdventure);
const { name, location, date, description } = newAdventure;
if (!name) {
return error(400, {
message: "Name field is required!",
});
}
// insert the adventure to the user's visited list
await db
let res = await db
.insert(adventureTable)
.values({
userId: event.locals.user.id,
type: "mylog",
name: name,
location: location,
date: date,
description: description,
location: location || null,
date: date || null,
description: description || null,
})
.returning({ insertedId: adventureTable.id })
.execute();
let res = await db
.select()
.from(adventureTable)
.where(
and(
eq(adventureTable.userId, event.locals.user.id),
eq(adventureTable.name, name),
eq(adventureTable.location, location),
eq(adventureTable.date, date),
eq(adventureTable.description, description)
)
)
.execute();
let insertedId = res[0].insertedId;
console.log(insertedId);
// return a response with the adventure object values
return new Response(
JSON.stringify({
adventure: { name, location, date },
message: { message: "Adventure added" },
id: res[0].id,
id: insertedId,
}),
{
status: 200,

View file

@ -13,7 +13,7 @@
import EditModal from "$lib/components/EditModal.svelte";
import { generateRandomString } from "$lib";
import { visitCount } from "$lib/utils/stores/visitCountStore";
import MoreFieldsInput from "$lib/components/MoreFieldsInput.svelte";
import MoreFieldsInput from "$lib/components/CreateNewAdventure.svelte";
let newName = "";
let newLocation = "";
@ -78,7 +78,16 @@
newAdventure,
}),
})
.then((response) => response.json())
.then((response) => {
if (!response.ok) {
return response.json().then((data) => {
throw new Error(
data.error || `Failed to add adventure - ${data?.message}`
);
});
}
return response.json();
})
.then((data) => {
let newId = data.id;
// add to local array for instant view update
@ -100,6 +109,7 @@
})
.catch((error) => {
console.error("Error:", error);
showToast(error.message);
});
};
@ -234,7 +244,7 @@
class="btn btn-secondary"
on:click={() => (isShowingMoreFields = !isShowingMoreFields)}
>
Show More Fields
<iconify-icon icon="mdi:plus" class="text-2xl"></iconify-icon>
</button>
</div>
{#if adventures.length != 0}
@ -307,7 +317,8 @@
<img src={deleteIcon} class="inline-block -mt-1" alt="Logo" /> Delete Data
</button>
<button class="btn btn-neutral" on:click={shareLink}>
<img src={deleteIcon} class="inline-block -mt-1" alt="Logo" /> Share as Link
<iconify-icon icon="mdi:share-variant" class="text-xl"></iconify-icon> Share
as Link
</button>
</div>
{/if}