34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
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
|
||
|
|
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
|
||
|
|
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 };
|
||
|
|
});
|
||
|
|
}
|