mirror of
https://github.com/aclindsa/moneygo.git
synced 2024-12-25 23:23:21 -05:00
Add cascading delete for splits/transactions
This commit is contained in:
parent
55a6e9a489
commit
12f50a6308
@ -276,6 +276,37 @@ func UpdateTransaction(t *Transaction) error {
|
||||
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) {
|
||||
user, err := GetUserFromSession(r)
|
||||
if err != nil {
|
||||
@ -413,8 +444,8 @@ func TransactionHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
count, err := DB.Delete(&transaction)
|
||||
if count != 1 || err != nil {
|
||||
err = DeleteTransaction(transaction)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user