Files
regress/server/src/routes/places.ts
T

34 lines
1.0 KiB
TypeScript
Raw Normal View History

import type { FastifyInstance } from 'fastify';
export default async function placesRoutes(app: FastifyInstance) {
const { db } = app.ctx;
const listPlaces = db.prepare(`
SELECT p.*, c.team AS claim_team, c.claimed_by_pubkey, c.claimed_at, c.claimed_at_block
FROM places p
LEFT JOIN claims c ON c.place_id = p.id
ORDER BY p.id
`);
const getPlace = db.prepare(`
SELECT p.*, c.team AS claim_team, c.claimed_by_pubkey, c.claimed_at, c.claimed_at_block
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 };
});
}