Add support for sqlite
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import Database from "better-sqlite3";
|
||||
import { fileURLToPath } from "url";
|
||||
import { dirname, join } from "path";
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const dbPath = join(__dirname, "../localesp.db");
|
||||
const db = new Database(dbPath);
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS locales (
|
||||
id TEXT PRIMARY KEY,
|
||||
nombre TEXT NOT NULL,
|
||||
descripcion TEXT,
|
||||
ubicacion TEXT,
|
||||
telefono TEXT,
|
||||
email TEXT,
|
||||
web TEXT,
|
||||
categoria TEXT,
|
||||
fecha TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||
lat REAL,
|
||||
lng REAL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS pendientes (
|
||||
id TEXT PRIMARY KEY,
|
||||
nombre TEXT NOT NULL,
|
||||
descripcion TEXT,
|
||||
ubicacion TEXT,
|
||||
telefono TEXT,
|
||||
email TEXT,
|
||||
web TEXT,
|
||||
categoria TEXT,
|
||||
estado TEXT DEFAULT 'pendiente',
|
||||
fechaPropuesta TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||
lat REAL,
|
||||
lng REAL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS config (
|
||||
id TEXT PRIMARY KEY,
|
||||
datos TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id TEXT PRIMARY KEY,
|
||||
email TEXT UNIQUE NOT NULL,
|
||||
password TEXT NOT NULL,
|
||||
created TEXT DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
`);
|
||||
|
||||
export default db;
|
||||
@@ -0,0 +1,95 @@
|
||||
import express from "express";
|
||||
import { fileURLToPath } from "url";
|
||||
import { dirname, join } from "path";
|
||||
import db from "./db.js";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const app = express();
|
||||
|
||||
app.use(express.json());
|
||||
app.use(express.static(join(__dirname, "../dist")));
|
||||
|
||||
// Locales (público, solo lectura)
|
||||
app.get("/api/locales", (_req, res) => {
|
||||
const locales = db.prepare("SELECT * FROM locales ORDER BY fecha DESC").all();
|
||||
res.json(locales);
|
||||
});
|
||||
|
||||
// Pendientes (admin)
|
||||
app.get("/api/pendientes", (_req, res) => {
|
||||
const pendientes = db.prepare("SELECT * FROM pendientes ORDER BY fechaPropuesta DESC").all();
|
||||
res.json(pendientes);
|
||||
});
|
||||
|
||||
// Enviar propuesta
|
||||
app.post("/api/propuestas", (req, res) => {
|
||||
const { nombre, descripcion, ubicacion, telefono, email, web, categoria, lat, lng } = req.body;
|
||||
const id = randomUUID();
|
||||
const fechaPropuesta = new Date().toISOString();
|
||||
|
||||
db.prepare(`
|
||||
INSERT INTO pendientes (id, nombre, descripcion, ubicacion, telefono, email, web, categoria, estado, fechaPropuesta, lat, lng)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'pendiente', ?, ?, ?)
|
||||
`).run(id, nombre, descripcion, ubicacion, telefono, email, web, categoria, fechaPropuesta, lat, lng);
|
||||
|
||||
res.status(201).json({ id, nombre, descripcion, ubicacion, telefono, email, web, categoria, estado: "pendiente", fechaPropuesta, lat, lng });
|
||||
});
|
||||
|
||||
// Aprobar propuesta
|
||||
app.post("/api/aprobar/:id", (req, res) => {
|
||||
const { id } = req.params;
|
||||
const propuesta = db.prepare("SELECT * FROM pendientes WHERE id = ?").get(id);
|
||||
|
||||
if (!propuesta) return res.status(404).json({ error: "Propuesta no encontrada" });
|
||||
|
||||
const localeId = randomUUID();
|
||||
const fecha = new Date().toISOString();
|
||||
|
||||
db.prepare(`
|
||||
INSERT INTO locales (id, nombre, descripcion, ubicacion, telefono, email, web, categoria, fecha, lat, lng)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`).run(localeId, propuesta.nombre, propuesta.descripcion, propuesta.ubicacion, propuesta.telefono, propuesta.email, propuesta.web, propuesta.categoria, fecha, propuesta.lat, propuesta.lng);
|
||||
|
||||
db.prepare("DELETE FROM pendientes WHERE id = ?").run(id);
|
||||
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
// Rechazar propuesta
|
||||
app.post("/api/rechazar/:id", (req, res) => {
|
||||
const { id } = req.params;
|
||||
db.prepare("DELETE FROM pendientes WHERE id = ?").run(id);
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
// Eliminar local
|
||||
app.delete("/api/locales/:id", (req, res) => {
|
||||
const { id } = req.params;
|
||||
db.prepare("DELETE FROM locales WHERE id = ?").run(id);
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
// Config (palabras filtradas)
|
||||
app.get("/api/config/filtro_palabras", (_req, res) => {
|
||||
const config = db.prepare("SELECT datos FROM config WHERE id = ?").get("filtro_palabras");
|
||||
if (config) {
|
||||
res.json({ palabras: JSON.parse(config.datos) });
|
||||
} else {
|
||||
res.json({ palabras: [] });
|
||||
}
|
||||
});
|
||||
|
||||
app.post("/api/config/filtro_palabras", (req, res) => {
|
||||
const { palabras } = req.body;
|
||||
db.prepare("INSERT OR REPLACE INTO config (id, datos) VALUES (?, ?)").run("filtro_palabras", JSON.stringify(palabras));
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
// SPA fallback
|
||||
app.get(/^\/(?!api\/).*/, (_req, res) => {
|
||||
res.sendFile(join(__dirname, "../dist/index.html"));
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
app.listen(PORT, () => console.log(`Server listening on http://localhost:${PORT}`));
|
||||
Reference in New Issue
Block a user