feat: creates backend for the project
This commit is contained in:
54
backend/src/routes/transactions.ts
Normal file
54
backend/src/routes/transactions.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { Router } from 'express';
|
||||
import { asyncHandler } from '../utils';
|
||||
import * as transactionService from '../services/transactions';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get(
|
||||
'/',
|
||||
asyncHandler(async (req, res) => {
|
||||
const q = req.query;
|
||||
const result = await transactionService.getTransactions({
|
||||
accountId: q.accountId ? Number(q.accountId) : undefined,
|
||||
from: q.from as string | undefined,
|
||||
to: q.to as string | undefined,
|
||||
direction: q.direction as string | undefined,
|
||||
categoryId: q.categoryId ? Number(q.categoryId) : undefined,
|
||||
search: q.search as string | undefined,
|
||||
amountMin: q.amountMin ? Number(q.amountMin) : undefined,
|
||||
amountMax: q.amountMax ? Number(q.amountMax) : undefined,
|
||||
onlyUnconfirmed: q.onlyUnconfirmed === 'true',
|
||||
sortBy: q.sortBy as 'date' | 'amount' | undefined,
|
||||
sortOrder: q.sortOrder as 'asc' | 'desc' | undefined,
|
||||
page: q.page ? Number(q.page) : undefined,
|
||||
pageSize: q.pageSize ? Number(q.pageSize) : undefined,
|
||||
});
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
router.put(
|
||||
'/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
const id = Number(req.params.id);
|
||||
if (isNaN(id)) {
|
||||
res.status(400).json({ error: 'BAD_REQUEST', message: 'Invalid transaction id' });
|
||||
return;
|
||||
}
|
||||
|
||||
const { categoryId, comment } = req.body;
|
||||
if (categoryId === undefined && comment === undefined) {
|
||||
res.status(400).json({ error: 'BAD_REQUEST', message: 'No fields to update' });
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await transactionService.updateTransaction(id, { categoryId, comment });
|
||||
if (!result) {
|
||||
res.status(404).json({ error: 'NOT_FOUND', message: 'Transaction not found' });
|
||||
return;
|
||||
}
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user