1- const { GraphQLClient } = require ( "graphql-request " )
1+ const { get , put } = require ( "./kvstoreclient " )
22
3- const hasuraEndpoint = "https://dshomoye.hasura.app/v1/graphql"
4- const hasuraAdminKey = process . env . HASURA_ADMIN_KEY
5-
6- const client = new GraphQLClient ( hasuraEndpoint , {
7- headers : {
8- "x-hasura-admin-secret" : hasuraAdminKey ,
9- } ,
10- } )
3+ const buildKey = ( pathname , hostname ) =>
4+ `dshomoye.dev:page_stats:likes:${ hostname } :${ pathname } `
115
126const getPageLikes = async ( pathname , hostname ) => {
13- const query = `query MyQuery {
14- page_stats(where: {pathname: {_eq: "${ pathname } "}, hostname: {_eq: "${ hostname } "}}) {
15- hostname
16- likes
17- pathname
18- }
19- }`
20- const data = await client . request ( query )
21- return data . page_stats
22- }
23-
24- const addPage = async ( pathname , hostname ) => {
25- const query = `mutation MyMutation {
26- insert_page_stats(objects: {hostname: "${ hostname } ", pathname: "${ pathname } "}) {
27- returning {
28- likes
29- }
30- }
31- }`
32- const res = await client . request ( query )
33- return res . insert_page_stats . returning
7+ const res = await get ( buildKey ( pathname , hostname ) )
8+ return res
349}
3510
3611const updatePageLike = async ( pathname , hostname , by = 1 ) => {
37- const query = `mutation MyMutation {
38- update_page_stats(where: {hostname: {_eq: "${ hostname } "}, pathname: {_eq: "${ pathname } "}}, _inc: {likes: ${ by } }) {
39- returning {
40- likes
41- }
42- }
43- }`
44- const res = await client . request ( query )
45- return res . update_page_stats . returning
12+ const current = await getPageLikes ( pathname , hostname )
13+ if ( ! current ) {
14+ const nextVal = by > 0 ? by : 0 ;
15+ console . log ( 'updating page, current is ' , nextVal )
16+ await put ( buildKey ( pathname , hostname ) , nextVal )
17+ return nextVal
18+ } else {
19+ let count = parseInt ( current )
20+ count += by
21+ await put ( buildKey ( pathname , hostname ) , count )
22+ return count
23+ }
4624}
4725
4826exports . handler = async ( event ) => {
27+ if ( event . httpMethod != "POST" ) {
28+ return {
29+ statusCode : 404 ,
30+ body : `{"error": "Not Found"}` ,
31+ }
32+ }
4933 const eventData = JSON . parse ( event . body )
5034 if ( eventData . action === "get_likes" ) {
5135 const { pathname, hostname } = eventData . payload
5236 let res = await getPageLikes ( pathname , hostname )
53- if ( res . length > 0 ) {
37+ if ( ! res ) {
5438 return {
5539 statusCode : 200 ,
56- body : JSON . stringify ( {
57- likes : res [ 0 ] . likes ,
58- } ) ,
40+ body : JSON . stringify ( { likes : 0 } ) ,
5941 }
60- }
61- res = await addPage ( pathname , hostname )
62- if ( res . length > 0 ) {
42+ } else {
6343 return {
6444 statusCode : 200 ,
65- body : JSON . stringify ( res [ 0 ] ) ,
45+ body : JSON . stringify ( { likes : res } ) ,
6646 }
6747 }
6848 } else if (
@@ -77,7 +57,7 @@ exports.handler = async (event) => {
7757 )
7858 return {
7959 statusCode : 200 ,
80- body : JSON . stringify ( res [ 0 ] ) ,
60+ body : JSON . stringify ( { likes : res } ) ,
8161 }
8262 }
8363
0 commit comments