not allow to delete category when other transaction is using it

This commit is contained in:
MaysWind
2020-12-14 00:23:07 +08:00
parent d2c1dcbdbd
commit 99c628cc8a
5 changed files with 27 additions and 5 deletions
+1 -1
View File
@@ -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())
+1
View File
@@ -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")
)
+23 -4
View File
@@ -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
})
}
+1
View File
@@ -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',
+1
View File
@@ -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': '交易标签标题不能为空',