aboutsummaryrefslogtreecommitdiffstats
path: root/routes/items.js
blob: 4d831592587afbd677c0b5ca6f7a2fd576f160b6 (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
45
46
47
import { g, s, db } from "$lib/db";
import { v4 } from "uuid";
import { bail, fail } from "$lib/utils";

export default {
  async list({ params: { id } }, res) {
    let items = await db.lRange(`${id}:items`, 0, -1);
    items = await Promise.all(items.map(async (id) => await g(`item:${id}`)));
    res.send(items);
  },

  async get({ params: { id } }, res) {
    try {
      let item = await g(`item:${id}`);
      console.log("ITEM", item);
      if (!item) fail("Item not found");
      res.send(item);
    } catch (e) {
      bail(res, e.message);
    }
  },

  async create({ body: item, user: { id } }, res) {
    if (!item.id) {
      item.id = v4();
      await db.lPush(`${id}:items`, item.id);
    }

      await s(`item:${item.id}`, item);

    res.send(item);
  },

  async del({ body: { item }, user: { id } }, res) {
    try {
      console.log(id);
      console.log(item.id);
      let n = await db.lRem(`${id}:items`, 0, item.id);
      if (n) db.del(`item:${item.id}`);
      else fail("item not found");

      res.send({});
    } catch (e) {
      bail(res, e.message);
    }
  },
};