Skip to content

Commit

Permalink
refactor: separate routes in standalone file
Browse files Browse the repository at this point in the history
  • Loading branch information
acifani committed May 8, 2024
1 parent 305a452 commit 62bac26
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
23 changes: 2 additions & 21 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
import express from 'express';
import * as actual from './actual';
import { authMiddleware } from './auth';
import { router } from './routes';

const app = express();

app.use(authMiddleware);

app.get('/budget/:month', async (req, res) => {
const budget = await actual.getBudgetAtMonth(req.params.month);
res.status(200).json(budget);
});

app.get('/accounts', async (_, res) => {
const accounts = await actual.getAccounts();
res.status(200).json(accounts);
});

app.get('/accounts/:accountid/transactions', async (req, res) => {
const transactions = await actual.getTransactions(req.params.accountid);
res.status(200).json(transactions);
});

app.get('/transactions', async (req, res) => {
const offbudget = req.query['offbudget'] === 'true';
const transactions = await actual.getAllTransactions(offbudget);
res.status(200).json(transactions);
});
app.use('/', router);

const errorHandler: express.ErrorRequestHandler = (err, req, res, next) => {
console.error(err);
Expand Down
26 changes: 26 additions & 0 deletions routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as actual from './actual';
import express from 'express';
const router = express.Router();

router.get('/budget/:month', async (req, res) => {
const budget = await actual.getBudgetAtMonth(req.params.month);
res.status(200).json(budget);
});

router.get('/accounts', async (_, res) => {
const accounts = await actual.getAccounts();
res.status(200).json(accounts);
});

router.get('/accounts/:accountid/transactions', async (req, res) => {
const transactions = await actual.getTransactions(req.params.accountid);
res.status(200).json(transactions);
});

router.get('/transactions', async (req, res) => {
const offbudget = req.query['offbudget'] === 'true';
const transactions = await actual.getAllTransactions(offbudget);
res.status(200).json(transactions);
});

export { router };

0 comments on commit 62bac26

Please sign in to comment.