backend/src/controller/router.ts
2023-12-19 09:12:42 +01:00

113 lines
4.1 KiB
TypeScript

// server.ts
import express, { Request, Response } from 'express';
import { User, Category, Transaction, Account } from './models'; // Adjust the path to your models
import pug from "pug"
import path from 'path';
import { TransactionRouter } from './router/transactions';
import bodyParser from 'body-parser';
import { CategoryRouter } from './router/categories';
export class Router {
app: any;
constructor(Path = "", PORT = 3000) {
let app = express();
// Set the view engine to Pug
app.set('view engine', 'pug');
app.set('views', './views'); // Set the views directory
console.log(Path)
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.use('/assets', express.static(Path));
app.get('/', async (req: Request, res: Response) => {
try {
const users = await User.findAll();
const categories = await Category.findAll();
const transactions = await Transaction.findAll();
res.render('index', {
title: 'Home',
userCount: users.length,
categoryCount: categories.length,
transactionCount: transactions.length,
});
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
});
let transactionR = TransactionRouter.routes();
app.use('/transactions', transactionR)
app.get('/users', async (req: Request, res: Response) => {
try {
const users = await User.findAll();
res.render('users', { title: 'Users', users });
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
});
let categoriesR = CategoryRouter.routes();
app.use('/categories', categoriesR)
/*
app.get('/transactions', async (req: Request, res: Response) => {
try {
const transactions = await Transaction.findAll({
include: Account
});
const categories = await Category.findAll({});
const accounts = await Account.findAll({});
console.log(transactions)
res.render('transactions/transactions', { title: 'Transactions', transactions, categories, accounts });
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
});
app.get('/transactions/acc/:acc', async (req: Request, res: Response) => {
try {
const transactions = await Transaction.findAll({
include: Account,
where: {
AccountID: req.params.acc
}
});
console.log(transactions)
res.render('transactions/transactions', { title: 'Transactions', transactions });
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
});
app.get('/transactions/cat/:cat', async (req: Request, res: Response) => {
try {
const transactions = await Transaction.findAll({
include: [Account, Category],
where: {
//@ts-ignore
CategoryID: req.params.cat
}
});
console.log(transactions)
res.render('transactions/transactions', { title: 'Transactions', transactions });
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
});
*/
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
this.app = app;
}
}