import express from "express"; import { randomUUID } from "crypto"; import { fileURLToPath } from "url"; import { dirname, join } from "path"; import db from "./db.js"; const __dirname = dirname(fileURLToPath(import.meta.url)); const app = express(); app.use(express.json()); app.use(express.static(join(__dirname, "../dist"))); app.get("/api/businesses", (_req, res) => { res.json(db.prepare("SELECT * FROM businesses WHERE status = 'approved' ORDER BY createdAt DESC").all()); }); app.post("/api/businesses", (req, res) => { const { name, category, address, description, contact, lat, lng } = req.body; const business = { id: randomUUID(), name, category, address, description, contact: contact || "", lat, lng, status: "pending", createdAt: new Date().toISOString(), }; db.prepare(` INSERT INTO businesses (id, name, category, address, description, contact, lat, lng, status, createdAt) VALUES (@id, @name, @category, @address, @description, @contact, @lat, @lng, @status, @createdAt) `).run(business); res.status(201).json(business); }); app.get("/api/admin/pending", (_req, res) => { res.json(db.prepare("SELECT * FROM businesses WHERE status = 'pending' ORDER BY createdAt ASC").all()); }); app.patch("/api/admin/businesses/:id", (req, res) => { const { status } = req.body; if (!["approved", "rejected"].includes(status)) return res.status(400).json({ error: "status must be 'approved' or 'rejected'" }); const info = db.prepare("UPDATE businesses SET status = ? WHERE id = ?").run(status, req.params.id); if (info.changes === 0) return res.status(404).json({ error: "not found" }); res.json({ ok: true }); }); app.get(/^\/(?!api\/).*/, (_req, res) => { res.sendFile(join(__dirname, "../dist/index.html")); }); const PORT = process.env.PORT || 3000; app.listen(PORT, () => console.log(`API listening on http://localhost:${PORT}`));