1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-12-27 07:52:28 -05:00

Add cascading delete for splits/transactions

This commit is contained in:
Aaron Lindsay 2015-06-29 07:25:29 -04:00
parent 55a6e9a489
commit 12f50a6308

View File

@ -276,6 +276,37 @@ func UpdateTransaction(t *Transaction) error {
return nil return nil
} }
func DeleteTransaction(t *Transaction) error {
transaction, err := DB.Begin()
if err != nil {
return err
}
_, err = transaction.Exec("DELETE from splits where TransactionId=?", t.TransactionId)
if err != nil {
transaction.Rollback()
return err
}
count, err := transaction.Delete(t)
if err != nil {
transaction.Rollback()
return err
}
if count != 1 {
transaction.Rollback()
return errors.New("Deleted more than one transaction")
}
err = transaction.Commit()
if err != nil {
transaction.Rollback()
return err
}
return nil
}
func TransactionHandler(w http.ResponseWriter, r *http.Request) { func TransactionHandler(w http.ResponseWriter, r *http.Request) {
user, err := GetUserFromSession(r) user, err := GetUserFromSession(r)
if err != nil { if err != nil {
@ -413,8 +444,8 @@ func TransactionHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
count, err := DB.Delete(&transaction) err = DeleteTransaction(transaction)
if count != 1 || err != nil { if err != nil {
WriteError(w, 999 /*Internal Error*/) WriteError(w, 999 /*Internal Error*/)
log.Print(err) log.Print(err)
return return