import { blob } from "https://esm.town/v/std/blob";
let stories = [
{ id: 1, title: "Fake Story 1", upvotes: 10 },
{ id: 2, title: "Fake Story 2", upvotes: 20 },
];
export default async function main(req: Request): Promise<Response> {
if (req.method === "GET") {
return Response.json(stories);
} else if (req.method === "POST") {
const newStory = await req.json();
newStory.id = stories.length + 1;
newStory.upvotes = 0;
stories.push(newStory);
await blob.setJSON("hackernews_stories", stories);
return new Response("Story added successfully");
} else if (req.method === "PATCH") {
const { id, action } = await req.json();
const story = stories.find((s) => s.id === id);
if (action === "upvote" && story) {
story.upvotes++;
await blob.setJSON("hackernews_stories", stories);
return new Response("Story upvoted successfully");
} else {
return new Response("Story or action not found", { status: 404 });
}
}
return new Response("Method not allowed", { status: 405 });
}