code refactor

This commit is contained in:
MaysWind
2025-04-19 00:04:04 +08:00
parent 1ac633bdd7
commit fd1242490f
6 changed files with 12 additions and 12 deletions
+5 -5
View File
@@ -229,11 +229,11 @@ func (a *TransactionCategoriesApi) CategoryModifyHandler(c *core.WebContext) (an
return nil, errs.ErrNothingWillBeUpdated return nil, errs.ErrNothingWillBeUpdated
} }
if category.ParentCategoryId == 0 && newCategory.ParentCategoryId != 0 { if category.ParentCategoryId == models.LevelOneTransactionCategoryParentId && newCategory.ParentCategoryId != models.LevelOneTransactionCategoryParentId {
return nil, errs.Or(err, errs.ErrNotAllowChangePrimaryTransactionCategoryToSecondary) return nil, errs.Or(err, errs.ErrNotAllowChangePrimaryTransactionCategoryToSecondary)
} }
if category.ParentCategoryId != 0 && newCategory.ParentCategoryId == 0 { if category.ParentCategoryId != models.LevelOneTransactionCategoryParentId && newCategory.ParentCategoryId == models.LevelOneTransactionCategoryParentId {
return nil, errs.Or(err, errs.ErrNotAllowChangeSecondaryTransactionCategoryToPrimary) return nil, errs.Or(err, errs.ErrNotAllowChangeSecondaryTransactionCategoryToPrimary)
} }
@@ -256,7 +256,7 @@ func (a *TransactionCategoriesApi) CategoryModifyHandler(c *core.WebContext) (an
return nil, errs.Or(err, errs.ErrNotAllowChangePrimaryTransactionType) return nil, errs.Or(err, errs.ErrNotAllowChangePrimaryTransactionType)
} }
if toPrimaryCategory.ParentCategoryId != 0 { if toPrimaryCategory.ParentCategoryId != models.LevelOneTransactionCategoryParentId {
return nil, errs.Or(err, errs.ErrNotAllowUseSecondaryTransactionAsPrimaryCategory) return nil, errs.Or(err, errs.ErrNotAllowUseSecondaryTransactionAsPrimaryCategory)
} }
} }
@@ -433,7 +433,7 @@ func (a *TransactionCategoriesApi) getTransactionCategoryListByTypeResponse(cate
for i := 0; i < len(categoryResps); i++ { for i := 0; i < len(categoryResps); i++ {
categoryResp := categoryResps[i] categoryResp := categoryResps[i]
if categoryResp.ParentId <= models.LevelOneTransactionParentId { if categoryResp.ParentId <= models.LevelOneTransactionCategoryParentId {
continue continue
} }
@@ -449,7 +449,7 @@ func (a *TransactionCategoriesApi) getTransactionCategoryListByTypeResponse(cate
finalCategoryResps := make(models.TransactionCategoryInfoResponseSlice, 0) finalCategoryResps := make(models.TransactionCategoryInfoResponseSlice, 0)
for i := 0; i < len(categoryResps); i++ { for i := 0; i < len(categoryResps); i++ {
if parentId <= 0 && categoryResps[i].ParentId == models.LevelOneTransactionParentId { if parentId <= 0 && categoryResps[i].ParentId == models.LevelOneTransactionCategoryParentId {
sort.Sort(categoryResps[i].SubCategories) sort.Sort(categoryResps[i].SubCategories)
finalCategoryResps = append(finalCategoryResps, categoryResps[i]) finalCategoryResps = append(finalCategoryResps, categoryResps[i])
} else if parentId > 0 && categoryResps[i].ParentId == parentId { } else if parentId > 0 && categoryResps[i].ParentId == parentId {
+1 -1
View File
@@ -959,7 +959,7 @@ func (l *UserDataCli) checkTransactionCategory(c *core.CliContext, transaction *
return errs.ErrTransactionCategoryNotFound return errs.ErrTransactionCategoryNotFound
} }
if category.ParentCategoryId == models.LevelOneTransactionParentId { if category.ParentCategoryId == models.LevelOneTransactionCategoryParentId {
log.CliErrorf(c, "[user_data.checkTransactionCategory] the transaction category \"id:%d\" of transaction \"id:%d\" is not a sub category", transaction.CategoryId, transaction.TransactionId) log.CliErrorf(c, "[user_data.checkTransactionCategory] the transaction category \"id:%d\" of transaction \"id:%d\" is not a sub category", transaction.CategoryId, transaction.TransactionId)
return errs.ErrOperationFailed return errs.ErrOperationFailed
} }
@@ -78,7 +78,7 @@ func (c *DataTableTransactionDataExporter) getExportedTransactionCategoryName(da
return "" return ""
} }
if category.ParentCategoryId == 0 { if category.ParentCategoryId == models.LevelOneTransactionCategoryParentId {
return dataTableBuilder.ReplaceDelimiters(category.Name) return dataTableBuilder.ReplaceDelimiters(category.Name)
} }
+2 -2
View File
@@ -1,7 +1,7 @@
package models package models
// LevelOneTransactionParentId represents the parent id of level-one transaction category // LevelOneTransactionCategoryParentId represents the parent id of level-one transaction category
const LevelOneTransactionParentId = 0 const LevelOneTransactionCategoryParentId = 0
// TransactionCategoryType represents transaction category type // TransactionCategoryType represents transaction category type
type TransactionCategoryType byte type TransactionCategoryType byte
+2 -2
View File
@@ -161,7 +161,7 @@ func (s *TransactionCategoryService) GetMaxDisplayOrder(c core.Context, uid int6
} }
category := &models.TransactionCategory{} category := &models.TransactionCategory{}
has, err := s.UserDataDB(uid).NewSession(c).Cols("uid", "deleted", "parent_category_id", "display_order").Where("uid=? AND deleted=? AND type=? AND parent_category_id=?", uid, false, categoryType, models.LevelOneTransactionParentId).OrderBy("display_order desc").Limit(1).Get(category) has, err := s.UserDataDB(uid).NewSession(c).Cols("uid", "deleted", "parent_category_id", "display_order").Where("uid=? AND deleted=? AND type=? AND parent_category_id=?", uid, false, categoryType, models.LevelOneTransactionCategoryParentId).OrderBy("display_order desc").Limit(1).Get(category)
if err != nil { if err != nil {
return 0, err return 0, err
@@ -463,7 +463,7 @@ func (s *TransactionCategoryService) GetSubCategoryNameMapByList(categories []*m
for i := 0; i < len(categories); i++ { for i := 0; i < len(categories); i++ {
category := categories[i] category := categories[i]
if category.ParentCategoryId == models.LevelOneTransactionParentId { if category.ParentCategoryId == models.LevelOneTransactionCategoryParentId {
continue continue
} }
+1 -1
View File
@@ -2191,7 +2191,7 @@ func (s *TransactionService) isCategoryValid(sess *xorm.Session, transaction *mo
return errs.ErrCannotUseHiddenTransactionCategory return errs.ErrCannotUseHiddenTransactionCategory
} }
if category.ParentCategoryId < 1 { if category.ParentCategoryId == models.LevelOneTransactionCategoryParentId {
return errs.ErrCannotUsePrimaryCategoryForTransaction return errs.ErrCannotUsePrimaryCategoryForTransaction
} }