From 99c628cc8a9227e073e6ca00c5b1b56a9c573ddc Mon Sep 17 00:00:00 2001 From: MaysWind Date: Mon, 14 Dec 2020 00:23:07 +0800 Subject: [PATCH] not allow to delete category when other transaction is using it --- pkg/api/transaction_categories.go | 2 +- pkg/errs/transaction_category.go | 1 + pkg/services/transaction_categories.go | 27 ++++++++++++++++++++++---- src/locales/en.js | 1 + src/locales/zh_Hans.js | 1 + 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/pkg/api/transaction_categories.go b/pkg/api/transaction_categories.go index 72c17e53..a277c52d 100644 --- a/pkg/api/transaction_categories.go +++ b/pkg/api/transaction_categories.go @@ -299,7 +299,7 @@ func (a *TransactionCategoriesApi) CategoryDeleteHandler(c *core.Context) (inter } uid := c.GetCurrentUid() - err = a.categories.DeleteCategories(uid, []int64{categoryDeleteReq.Id}) + err = a.categories.DeleteCategory(uid, categoryDeleteReq.Id) if err != nil { log.ErrorfWithRequestId(c, "[transaction_categories.CategoryDeleteHandler] failed to delete category \"id:%d\" for user \"uid:%d\", because %s", categoryDeleteReq.Id, uid, err.Error()) diff --git a/pkg/errs/transaction_category.go b/pkg/errs/transaction_category.go index 90012b66..3f6293b4 100644 --- a/pkg/errs/transaction_category.go +++ b/pkg/errs/transaction_category.go @@ -9,4 +9,5 @@ var ( ErrParentTransactionCategoryNotFound = NewNormalError(NORMAL_SUBCATEGORY_CATEGORY, 3, http.StatusBadRequest, "parent transaction category not found") ErrCannotAddToSecondaryTransactionCategory = NewNormalError(NORMAL_SUBCATEGORY_CATEGORY, 4, http.StatusBadRequest, "cannot add to secondary transaction category") ErrCannotUsePrimaryCategoryForTransaction = NewNormalError(NORMAL_SUBCATEGORY_CATEGORY, 5, http.StatusBadRequest, "cannot use primary category for transaction category") + ErrTransactionCategoryInUseCannotBeDeleted = NewNormalError(NORMAL_SUBCATEGORY_CATEGORY, 6, http.StatusBadRequest, "transaction category is in use and cannot be deleted") ) diff --git a/pkg/services/transaction_categories.go b/pkg/services/transaction_categories.go index d9da0194..0caa02cc 100644 --- a/pkg/services/transaction_categories.go +++ b/pkg/services/transaction_categories.go @@ -256,7 +256,7 @@ func (s *TransactionCategoryService) ModifyCategoryDisplayOrders(uid int64, cate }) } -func (s *TransactionCategoryService) DeleteCategories(uid int64, ids []int64) error { +func (s *TransactionCategoryService) DeleteCategory(uid int64, categoryId int64) error { if uid <= 0 { return errs.ErrUserIdInvalid } @@ -269,7 +269,28 @@ func (s *TransactionCategoryService) DeleteCategories(uid int64, ids []int64) er } return s.UserDataDB(uid).DoTransaction(func(sess *xorm.Session) error { - deletedRows, err := sess.Cols("deleted", "deleted_unix_time").In("category_id", ids).Where("uid=? AND deleted=?", uid, false).Update(updateModel) + var categoryAndSubCategories []*models.TransactionCategory + err := s.UserDataDB(uid).Where("uid=? AND deleted=? AND (category_id=? OR parent_category_id=?)", uid, false, categoryId, categoryId).Find(&categoryAndSubCategories) + + if err != nil { + return err + } else if len(categoryAndSubCategories) < 1 { + return errs.ErrTransactionCategoryNotFound + } + + categoryAndSubCategoryIds := make([]int64, len(categoryAndSubCategories)) + + for i := 0; i < len(categoryAndSubCategories); i++ { + categoryAndSubCategoryIds[i] = categoryAndSubCategories[i].CategoryId + } + + exists, err := sess.Cols("uid", "deleted", "category_id").Where("uid=? AND deleted=?", uid, false).In("category_id", categoryAndSubCategoryIds).Limit(1).Exist(&models.Transaction{}) + + if exists { + return errs.ErrTransactionCategoryInUseCannotBeDeleted + } + + deletedRows, err := sess.Cols("deleted", "deleted_unix_time").Where("uid=? AND deleted=?", uid, false).In("category_id", categoryAndSubCategoryIds).Update(updateModel) if err != nil { return err @@ -277,8 +298,6 @@ func (s *TransactionCategoryService) DeleteCategories(uid int64, ids []int64) er return errs.ErrTransactionCategoryNotFound } - _, err = sess.Cols("deleted", "deleted_unix_time").In("parent_category_id", ids).Where("uid=? AND deleted=?", uid, false).Update(updateModel) - return err }) } diff --git a/src/locales/en.js b/src/locales/en.js index 253062be..5cb4b75e 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -325,6 +325,7 @@ export default { 'parent transaction category not found': 'Parent transaction category is not found', 'cannot add to secondary transaction category': 'Cannot add transaction category to secondary transaction category', 'cannot use primary category for transaction category': 'Cannot use primary category for transaction category', + 'transaction category is in use and cannot be deleted': 'Transaction category is in use and it cannot be deleted', 'transaction tag id is invalid': 'Transaction tag ID is invalid', 'transaction tag not found': 'Transaction tag is not found', 'transaction tag name is empty': 'Transaction tag title is empty', diff --git a/src/locales/zh_Hans.js b/src/locales/zh_Hans.js index d8a0b74a..dba9d27a 100644 --- a/src/locales/zh_Hans.js +++ b/src/locales/zh_Hans.js @@ -325,6 +325,7 @@ export default { 'parent transaction category not found': '父级交易分类不存在', 'cannot add to secondary transaction category': '不能在二级交易分类中添加', 'cannot use primary category for transaction category': '交易分类不能使用一级分类', + 'transaction category is in use and cannot be deleted': '交易分类正在被使用,无法删除', 'transaction tag id is invalid': '交易标签ID无效', 'transaction tag not found': '交易标签不存在', 'transaction tag name is empty': '交易标签标题不能为空',