added get shortener by id route

pull/3/head
TZGyn 2 years ago
parent 064dc8c157
commit 7de2344824
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -0,0 +1,20 @@
import { db } from '$lib/db'
import type { PageServerLoad } from './$types'
export const load = (async (event) => {
const user = event.locals.userObject
const shortenerId = event.params.id
const shortener = await db.query.shortener.findFirst({
where: (shortener, { eq, and }) =>
and(
eq(shortener.code, shortenerId),
eq(shortener.userId, user.id),
),
with: {
visitor: true,
},
})
return { shortener }
}) satisfies PageServerLoad

@ -0,0 +1,7 @@
<script lang="ts">
import type { PageData } from './$types'
export let data: PageData
</script>
{JSON.stringify(data.shortener)}

@ -4,8 +4,29 @@ import { and, eq } from 'drizzle-orm'
import type { RequestHandler } from './$types' import type { RequestHandler } from './$types'
import { z } from 'zod' import { z } from 'zod'
export const GET: RequestHandler = async () => { export const GET: RequestHandler = async (event) => {
return new Response() const user = event.locals.userObject
const shortenerId = event.params.id
const shortener = await db.query.shortener.findFirst({
where: (shortener, { eq, and }) =>
and(
eq(shortener.code, shortenerId),
eq(shortener.userId, user.id),
),
})
if (!shortener) {
return generateResponse({
success: false,
message: 'Invalid Shortener',
})
}
return generateResponse({
success: true,
data: shortener,
})
} }
const updateShortenerSchema = z.object({ const updateShortenerSchema = z.object({

Loading…
Cancel
Save