mirror of
https://github.com/aclindsa/moneygo.git
synced 2024-10-30 07:40:05 -04:00
Re-parent accounts and transactions on account deletion
This commit is contained in:
parent
12f50a6308
commit
524d82ecf7
45
accounts.go
45
accounts.go
@ -127,6 +127,45 @@ func UpdateAccount(a *Account) error {
|
|||||||
return insertUpdateAccount(a, false)
|
return insertUpdateAccount(a, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteAccount(a *Account) error {
|
||||||
|
transaction, err := DB.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Re-parent splits to this account's parent account
|
||||||
|
_, err = transaction.Exec("UPDATE splits SET AccountId=? WHERE AccountId=?", a.ParentAccountId, a.AccountId)
|
||||||
|
if err != nil {
|
||||||
|
transaction.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Re-parent child accounts to this account's parent account
|
||||||
|
_, err = transaction.Exec("UPDATE accounts SET ParentAccountId=? WHERE ParentAccountId=?", a.ParentAccountId, a.AccountId)
|
||||||
|
if err != nil {
|
||||||
|
transaction.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
count, err := transaction.Delete(a)
|
||||||
|
if err != nil {
|
||||||
|
transaction.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if count != 1 {
|
||||||
|
transaction.Rollback()
|
||||||
|
return errors.New("Was going to delete more than one account")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = transaction.Commit()
|
||||||
|
if err != nil {
|
||||||
|
transaction.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func AccountHandler(w http.ResponseWriter, r *http.Request) {
|
func AccountHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
user, err := GetUserFromSession(r)
|
user, err := GetUserFromSession(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -186,7 +225,7 @@ func AccountHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//Return Account with this Id
|
// Return Account with this Id
|
||||||
account, err := GetAccount(accountid, user.UserId)
|
account, err := GetAccount(accountid, user.UserId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
WriteError(w, 3 /*Invalid Request*/)
|
WriteError(w, 3 /*Invalid Request*/)
|
||||||
@ -246,8 +285,8 @@ func AccountHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
count, err := DB.Delete(&account)
|
err = DeleteAccount(account)
|
||||||
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
|
||||||
|
Loading…
Reference in New Issue
Block a user