1.0.0Updated 8 months ago
import Infinity from "@infinity-beyond/mod.ts";

const infinity = new Infinity({
  name: 'infinity-beyond-test'
});

import { Stickers } from "./classes/stickers.ts";
import { Plays } from "./classes/plays.ts";

infinity.get('/test', (_req, res) => {
  return res.json({ test: 123 });
})

infinity.get('/stickers', async (_req, res) => {
  console.log(await Stickers.UserCount());
  return res.json({ user_count: await Stickers.UserCount() });
})

infinity.get('/stickers/:key', async (req, res) => {
  const [ balance, history ] = await Promise.all([
    Stickers.Balance(req.params.key),
    Stickers.History(req.params.key)
  ]);

  return res.json({ balance, history });
})

infinity.post('/stickers/:key/add', async (req, res) => {
  const key = req.params.key;
  const { amount } = req.body;
  if(amount === undefined) return res.json({ message: `Invalid 'amount' provided` }, { status: 400 });

  return res.json(await Stickers.AddEntry({
    key,
    amount,
    reason: 'TEST',
    description: `Added from /stickers/${key}/add`,
    request_id: req.id
  }));
})

infinity.get('/plays', async (_req, res) => {
  return res.json({ user_count: await Plays.UserCount() });
})

infinity.get('/plays/:key', async (req, res) => {
  const count: number = Number(req.query.get('count'));
  const [ balance, history ] = await Promise.all([
    Plays.Balance(req.params.key),
    Plays.History(req.params.key, count)
  ]);

  return res.json({ balance, history });
})

infinity.post('/plays/:key/add', async (req, res) => {
  const key = req.params.key;
  const { amount } = req.body;
  if(amount === undefined) return res.json({ message: `Invalid 'amount' provided` }, { status: 400 });

  return res.json(await Plays.AddEntry({
    key,
    amount,
    reason: 'TEST',
    description: `Added from /plays/${key}/add`,
    request_id: req.id
  }));
})

infinity.listen();