2026-08-05 13:14:03 +00:00
|
|
|
import type { FastifyInstance } from 'fastify';
|
|
|
|
|
|
|
|
|
|
export default async function placesRoutes(app: FastifyInstance) {
|
|
|
|
|
const { db } = app.ctx;
|
|
|
|
|
|
|
|
|
|
const listPlaces = db.prepare(`
|
2026-08-05 17:20:35 +00:00
|
|
|
SELECT p.*, c.team AS claim_team, c.claimed_by_pubkey, c.claimed_at, c.claimed_at_block
|
2026-08-05 13:14:03 +00:00
|
|
|
FROM places p
|
|
|
|
|
LEFT JOIN claims c ON c.place_id = p.id
|
|
|
|
|
ORDER BY p.id
|
|
|
|
|
`);
|
|
|
|
|
const getPlace = db.prepare(`
|
2026-08-05 17:20:35 +00:00
|
|
|
SELECT p.*, c.team AS claim_team, c.claimed_by_pubkey, c.claimed_at, c.claimed_at_block
|
2026-08-05 13:14:03 +00:00
|
|
|
FROM places p
|
|
|
|
|
LEFT JOIN claims c ON c.place_id = p.id
|
|
|
|
|
WHERE p.id = ?
|
|
|
|
|
`);
|
|
|
|
|
const getLinksForPlace = db.prepare(`
|
|
|
|
|
SELECT * FROM links WHERE from_place_id = ? OR to_place_id = ?
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
app.get('/api/places', async () => {
|
|
|
|
|
return listPlaces.all();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/api/places/:id', async (req, reply) => {
|
|
|
|
|
const id = Number((req.params as { id: string }).id);
|
|
|
|
|
const place = getPlace.get(id);
|
|
|
|
|
if (!place) return reply.code(404).send({ error: 'place not found' });
|
|
|
|
|
const links = getLinksForPlace.all(id, id);
|
|
|
|
|
return { ...place, links };
|
|
|
|
|
});
|
|
|
|
|
}
|