|
| 1 | +import { fireStore, firebase } from "@/utils/services/firebase/config" |
1 | 2 | import { supabase } from "@/utils/services/supabase/config" |
| 3 | +import { |
| 4 | + doc, |
| 5 | + getDoc, |
| 6 | + getFirestore, |
| 7 | + increment, |
| 8 | + setDoc, |
| 9 | + updateDoc, |
| 10 | +} from "firebase/firestore" |
2 | 11 |
|
3 | 12 | import { IResetPasswordRequest } from "@/types/api/request/auth/reset-password" |
4 | 13 | import { ISignInEmailPasswordRequest } from "@/types/api/request/auth/sign-in-with-email-password" |
@@ -579,6 +588,46 @@ export const ListPostByUserUuid = async ( |
579 | 588 | } |
580 | 589 | } |
581 | 590 |
|
| 591 | +export const incrementPostViewCountByUuid = async (postUuid: string) => { |
| 592 | + const postRef = doc(fireStore, "postViewCount", postUuid) |
| 593 | + |
| 594 | + try { |
| 595 | + const docSnap = await getDoc(postRef) |
| 596 | + |
| 597 | + if (!docSnap.exists()) { |
| 598 | + // Document doesn't exist, create it with initial view count of 1 |
| 599 | + await setDoc(postRef, { viewCount: 1 }) |
| 600 | + return { viewCount: 1 } |
| 601 | + } else { |
| 602 | + // Document exists, increment the view count |
| 603 | + const updateResult = await updateDoc(postRef, { |
| 604 | + viewCount: increment(1), |
| 605 | + }) |
| 606 | + |
| 607 | + return updateResult |
| 608 | + } |
| 609 | + } catch (error) { |
| 610 | + throw error |
| 611 | + } |
| 612 | +} |
| 613 | + |
| 614 | +export const getPostViewCountByUuid = async ( |
| 615 | + postUuid: string |
| 616 | +): Promise<number | undefined> => { |
| 617 | + const postRef = doc(fireStore, "postViewCount", postUuid) |
| 618 | + |
| 619 | + try { |
| 620 | + const docSnap = await getDoc(postRef) |
| 621 | + if (docSnap.exists()) { |
| 622 | + return docSnap.data().viewCount || 0 |
| 623 | + } else { |
| 624 | + return 0 |
| 625 | + } |
| 626 | + } catch (error) { |
| 627 | + return undefined |
| 628 | + } |
| 629 | +} |
| 630 | + |
582 | 631 | // Industry |
583 | 632 | export const getIndustryList = async () => { |
584 | 633 | try { |
|
0 commit comments