diff --git a/src/components/LockedContent.tsx b/src/components/LockedContent.tsx index 60145a2..7f1a97a 100644 --- a/src/components/LockedContent.tsx +++ b/src/components/LockedContent.tsx @@ -6,8 +6,18 @@ import { LockClosedIcon, LockOpen1Icon } from "@radix-ui/react-icons" import { useEffect, useState } from "react" import "../styles/locked-content.css" +import type { Generated } from "kysely" -const LocationButton = ({ imageUrl = "#" }: { imageUrl?: string }) => { +const incrementCounter = async (id: string | Generated) => + await fetch(`${import.meta.env.PUBLIC_HOME_URL}/api/content/increment?id=${id}`) + +const LocationButton = ({ + contentId = "", + imageUrl = "#" +}: { + contentId?: string | Generated + imageUrl?: string +}) => { const [atTarget, setAtTarget] = useState(false) const [contentVisible, setContentVisible] = useState(false) const [hasPermission, setHasPermission] = useState(false) @@ -37,6 +47,11 @@ const LocationButton = ({ imageUrl = "#" }: { imageUrl?: string }) => { } } + const handleUnlock = async () => { + setContentVisible(true) + await incrementCounter(contentId) + } + useEffect(() => { navigator.permissions.query({ name: "geolocation" }).then(permissionStatus => { if (permissionStatus.state === "granted") { @@ -60,7 +75,7 @@ const LocationButton = ({ imageUrl = "#" }: { imageUrl?: string }) => {
- diff --git a/src/lib/db.ts b/src/lib/db.ts index cd99b5c..9583650 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -12,4 +12,5 @@ export interface ContentTable { author: string description: string created_at: Generated + unlocked_counter: Generated } diff --git a/src/pages/[id].astro b/src/pages/[id].astro index 185252a..b2310c9 100644 --- a/src/pages/[id].astro +++ b/src/pages/[id].astro @@ -19,11 +19,13 @@ import dayjs from 'dayjs'; import relativeTime from 'dayjs/plugin/relativeTime'; import localeTR from 'dayjs/locale/tr'; -type Content = Omit; +type Content = Omit; const { id } = Astro.params; -const res = await fetch(`${import.meta.env.HOME_URL}/api/content?id=${id}`); +const res = await fetch( + `${import.meta.env.PUBLIC_HOME_URL}/api/content?id=${id}` +); const data: Content | null = res.status === 200 ? await res.json() : null; @@ -65,7 +67,7 @@ const dateFromNow = dayjs(data?.created_at).fromNow(); - +
Paylaş

- Fotoğrafın kilidi şu ana dek 2 kez açıldı + Fotoğrafın kilidi şu ana dek {data?.unlocked_counter} kez açıldı

diff --git a/src/pages/api/content/increment.ts b/src/pages/api/content/increment.ts new file mode 100644 index 0000000..3b5b7f8 --- /dev/null +++ b/src/pages/api/content/increment.ts @@ -0,0 +1,39 @@ +import type { Database } from "@/lib/db" +import { createKysely } from "@vercel/postgres-kysely" +import type { APIRoute } from "astro" + +export const GET: APIRoute = async ({ request }) => { + const contentId = new URL(request.url).searchParams.get("id") + + if (!contentId) { + return new Response(null, { + status: 400, + statusText: "Content id is required" + }) + } + + const db = createKysely({ connectionString: import.meta.env.POSTGRES_URL }) + + try { + const result = await db + .updateTable("contents") + .set(eb => ({ unlocked_counter: eb("unlocked_counter", "+", 1) })) + .where("id", "=", contentId) + .executeTakeFirst() + + if (result) { + return new Response(JSON.stringify({ counter: Number(result) })) + } else { + return new Response(null, { + status: 404, + statusText: "Could not increment the counter" + }) + } + } catch (error) { + console.error("Error fetching content:", error) + return new Response(null, { + status: 500, + statusText: "Error while fetching content" + }) + } +} diff --git a/src/pages/api/content.ts b/src/pages/api/content/index.ts similarity index 96% rename from src/pages/api/content.ts rename to src/pages/api/content/index.ts index 93aaee7..ed8d709 100644 --- a/src/pages/api/content.ts +++ b/src/pages/api/content/index.ts @@ -3,7 +3,7 @@ import type { APIRoute } from "astro" import { createKysely } from "@vercel/postgres-kysely" import { customAlphabet } from "nanoid" -import type { Database } from "../../lib/db" +import type { Database } from "@/lib/db" export const POST: APIRoute = async ({ request }) => { const data = await request.formData() @@ -79,11 +79,13 @@ export const GET: APIRoute = async ({ request }) => { const content = await db .selectFrom("contents") .select(({ fn }) => [ + "id", "blob_url", fn("ST_AsGeoJSON", ["loc"]).as("loc"), "description", "author", - "created_at" + "created_at", + "unlocked_counter" ]) .where("url", "=", contentId) .executeTakeFirst()