From d8424b8848c4f5f3a3769d9ed290e48fe3f028a5 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sat, 14 Nov 2020 14:11:36 +0800 Subject: [PATCH] support hide/unhide account --- cmd/webserver.go | 1 + pkg/api/accounts.go | 25 +++++++++++-- pkg/models/account.go | 5 +++ pkg/services/accounts.go | 23 ++++++++++++ src/lib/services.js | 6 ++++ src/locales/en.js | 2 ++ src/locales/zh_Hans.js | 2 ++ src/views/mobile/accounts/AccountList.vue | 44 ++++++++++++++++++++++- 8 files changed, 105 insertions(+), 3 deletions(-) diff --git a/cmd/webserver.go b/cmd/webserver.go index c333199d..1704fb7b 100644 --- a/cmd/webserver.go +++ b/cmd/webserver.go @@ -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/hide.json", bindApi(api.Accounts.AccountHideHandler)) apiV1Route.POST("/accounts/move.json", bindApi(api.Accounts.AccountMoveHandler)) apiV1Route.POST("/accounts/delete.json", bindApi(api.Accounts.AccountDeleteHandler)) } diff --git a/pkg/api/accounts.go b/pkg/api/accounts.go index 24b00666..67d5be84 100644 --- a/pkg/api/accounts.go +++ b/pkg/api/accounts.go @@ -124,6 +124,27 @@ func (a *AccountsApi) AccountCreateHandler(c *core.Context) (interface{}, *errs. return accountInfoResp, nil } +func (a *AccountsApi) AccountHideHandler(c *core.Context) (interface{}, *errs.Error) { + var accountHideReq models.AccountHideRequest + err := c.ShouldBindJSON(&accountHideReq) + + if err != nil { + log.WarnfWithRequestId(c, "[accounts.AccountHideHandler] parse request failed, because %s", err.Error()) + return nil, errs.NewIncompleteOrIncorrectSubmissionError(err) + } + + uid := c.GetCurrentUid() + err = a.accounts.HideAccount(uid, []int64{accountHideReq.Id}, accountHideReq.Hidden) + + if err != nil { + log.ErrorfWithRequestId(c, "[accounts.AccountHideHandler] failed to hide account \"id:%d\" for user \"uid:%d\", because %s", accountHideReq.Id, uid, err.Error()) + return nil, errs.Or(err, errs.ErrOperationFailed) + } + + log.InfofWithRequestId(c, "[accounts.AccountHideHandler] user \"uid:%d\" has hidden account \"id:%d\"", uid, accountHideReq.Id) + return true, nil +} + func (a *AccountsApi) AccountMoveHandler(c *core.Context) (interface{}, *errs.Error) { var accountMoveReq models.AccountMoveRequest err := c.ShouldBindJSON(&accountMoveReq) @@ -171,11 +192,11 @@ func (a *AccountsApi) AccountDeleteHandler(c *core.Context) (interface{}, *errs. 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()) + log.ErrorfWithRequestId(c, "[accounts.AccountDeleteHandler] failed to delete account \"id:%d\" 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) + log.InfofWithRequestId(c, "[accounts.AccountDeleteHandler] user \"uid:%d\" has deleted account \"id:%d\"", uid, accountDeleteReq.Id) return true, nil } diff --git a/pkg/models/account.go b/pkg/models/account.go index bd29fb96..e96ca51a 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -51,6 +51,11 @@ type AccountCreateRequest struct { SubAccounts []*AccountCreateRequest `json:"subAccounts" binding:"omitempty"` } +type AccountHideRequest struct { + Id int64 `json:"id,string" binding:"required,min=1"` + Hidden bool `json:"hidden"` +} + type AccountMoveRequest struct { NewDisplayOrders []*AccountNewDisplayOrderRequest `json:"newDisplayOrders"` } diff --git a/pkg/services/accounts.go b/pkg/services/accounts.go index cd14354c..a641a0fc 100644 --- a/pkg/services/accounts.go +++ b/pkg/services/accounts.go @@ -122,6 +122,29 @@ func (s *AccountService) CreateAccounts(mainAccount *models.Account, childrenAcc }) } +func (s *AccountService) HideAccount(uid int64, ids []int64, hidden bool) error { + if uid <= 0 { + return errs.ErrUserIdInvalid + } + + now := time.Now().Unix() + + updateModel := &models.Account{ + Hidden: hidden, + UpdatedUnixTime: now, + } + + return s.UserDataDB(uid).DoTransaction(func(sess *xorm.Session) error { + deletedRows, err := sess.Cols("hidden", "updated_unix_time").In("account_id", ids).Where("uid=? AND deleted=?", uid, false).Update(updateModel) + + if deletedRows < 1 { + return errs.ErrAccountNotFound + } + + return err + }) +} + func (s *AccountService) ModifyAccountDisplayOrders(uid int64, accounts []*models.Account) error { if uid <= 0 { return errs.ErrUserIdInvalid diff --git a/src/lib/services.js b/src/lib/services.js index f1346f9c..c3992dba 100644 --- a/src/lib/services.js +++ b/src/lib/services.js @@ -174,6 +174,12 @@ export default { subAccounts }); }, + hideAccount: ({ id, hidden }) => { + return axios.post('v1/accounts/hide.json', { + id, + hidden + }); + }, moveAccount: ({ newDisplayOrders }) => { return axios.post('v1/accounts/move.json', { newDisplayOrders, diff --git a/src/locales/en.js b/src/locales/en.js index 33722f33..f41c76ed 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -324,6 +324,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', + 'Unable to hide this account': 'Unable to hide this account', + 'Unable to unhide this account': 'Unable to unhide this account', 'Unable to move account': 'Unable to move 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', diff --git a/src/locales/zh_Hans.js b/src/locales/zh_Hans.js index 49e72cde..9836bf6b 100644 --- a/src/locales/zh_Hans.js +++ b/src/locales/zh_Hans.js @@ -324,6 +324,8 @@ export default { 'Account currency cannot be empty': '账户货币不能为空', 'You have added a new account': '您已经添加新账户', 'Unable to add account': '无法添加账户', + 'Unable to hide this account': '无法隐藏账户', + 'Unable to unhide this account': '无法取消隐藏账户', 'Unable to move account': '无法移动账户', 'Are you sure you want to delete this account?': '您确定要删除该账户吗?', 'Unable to delete this account': '无法删除该账户', diff --git a/src/views/mobile/accounts/AccountList.vue b/src/views/mobile/accounts/AccountList.vue index cae3760d..5ca0010d 100644 --- a/src/views/mobile/accounts/AccountList.vue +++ b/src/views/mobile/accounts/AccountList.vue @@ -55,7 +55,12 @@ :title="account.name" :after="account.balance | currency(account.currency)" link="#" swipeout @taphold.native="setSortable()" > - + + + + + + @@ -278,6 +283,43 @@ export default { }, edit() { + }, + hide(account, hidden) { + const self = this; + + self.$showLoading(); + + self.$services.hideAccount({ + id: account.id, + hidden: hidden + }).then(response => { + self.$hideLoading(); + const data = response.data; + + if (!data || !data.success || !data.result) { + if (hidden) { + self.$toast('Unable to hide this account'); + } else { + self.$toast('Unable to unhide this account'); + } + + return; + } + + account.hidden = hidden; + }).catch(error => { + self.$hideLoading(); + + if (error.response && error.response.data && error.response.data.errorMessage) { + self.$toast({ error: error.response.data }); + } else if (!error.processed) { + if (hidden) { + self.$toast('Unable to hide this account'); + } else { + self.$toast('Unable to unhide this account'); + } + } + }); }, remove(account) { const self = this;