|
| 1 | +import faunadb, { query as q } from 'faunadb'; |
| 2 | + |
| 3 | +import { AllContentRes, ContentMetaRes } from '@/types/fauna'; |
| 4 | + |
| 5 | +const faunaClient = new faunadb.Client({ |
| 6 | + secret: process.env.FAUNA_SECRET as string, |
| 7 | +}); |
| 8 | + |
| 9 | +/** |
| 10 | + * Get all content meta from the database |
| 11 | + */ |
| 12 | +export const getAllContent = async () => { |
| 13 | + const { data } = await faunaClient.query<AllContentRes>( |
| 14 | + q.Map( |
| 15 | + q.Paginate(q.Documents(q.Collection('contents'))), |
| 16 | + q.Lambda((x) => q.Get(x)) |
| 17 | + ) |
| 18 | + ); |
| 19 | + |
| 20 | + return data.map((x) => x.data); |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Get single content meta from the database by slug |
| 25 | + */ |
| 26 | +export const getContentMeta = async (slug: string) => { |
| 27 | + const data = await faunaClient.query<ContentMetaRes>( |
| 28 | + q.Let( |
| 29 | + { |
| 30 | + contentRef: q.Match(q.Index('get_contents_by_slug'), slug), |
| 31 | + contentExists: q.Exists(q.Var('contentRef')), |
| 32 | + }, |
| 33 | + q.If(q.Var('contentExists'), q.Get(q.Var('contentRef')), null) |
| 34 | + ) |
| 35 | + ); |
| 36 | + |
| 37 | + return data.data; |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * Create or add view count to the slug |
| 42 | + */ |
| 43 | +export const upsertContentMeta = async (slug: string) => { |
| 44 | + const data = await faunaClient.query<ContentMetaRes>( |
| 45 | + q.Let( |
| 46 | + { |
| 47 | + match: q.Match(q.Index('get_contents_by_slug'), slug), |
| 48 | + contentExists: q.Exists(q.Var('match')), |
| 49 | + }, |
| 50 | + q.If( |
| 51 | + q.Var('contentExists'), |
| 52 | + q.Update(q.Select('ref', q.Get(q.Var('match'))), { |
| 53 | + data: { |
| 54 | + views: q.Add(q.Select(['data', 'views'], q.Get(q.Var('match'))), 1), |
| 55 | + }, |
| 56 | + }), |
| 57 | + q.Create(q.Collection('contents'), { |
| 58 | + data: { slug: slug, views: 1, likes: 1, likesByUser: {} }, |
| 59 | + }) |
| 60 | + ) |
| 61 | + ) |
| 62 | + ); |
| 63 | + |
| 64 | + return data.data; |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * Create or add like count to the slug |
| 69 | + */ |
| 70 | +export const upsertLike = async (slug: string, sessionId: string) => { |
| 71 | + const data = await faunaClient.query<ContentMetaRes>( |
| 72 | + q.Let( |
| 73 | + { |
| 74 | + match: q.Match(q.Index('get_contents_by_slug'), slug), |
| 75 | + userLike: q.Select( |
| 76 | + ['data', 'likesByUser', sessionId], |
| 77 | + q.Get(q.Var('match')), |
| 78 | + false |
| 79 | + ), |
| 80 | + }, |
| 81 | + |
| 82 | + q.If( |
| 83 | + // If userLike is found |
| 84 | + q.IsInteger(q.Var('userLike')), |
| 85 | + q.If( |
| 86 | + // First check if we can add more |
| 87 | + q.GTE(q.Var('userLike'), 5), |
| 88 | + q.Abort('Max 5'), |
| 89 | + // Then Update the likes and update the likesByUser by sessionId |
| 90 | + q.Update(q.Select('ref', q.Get(q.Var('match'))), { |
| 91 | + data: { |
| 92 | + likes: q.Add( |
| 93 | + q.Select(['data', 'likes'], q.Get(q.Var('match'))), |
| 94 | + 1 |
| 95 | + ), |
| 96 | + likesByUser: { |
| 97 | + [`${sessionId}`]: q.Add(q.Var('userLike'), 1), |
| 98 | + }, |
| 99 | + }, |
| 100 | + }) |
| 101 | + ), |
| 102 | + // Else, Update the likes and create new key with sessionId |
| 103 | + q.Update(q.Select('ref', q.Get(q.Var('match'))), { |
| 104 | + data: { |
| 105 | + likes: q.Add(q.Select(['data', 'likes'], q.Get(q.Var('match'))), 1), |
| 106 | + likesByUser: { |
| 107 | + [`${sessionId}`]: 1, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }) |
| 111 | + ) |
| 112 | + ) |
| 113 | + ); |
| 114 | + |
| 115 | + return data.data; |
| 116 | +}; |
0 commit comments