support delete account

This commit is contained in:
MaysWind
2020-11-11 23:50:02 +08:00
parent b5a5032bd2
commit e4407c8137
8 changed files with 104 additions and 2 deletions
+1
View File
@@ -161,6 +161,7 @@ func startWebServer(c *cli.Context) error {
// Accounts
apiV1Route.GET("/accounts/list.json", bindApi(api.Accounts.AccountListHandler))
apiV1Route.POST("/accounts/add.json", bindApi(api.Accounts.AccountCreateHandler))
apiV1Route.POST("/accounts/delete.json", bindApi(api.Accounts.AccountDeleteHandler))
}
}
+21
View File
@@ -124,6 +124,27 @@ func (a *AccountsApi) AccountCreateHandler(c *core.Context) (interface{}, *errs.
return accountInfoResp, nil
}
func (a *AccountsApi) AccountDeleteHandler(c *core.Context) (interface{}, *errs.Error) {
var accountDeleteReq models.AccountDeleteRequest
err := c.ShouldBindJSON(&accountDeleteReq)
if err != nil {
log.WarnfWithRequestId(c, "[accounts.AccountDeleteHandler] parse request failed, because %s", err.Error())
return nil, errs.NewIncompleteOrIncorrectSubmissionError(err)
}
uid := c.GetCurrentUid()
err = a.accounts.DeleteAccounts(uid, []int64{accountDeleteReq.Id})
if err != nil {
log.ErrorfWithRequestId(c, "[accounts.AccountDeleteHandler] failed to delete account \"id:%s\" for user \"uid:%d\", because %s", accountDeleteReq.Id, uid, err.Error())
return nil, errs.Or(err, errs.ErrOperationFailed)
}
log.InfofWithRequestId(c, "[accounts.AccountDeleteHandler] user \"uid:%d\" has deleted account \"id:%s\"", uid, accountDeleteReq.Id)
return true, nil
}
func (a *AccountsApi) createNewAccount(uid int64, accountCreateReq *models.AccountCreateRequest, order int) *models.Account {
return &models.Account{
Uid: uid,
+4
View File
@@ -51,6 +51,10 @@ type AccountCreateRequest struct {
SubAccounts []*AccountCreateRequest `json:"subAccounts" binding:"omitempty"`
}
type AccountDeleteRequest struct {
Id int64 `json:"id,string" binding:"required,min=1"`
}
type AccountInfoResponse struct {
Id int64 `json:"id,string"`
Name string `json:"name"`
+24
View File
@@ -121,3 +121,27 @@ func (s *AccountService) CreateAccounts(mainAccount *models.Account, childrenAcc
return nil
})
}
func (s *AccountService) DeleteAccounts(uid int64, ids []int64) error {
if uid <= 0 {
return errs.ErrUserIdInvalid
}
now := time.Now().Unix()
updateModel := &models.Account{
Deleted: true,
UpdatedUnixTime: now,
DeletedUnixTime: now,
}
return s.UserDataDB(uid).DoTransaction(func(sess *xorm.Session) error {
deletedRows, err := sess.Cols("deleted", "deleted_unix_time").In("account_id", ids).Where("uid=? AND deleted=?", uid, false).Update(updateModel)
if deletedRows < 1 {
return errs.ErrAccountNotFound
}
return err
})
}
+5
View File
@@ -174,4 +174,9 @@ export default {
subAccounts
});
},
deleteAccount: ({ id }) => {
return axios.post('v1/accounts/delete.json', {
id
});
},
};
+3
View File
@@ -212,6 +212,7 @@ export default {
'account cannot have sub accounts': 'Account cannot have sub accounts',
},
'parameter': {
'id': 'ID',
'username': 'Username',
'password': 'Password',
'passcode': 'Passcode',
@@ -322,6 +323,8 @@ export default {
'Account currency cannot be empty': 'Account currency cannot be empty',
'You have added a new account': 'You have added a new account',
'Unable to add account': 'Unable to add account',
'Are you sure you want to delete this account?': 'Are you sure you want to delete this account?',
'Unable to delete this account': 'Unable to delete this account',
'User Profile': 'User Profile',
'Language': 'Language',
'Enable Thousands Separator': 'Enable Thousands Separator',
+3
View File
@@ -212,6 +212,7 @@ export default {
'account cannot have sub accounts': '账户不能包含子账户',
},
'parameter': {
'id': 'ID',
'username': '用户名',
'password': '密码',
'passcode': '验证码',
@@ -322,6 +323,8 @@ export default {
'Account currency cannot be empty': '账户货币不能为空',
'You have added a new account': '您已经添加新账户',
'Unable to add account': '无法添加账户',
'Are you sure you want to delete this account?': '您确定要删除该账户吗?',
'Unable to delete this account': '无法删除该账户',
'User Profile': '用户信息',
'Language': '语言',
'Enable Thousands Separator': '启用千位分隔符',
+43 -2
View File
@@ -41,7 +41,8 @@
<f7-card-header>{{ $t(accountCategory.name) }}</f7-card-header>
<f7-card-content :padding="false">
<f7-list>
<f7-list-item v-for="account in accounts[accountCategory.id]" :key="account.id"
<f7-list-item v-for="account in accounts[accountCategory.id]"
:key="account.id" :id="account | accountDomId"
:title="account.name" :after="account.balance | currency(account.currency)"
link="#" swipeout
>
@@ -140,8 +141,48 @@ export default {
edit() {
},
remove() {
remove(account) {
const self = this;
const app = self.$f7;
const $$ = app.$;
self.$confirm('Are you sure you want to delete this account?', () => {
self.$showLoading();
self.$services.deleteAccount({
id: account.id
}).then(response => {
self.$hideLoading();
const data = response.data;
if (!data || !data.success || !data.result) {
self.$alert('Unable to delete this account');
return;
}
app.swipeout.delete($$(`#${self.$options.filters.accountDomId(account)}`), () => {
const accountList = self.accounts[account.category];
for (let i = 0; i < accountList.length; i++) {
if (accountList[i].id === account.id) {
accountList.splice(i, 1);
}
}
});
}).catch(error => {
self.$hideLoading();
if (error.response && error.response.data && error.response.data.errorMessage) {
self.$alert({error: error.response.data});
} else if (!error.processed) {
self.$alert('Unable to delete this account');
}
});
});
}
},
filters: {
accountDomId(account) {
return 'account_' + account.id;
}
}
};