2023-04-11 21:46:47 +00:00
|
|
|
import {createServerSupabaseClient} from '@supabase/auth-helpers-nextjs'
|
2023-04-10 00:08:52 +00:00
|
|
|
import {NextApiRequest, NextApiResponse} from 'next'
|
2023-04-11 21:46:47 +00:00
|
|
|
import {Database} from '@/types/database.types'
|
2023-01-17 21:24:16 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2023-04-11 21:46:47 +00:00
|
|
|
const supabaseServerClient = createServerSupabaseClient<Database>({
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
})
|
|
|
|
|
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-11 21:46:47 +00:00
|
|
|
// @ts-ignore
|
|
|
|
await supabaseServerClient.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()
|
2023-04-11 21:46:47 +00:00
|
|
|
const response = await supabaseServerClient
|
2023-04-10 00:08:52 +00:00
|
|
|
.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
|
|
|
}
|