aboutsummaryrefslogtreecommitdiffstats
path: root/routes/locations.ts
blob: 6dd364ec0cb3a9d105277e21d861a99e1e6bf45e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { db, g } from "$lib/db";

export default {
  async list(c) {
    const locations = await g("locations");
    return c.json({ locations });
  },

  async nearby(c) {
    const lat = parseFloat(c.req.query("lat"));
    const lon = parseFloat(c.req.query("lon"));
    const radius = parseFloat(c.req.query("radius")) || 50;
    const count = parseInt(c.req.query("count")) || 50;

    if (isNaN(lat) || isNaN(lon)) {
      return c.json({ error: "lat and lon required" }, 400);
    }

    try {
      const ids = await db.geoSearch("locations:geo", {
        longitude: lon,
        latitude: lat,
      }, {
        radius,
        unit: "km",
      }, {
        COUNT: count,
        SORT: "ASC",
      });

      const locations = [];
      for (const id of ids) {
        const loc = await g(`location:${id}`);
        if (loc) locations.push(loc);
      }

      return c.json({ locations });
    } catch (e) {
      console.log("nearby search failed", e);
      const locations = await g("locations");
      return c.json({ locations });
    }
  },
};