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

39 lines
1.2 KiB
TypeScript
Raw Normal View History

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