Backend added table for shortener visitors

pull/3/head
TZGyn 2 years ago
parent 27851c74e0
commit 718c2fa6eb
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -35,20 +35,37 @@ app.post('/link', async ({ body }) => {
return { message: 'Success' } return { message: 'Success' }
}) })
app.get('/:shortenerCode', async ({ params: { shortenerCode }, set }) => { app.get(
const shortener = await db '/:shortenerCode',
.selectFrom('shortener') async ({ params: { shortenerCode }, set, request }) => {
.selectAll() const ip = request.headers.get('x-forwarded-for')
.where('code', '=', shortenerCode)
.execute()
if (!shortener.length) { const geolocation = await (
set.redirect = '/invalid' await fetch(`https://api.ipbase.com/v2/info?ip=${ip}`)
return ).json()
}
set.redirect = shortener[0].link const shortener = await db
}) .selectFrom('shortener')
.selectAll()
.where('code', '=', shortenerCode)
.orderBy('created_at', 'desc')
.execute()
const visitor_data = {
shortener_id: shortener[0].id,
country: geolocation.data.location.country.name as string,
}
await db.insertInto('visitor').values(visitor_data).execute()
if (!shortener.length) {
set.redirect = '/invalid'
return
}
set.redirect = shortener[0].link
}
)
app.listen(3000) app.listen(3000)

@ -1,15 +1,33 @@
import { Generated, Insertable, Selectable, Updateable } from 'kysely' import {
ColumnType,
Generated,
Insertable,
Selectable,
Updateable,
} from 'kysely'
export interface Database { export interface Database {
shortener: ShortenerTable shortener: ShortenerTable
visitor: VisitorTable
} }
export interface ShortenerTable { export interface ShortenerTable {
id: Generated<number> id: Generated<number>
link: string link: string
code: string code: string
created_at: ColumnType<Date, string | undefined, never>
} }
export type Shortener = Selectable<ShortenerTable> export type Shortener = Selectable<ShortenerTable>
export type NewShortener = Insertable<ShortenerTable> export type NewShortener = Insertable<ShortenerTable>
export type ShortenerUpdate = Updateable<ShortenerTable> export type ShortenerUpdate = Updateable<ShortenerTable>
export interface VisitorTable {
id: Generated<number>
shortener_id: number
country: string
created_at: ColumnType<Date, string | undefined, never>
}
export type Visitor = Selectable<VisitorTable>
export type NewVisitor = Insertable<VisitorTable>

Loading…
Cancel
Save