Ejemplo n.º 1
0
// Removes the expense by reversing all transactions associated with it,
// and deleting the expense from the db
func (e *Expense) Remove(db meddler.DB) error {
	transactions, err := e.Transactions(db)
	if err != nil {
		return err
	}
	for _, transaction := range transactions {
		err := transaction.remove(db)
		if err != nil {
			return err
		}
	}
	// remove the expense from the db
	_, err = db.Exec("DELETE FROM expenses WHERE id = ?", e.Id)
	if err != nil {
		return err
	}
	e = nil
	return nil
}
Ejemplo n.º 2
0
// Reverses a transaction, removes it from the DB, and frees the struct
func (t *Transaction) remove(db meddler.DB) error {
	lender, err := GetUserById(db, t.LenderId)
	if err != nil {
		return err
	}
	debtor, err := GetUserById(db, t.DebtorId)
	if err != nil {
		return err
	}

	// reverse the balance updates due to this transaction
	lender.UpdateBalance(db, -(t.Amount))
	debtor.UpdateBalance(db, t.Amount)

	// remove the transaction from the db
	_, err = db.Exec("DELETE FROM transactions WHERE id = ?", t.Id)
	if err != nil {
		return err
	}
	t = nil

	return nil
}