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.
47 lines
903 B
TypeScript
47 lines
903 B
TypeScript
import express from 'express'
|
|
import { nanoid } from 'nanoid'
|
|
import { createLinkSchema } from './src/zodSchema'
|
|
import { db } from './src/database'
|
|
|
|
const app = express()
|
|
const port = 1234
|
|
|
|
app.use(express.json())
|
|
|
|
app.get('/', (req, res) => {
|
|
res.json({ message: 'Hello', data: 'World' })
|
|
})
|
|
|
|
app.get('/link', async (req, res) => {
|
|
const shorteners = await db.selectFrom('shortener').selectAll().execute()
|
|
|
|
res.json({ shorteners })
|
|
})
|
|
|
|
app.post('/link', async (req, res) => {
|
|
const body = req.body
|
|
|
|
const createLink = createLinkSchema.safeParse(body)
|
|
|
|
if (!createLink.success) {
|
|
res.json({ message: 'Invalid Link', body })
|
|
return
|
|
}
|
|
|
|
const uuid = nanoid(10)
|
|
|
|
await db
|
|
.insertInto('shortener')
|
|
.values({
|
|
link: createLink.data.link,
|
|
code: uuid,
|
|
})
|
|
.execute()
|
|
|
|
res.json({ ...createLink.data, uuid })
|
|
})
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Listening on port ${port}...`)
|
|
})
|