Skip to content

Commit

Permalink
get more stuff fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
rugglcon committed Jul 15, 2019
1 parent 71e5989 commit cdb6e2f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/data/entities/budget.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, OneToMany, JoinColumn } from 'typeorm';
import { User } from './user';
import { Expense } from './expense';
import { decimal } from '../../util/decimal.transformer';

export interface NewBudget {
name: string;
Expand Down Expand Up @@ -66,7 +67,8 @@ export class Budget {
default: 0,
precision: 15,
type: 'decimal',
scale: 2
scale: 2,
transformer: decimal
})
total: number;
}
4 changes: 3 additions & 1 deletion src/data/entities/expense.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
import { Budget } from './budget';
import { decimal } from '../../util/decimal.transformer';

export interface NewExpense {
title: string;
Expand Down Expand Up @@ -41,7 +42,8 @@ export class Expense {
default: 0,
type: 'decimal',
precision: 15,
scale: 2
scale: 2,
transformer: decimal
})
cost: number;
}
2 changes: 1 addition & 1 deletion src/logic/budgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class BudgetLogic extends BaseLogic<Budget> {
const ret: SimpleBudget[] = [];
for (const b of budgets) {
ret.push({
id: b.id, total: Number(b.total), name: b.name, ownerId: b.ownerId
id: b.id, total: b.total, name: b.name, ownerId: b.ownerId
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/logic/expenses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class ExpenseLogic extends BaseLogic<Expense> {
for (const e of expenses) {
ret.push({
id: e.id,
cost: Number(e.cost),
cost: e.cost,
title: e.title,
budgetId: e.budgetId
});
Expand Down
15 changes: 12 additions & 3 deletions src/routes/budget.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ export const budgetRoutes = (cors: () => RequestHandler, budgetLogic: BudgetLogi
return;
}
res.status(200).send({
id: data.id, name: data.name, total: Number(data.total), ownerId: data.ownerId
id: data.id,
name: data.name,
total: data.total,
ownerId: data.ownerId
} as SimpleBudget);
}
} catch (err) {
Expand Down Expand Up @@ -117,7 +120,10 @@ export const budgetRoutes = (cors: () => RequestHandler, budgetLogic: BudgetLogi
console.log('updated budget', data);
logger.info(`user with id ${user.id} updated budget with id ${data.id}`);
res.status(200).send({
id: data.id, total: Number(data.total), name: data.name, ownerId: data.ownerId
id: data.id,
total: data.total,
name: data.name,
ownerId: data.ownerId
} as SimpleBudget);
} catch (err) {
res.status(500).send({message: 'Something went wrong.', err: err});
Expand Down Expand Up @@ -152,7 +158,10 @@ export const budgetRoutes = (cors: () => RequestHandler, budgetLogic: BudgetLogi
if (data) {
logger.info(`budget created with id: [${data.id}]`);
return res.status(200).send({
id: data.id, name: data.name, total: Number(data.total), ownerId: data.ownerId
id: data.id,
name: data.name,
total: data.total,
ownerId: data.ownerId
} as SimpleBudget);
} else {
logger.error('something went wrong trying to create the budget');
Expand Down
10 changes: 8 additions & 2 deletions src/routes/expense.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export const expenseRoutes = (cors: () => RequestHandler,
const reS = await expenseLogic.create(expense);
logger.info(`user with id [${user.id}] created expense [${reS.id}] under budget [${expense.budgetId}]`);
res.status(200).send({
id: reS.id, budgetId: reS.budgetId, cost: Number(reS.cost), title: reS.title
id: reS.id,
budgetId: reS.budgetId,
cost: reS.cost,
title: reS.title
} as SimpleExpense);
} catch (err) {
res.status(500).send({message: 'Something went wrong.', err: err});
Expand All @@ -66,7 +69,10 @@ export const expenseRoutes = (cors: () => RequestHandler,
const updated = await expenseLogic.update(expense);
logger.info(`user with id [${user.id}] updated expense with id [${expense.id}]`);
res.status(200).send({
cost: Number(updated.cost), title: updated.title, budgetId: updated.budgetId, id: updated.id
cost: updated.cost,
title: updated.title,
budgetId: updated.budgetId,
id: updated.id
} as SimpleExpense);
return;
} catch (err) {
Expand Down
6 changes: 6 additions & 0 deletions src/util/decimal.transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ValueTransformer } from 'typeorm';

export const decimal: ValueTransformer = {
to: (eValue: number) => eValue,
from: (dbValue: string) => Number(dbValue)
};

0 comments on commit cdb6e2f

Please sign in to comment.