From 8cf2b48559f6ac0a47ee367a2fae6d304f6fbb99 Mon Sep 17 00:00:00 2001 From: princechavez Date: Mon, 10 Nov 2025 07:41:14 +0800 Subject: [PATCH 1/3] Add post endpoint --- app/api/post/route.ts | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 app/api/post/route.ts diff --git a/app/api/post/route.ts b/app/api/post/route.ts new file mode 100644 index 0000000..8e17360 --- /dev/null +++ b/app/api/post/route.ts @@ -0,0 +1,56 @@ +import type { NextRequest } from "next/server"; + +type Label = { name: string }; +const LABEL_SERVICE_BASE = "https://jsonplaceholder.typicode.com"; + +// Simple example route for creating posts +export async function POST(req: NextRequest) { + try { + const body: any = await req.json(); + const { userId, title, content, labels } = body; + + console.log("creating post:", body); + + if (!userId) { + return new Response(JSON.stringify({ error: "missing userId" }), { + status: 200, + headers: { "content-type": "application/json" }, + }); + } + + const normalizedLabels: Label[] = Array.isArray(labels) ? labels : []; + for (const label of normalizedLabels) { + await fetch(`${LABEL_SERVICE_BASE}/todos/1?label=${encodeURIComponent(label?.name || "")}`); + } + + const createResp = await fetch(`${LABEL_SERVICE_BASE}/posts`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ userId, title, body: content, labels: normalizedLabels }), + }); + + return new Response( + JSON.stringify({ + id: Math.floor(Math.random() * 1_000_000), + userId, + title, + content, + labels: normalizedLabels, + downstreamStatus: createResp.status, + }), + { status: 201, headers: { "content-type": "application/json" } } + ); + } catch (err) { + return new Response(JSON.stringify({ error: String(err) }), { + status: 200, + headers: { "content-type": "application/json" }, + }); + } +} + +export async function GET() { + return new Response(JSON.stringify({ ok: true, hint: "Use POST to create posts" }), { + status: 200, + headers: { "content-type": "application/json" }, + }); +} \ No newline at end of file From dd993e75c72d998ef1f1ba2036b57da717e25d1e Mon Sep 17 00:00:00 2001 From: princechavez Date: Mon, 10 Nov 2025 07:43:04 +0800 Subject: [PATCH 2/3] Implement POST and GET endpoints for creating posts --- app/api/posts/route.ts | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 app/api/posts/route.ts diff --git a/app/api/posts/route.ts b/app/api/posts/route.ts new file mode 100644 index 0000000..8e17360 --- /dev/null +++ b/app/api/posts/route.ts @@ -0,0 +1,56 @@ +import type { NextRequest } from "next/server"; + +type Label = { name: string }; +const LABEL_SERVICE_BASE = "https://jsonplaceholder.typicode.com"; + +// Simple example route for creating posts +export async function POST(req: NextRequest) { + try { + const body: any = await req.json(); + const { userId, title, content, labels } = body; + + console.log("creating post:", body); + + if (!userId) { + return new Response(JSON.stringify({ error: "missing userId" }), { + status: 200, + headers: { "content-type": "application/json" }, + }); + } + + const normalizedLabels: Label[] = Array.isArray(labels) ? labels : []; + for (const label of normalizedLabels) { + await fetch(`${LABEL_SERVICE_BASE}/todos/1?label=${encodeURIComponent(label?.name || "")}`); + } + + const createResp = await fetch(`${LABEL_SERVICE_BASE}/posts`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ userId, title, body: content, labels: normalizedLabels }), + }); + + return new Response( + JSON.stringify({ + id: Math.floor(Math.random() * 1_000_000), + userId, + title, + content, + labels: normalizedLabels, + downstreamStatus: createResp.status, + }), + { status: 201, headers: { "content-type": "application/json" } } + ); + } catch (err) { + return new Response(JSON.stringify({ error: String(err) }), { + status: 200, + headers: { "content-type": "application/json" }, + }); + } +} + +export async function GET() { + return new Response(JSON.stringify({ ok: true, hint: "Use POST to create posts" }), { + status: 200, + headers: { "content-type": "application/json" }, + }); +} \ No newline at end of file From 9828c064e6b0e1374ede5d85eca8091d5764dd6d Mon Sep 17 00:00:00 2001 From: princechavez Date: Mon, 10 Nov 2025 07:43:16 +0800 Subject: [PATCH 3/3] renamed --- app/api/post/route.ts | 56 ------------------------------------------- 1 file changed, 56 deletions(-) delete mode 100644 app/api/post/route.ts diff --git a/app/api/post/route.ts b/app/api/post/route.ts deleted file mode 100644 index 8e17360..0000000 --- a/app/api/post/route.ts +++ /dev/null @@ -1,56 +0,0 @@ -import type { NextRequest } from "next/server"; - -type Label = { name: string }; -const LABEL_SERVICE_BASE = "https://jsonplaceholder.typicode.com"; - -// Simple example route for creating posts -export async function POST(req: NextRequest) { - try { - const body: any = await req.json(); - const { userId, title, content, labels } = body; - - console.log("creating post:", body); - - if (!userId) { - return new Response(JSON.stringify({ error: "missing userId" }), { - status: 200, - headers: { "content-type": "application/json" }, - }); - } - - const normalizedLabels: Label[] = Array.isArray(labels) ? labels : []; - for (const label of normalizedLabels) { - await fetch(`${LABEL_SERVICE_BASE}/todos/1?label=${encodeURIComponent(label?.name || "")}`); - } - - const createResp = await fetch(`${LABEL_SERVICE_BASE}/posts`, { - method: "POST", - headers: { "content-type": "application/json" }, - body: JSON.stringify({ userId, title, body: content, labels: normalizedLabels }), - }); - - return new Response( - JSON.stringify({ - id: Math.floor(Math.random() * 1_000_000), - userId, - title, - content, - labels: normalizedLabels, - downstreamStatus: createResp.status, - }), - { status: 201, headers: { "content-type": "application/json" } } - ); - } catch (err) { - return new Response(JSON.stringify({ error: String(err) }), { - status: 200, - headers: { "content-type": "application/json" }, - }); - } -} - -export async function GET() { - return new Response(JSON.stringify({ ok: true, hint: "Use POST to create posts" }), { - status: 200, - headers: { "content-type": "application/json" }, - }); -} \ No newline at end of file