mirror of https://github.com/TZGyn/shortener
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
903 B
TypeScript
36 lines
903 B
TypeScript
import { db } from '$lib/db'
|
|
import { redirect } from '@sveltejs/kit'
|
|
import type { PageServerLoad } from './$types'
|
|
import { sql } from 'drizzle-orm'
|
|
import { visitor as visitorSchema } from '$lib/db/schema'
|
|
|
|
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,
|
|
},
|
|
})
|
|
|
|
const visitor = await db
|
|
.select({
|
|
count: sql<number>`cast(count(*) as int)`,
|
|
month: sql<number>`cast(to_char(${visitorSchema.createdAt}, 'MM') as int)`,
|
|
})
|
|
.from(visitorSchema)
|
|
.groupBy(sql`to_char(${visitorSchema.createdAt}, 'MM')`)
|
|
|
|
if (!shortener) {
|
|
throw redirect(303, '/')
|
|
}
|
|
|
|
return { shortener, visitor }
|
|
}) satisfies PageServerLoad
|