portfolio/pages/api/views/[slug].ts

32 lines
989 B
TypeScript
Raw Normal View History

2023-04-10 00:08:52 +00:00
import {NextApiRequest, NextApiResponse} from 'next'
import {supabase} from '@/lib/supabase'
2023-01-17 21:24:16 +00:00
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
2023-04-10 00:08:52 +00:00
if (req.method === 'POST') {
2023-01-17 21:24:16 +00:00
if (req.query.slug !== undefined) {
const slug: string = req.query.slug.toString()
2023-04-10 00:08:52 +00:00
await supabase.rpc('increment_views', {page_slug: slug})
2023-01-17 21:24:16 +00:00
2023-04-10 00:08:52 +00:00
return res.status(200).json({})
}
2023-01-17 21:24:16 +00:00
2023-04-10 00:08:52 +00:00
return res.status(400).json({})
} else if (req.method === 'GET') {
if (req.query.slug !== undefined) {
const slug: string = req.query.slug.toString()
const response = await supabase
.from('analytics')
.select('views')
.eq('slug', slug)
.returns<any>()
2023-01-17 21:24:16 +00:00
2023-04-10 00:08:52 +00:00
const {views} = response.data[0]
2023-01-17 21:24:16 +00:00
2023-04-10 00:08:52 +00:00
return res.status(200).json({views})
2023-01-17 21:24:16 +00:00
}
2023-04-10 00:08:52 +00:00
return res.status(400).json({})
2023-01-17 21:24:16 +00:00
}
2023-04-10 00:08:52 +00:00
return res.status(405).json({})
2023-01-17 21:24:16 +00:00
}