Add migrations and code for sharedAdventures table
This commit is contained in:
11
src/routes/api/share/+server.ts
Normal file
11
src/routes/api/share/+server.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { db } from "$lib/db/db.server";
|
||||
import { sharedAdventures } from "$lib/db/schema";
|
||||
import type { Adventure } from "$lib/utils/types";
|
||||
|
||||
export async function POST({ request }: { request: Request }) {
|
||||
const { key , data } = await request.json();
|
||||
let adventure = data as Adventure;
|
||||
console.log(adventure);
|
||||
await db.insert(sharedAdventures).values({ id:key,data:adventure }).execute();
|
||||
return new Response(JSON.stringify({key:key}));
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
import SucessToast from "$lib/components/SucessToast.svelte";
|
||||
import mapDrawing from "$lib/assets/adventure_map.svg"
|
||||
import EditModal from "$lib/components/EditModal.svelte";
|
||||
import { generateRandomString } from "$lib";
|
||||
|
||||
let newName = '';
|
||||
let newLocation = '';
|
||||
@@ -79,6 +80,26 @@
|
||||
}
|
||||
}
|
||||
|
||||
function shareLink() {
|
||||
let key = generateRandomString()
|
||||
let data = JSON.stringify(adventures)
|
||||
fetch('/api/share', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ key, data }),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
console.log('Success:', data);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function handleClose() {
|
||||
editId = NaN;
|
||||
editName = '';
|
||||
@@ -144,5 +165,8 @@
|
||||
<button class="btn btn-neutral" on:click={deleteData}>
|
||||
<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
|
||||
</button>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
Reference in New Issue
Block a user