code refactor

This commit is contained in:
MaysWind
2024-08-05 00:50:45 +08:00
parent 8f8a94cd66
commit c137156c97
8 changed files with 87 additions and 78 deletions
+6 -6
View File
@@ -356,7 +356,7 @@ func (a *UsersApi) UserUpdateProfileHandler(c *core.Context) (any, *errs.Error)
userNew.DecimalSeparator = *userUpdateReq.DecimalSeparator userNew.DecimalSeparator = *userUpdateReq.DecimalSeparator
anythingUpdate = true anythingUpdate = true
} else { } else {
userNew.DecimalSeparator = models.DECIMAL_SEPARATOR_INVALID userNew.DecimalSeparator = core.DECIMAL_SEPARATOR_INVALID
} }
if userUpdateReq.DigitGroupingSymbol != nil && *userUpdateReq.DigitGroupingSymbol != user.DigitGroupingSymbol { if userUpdateReq.DigitGroupingSymbol != nil && *userUpdateReq.DigitGroupingSymbol != user.DigitGroupingSymbol {
@@ -364,7 +364,7 @@ func (a *UsersApi) UserUpdateProfileHandler(c *core.Context) (any, *errs.Error)
userNew.DigitGroupingSymbol = *userUpdateReq.DigitGroupingSymbol userNew.DigitGroupingSymbol = *userUpdateReq.DigitGroupingSymbol
anythingUpdate = true anythingUpdate = true
} else { } else {
userNew.DigitGroupingSymbol = models.DIGIT_GROUPING_SYMBOL_INVALID userNew.DigitGroupingSymbol = core.DIGIT_GROUPING_SYMBOL_INVALID
} }
if userUpdateReq.DigitGrouping != nil && *userUpdateReq.DigitGrouping != user.DigitGrouping { if userUpdateReq.DigitGrouping != nil && *userUpdateReq.DigitGrouping != user.DigitGrouping {
@@ -372,7 +372,7 @@ func (a *UsersApi) UserUpdateProfileHandler(c *core.Context) (any, *errs.Error)
userNew.DigitGrouping = *userUpdateReq.DigitGrouping userNew.DigitGrouping = *userUpdateReq.DigitGrouping
anythingUpdate = true anythingUpdate = true
} else { } else {
userNew.DigitGrouping = models.DIGIT_GROUPING_TYPE_INVALID userNew.DigitGrouping = core.DIGIT_GROUPING_TYPE_INVALID
} }
if userUpdateReq.CurrencyDisplayType != nil && *userUpdateReq.CurrencyDisplayType != user.CurrencyDisplayType { if userUpdateReq.CurrencyDisplayType != nil && *userUpdateReq.CurrencyDisplayType != user.CurrencyDisplayType {
@@ -399,15 +399,15 @@ func (a *UsersApi) UserUpdateProfileHandler(c *core.Context) (any, *errs.Error)
userNew.IncomeAmountColor = models.AMOUNT_COLOR_TYPE_INVALID userNew.IncomeAmountColor = models.AMOUNT_COLOR_TYPE_INVALID
} }
if modifyUserLanguage || userNew.DecimalSeparator != models.DECIMAL_SEPARATOR_INVALID || userNew.DigitGroupingSymbol != models.DIGIT_GROUPING_SYMBOL_INVALID { if modifyUserLanguage || userNew.DecimalSeparator != core.DECIMAL_SEPARATOR_INVALID || userNew.DigitGroupingSymbol != core.DIGIT_GROUPING_SYMBOL_INVALID {
decimalSeparator := userNew.DecimalSeparator decimalSeparator := userNew.DecimalSeparator
digitGroupingSymbol := userNew.DigitGroupingSymbol digitGroupingSymbol := userNew.DigitGroupingSymbol
if userNew.DecimalSeparator == models.DECIMAL_SEPARATOR_INVALID { if userNew.DecimalSeparator == core.DECIMAL_SEPARATOR_INVALID {
decimalSeparator = user.DecimalSeparator decimalSeparator = user.DecimalSeparator
} }
if userNew.DigitGroupingSymbol == models.DIGIT_GROUPING_SYMBOL_INVALID { if userNew.DigitGroupingSymbol == core.DIGIT_GROUPING_SYMBOL_INVALID {
digitGroupingSymbol = user.DigitGroupingSymbol digitGroupingSymbol = user.DigitGroupingSymbol
} }
@@ -1,4 +1,4 @@
package models package core
import ( import (
"fmt" "fmt"
+7 -5
View File
@@ -1,6 +1,8 @@
package locales package locales
import "github.com/mayswind/ezbookkeeping/pkg/models" import (
"github.com/mayswind/ezbookkeeping/pkg/core"
)
// DefaultLanguage represents the default language // DefaultLanguage represents the default language
var DefaultLanguage = en var DefaultLanguage = en
@@ -26,8 +28,8 @@ func GetLocaleTextItems(locale string) *LocaleTextItems {
return DefaultLanguage return DefaultLanguage
} }
func IsDecimalSeparatorEqualsDigitGroupingSymbol(decimalSeparator models.DecimalSeparator, digitGroupingSymbol models.DigitGroupingSymbol, locale string) bool { func IsDecimalSeparatorEqualsDigitGroupingSymbol(decimalSeparator core.DecimalSeparator, digitGroupingSymbol core.DigitGroupingSymbol, locale string) bool {
if decimalSeparator == models.DECIMAL_SEPARATOR_DEFAULT && digitGroupingSymbol == models.DIGIT_GROUPING_SYMBOL_DEFAULT { if decimalSeparator == core.DECIMAL_SEPARATOR_DEFAULT && digitGroupingSymbol == core.DIGIT_GROUPING_SYMBOL_DEFAULT {
return false return false
} }
@@ -37,11 +39,11 @@ func IsDecimalSeparatorEqualsDigitGroupingSymbol(decimalSeparator models.Decimal
localeTextItems := GetLocaleTextItems(locale) localeTextItems := GetLocaleTextItems(locale)
if decimalSeparator == models.DECIMAL_SEPARATOR_DEFAULT { if decimalSeparator == core.DECIMAL_SEPARATOR_DEFAULT {
decimalSeparator = localeTextItems.DefaultTypes.DecimalSeparator decimalSeparator = localeTextItems.DefaultTypes.DecimalSeparator
} }
if digitGroupingSymbol == models.DIGIT_GROUPING_SYMBOL_DEFAULT { if digitGroupingSymbol == core.DIGIT_GROUPING_SYMBOL_DEFAULT {
digitGroupingSymbol = localeTextItems.DefaultTypes.DigitGroupingSymbol digitGroupingSymbol = localeTextItems.DefaultTypes.DigitGroupingSymbol
} }
+5 -3
View File
@@ -1,6 +1,8 @@
package locales package locales
import "github.com/mayswind/ezbookkeeping/pkg/models" import (
"github.com/mayswind/ezbookkeeping/pkg/core"
)
// LocaleTextItems represents all text items need to be translated // LocaleTextItems represents all text items need to be translated
type LocaleTextItems struct { type LocaleTextItems struct {
@@ -10,8 +12,8 @@ type LocaleTextItems struct {
} }
type DefaultTypes struct { type DefaultTypes struct {
DecimalSeparator models.DecimalSeparator DecimalSeparator core.DecimalSeparator
DigitGroupingSymbol models.DigitGroupingSymbol DigitGroupingSymbol core.DigitGroupingSymbol
} }
// VerifyEmailTextItems represents text items need to be translated in verify mail // VerifyEmailTextItems represents text items need to be translated in verify mail
+5 -3
View File
@@ -1,11 +1,13 @@
package locales package locales
import "github.com/mayswind/ezbookkeeping/pkg/models" import (
"github.com/mayswind/ezbookkeeping/pkg/core"
)
var en = &LocaleTextItems{ var en = &LocaleTextItems{
DefaultTypes: &DefaultTypes{ DefaultTypes: &DefaultTypes{
DecimalSeparator: models.DECIMAL_SEPARATOR_DOT, DecimalSeparator: core.DECIMAL_SEPARATOR_DOT,
DigitGroupingSymbol: models.DIGIT_GROUPING_SYMBOL_COMMA, DigitGroupingSymbol: core.DIGIT_GROUPING_SYMBOL_COMMA,
}, },
VerifyEmailTextItems: &VerifyEmailTextItems{ VerifyEmailTextItems: &VerifyEmailTextItems{
Title: "Verify Email", Title: "Verify Email",
+5 -3
View File
@@ -1,11 +1,13 @@
package locales package locales
import "github.com/mayswind/ezbookkeeping/pkg/models" import (
"github.com/mayswind/ezbookkeeping/pkg/core"
)
var zhHans = &LocaleTextItems{ var zhHans = &LocaleTextItems{
DefaultTypes: &DefaultTypes{ DefaultTypes: &DefaultTypes{
DecimalSeparator: models.DECIMAL_SEPARATOR_DOT, DecimalSeparator: core.DECIMAL_SEPARATOR_DOT,
DigitGroupingSymbol: models.DIGIT_GROUPING_SYMBOL_COMMA, DigitGroupingSymbol: core.DIGIT_GROUPING_SYMBOL_COMMA,
}, },
VerifyEmailTextItems: &VerifyEmailTextItems{ VerifyEmailTextItems: &VerifyEmailTextItems{
Title: "验证邮箱", Title: "验证邮箱",
+55 -54
View File
@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/settings" "github.com/mayswind/ezbookkeeping/pkg/settings"
"github.com/mayswind/ezbookkeeping/pkg/utils" "github.com/mayswind/ezbookkeeping/pkg/utils"
) )
@@ -90,20 +91,20 @@ type User struct {
Salt string `xorm:"VARCHAR(10) NOT NULL"` Salt string `xorm:"VARCHAR(10) NOT NULL"`
CustomAvatarType string `xorm:"VARCHAR(10)"` CustomAvatarType string `xorm:"VARCHAR(10)"`
DefaultAccountId int64 DefaultAccountId int64
TransactionEditScope TransactionEditScope `xorm:"TINYINT NOT NULL"` TransactionEditScope TransactionEditScope `xorm:"TINYINT NOT NULL"`
Language string `xorm:"VARCHAR(10)"` Language string `xorm:"VARCHAR(10)"`
DefaultCurrency string `xorm:"VARCHAR(3) NOT NULL"` DefaultCurrency string `xorm:"VARCHAR(3) NOT NULL"`
FirstDayOfWeek WeekDay `xorm:"TINYINT NOT NULL"` FirstDayOfWeek WeekDay `xorm:"TINYINT NOT NULL"`
LongDateFormat LongDateFormat `xorm:"TINYINT"` LongDateFormat LongDateFormat `xorm:"TINYINT"`
ShortDateFormat ShortDateFormat `xorm:"TINYINT"` ShortDateFormat ShortDateFormat `xorm:"TINYINT"`
LongTimeFormat LongTimeFormat `xorm:"TINYINT"` LongTimeFormat LongTimeFormat `xorm:"TINYINT"`
ShortTimeFormat ShortTimeFormat `xorm:"TINYINT"` ShortTimeFormat ShortTimeFormat `xorm:"TINYINT"`
DecimalSeparator DecimalSeparator `xorm:"TINYINT"` DecimalSeparator core.DecimalSeparator `xorm:"TINYINT"`
DigitGroupingSymbol DigitGroupingSymbol `xorm:"TINYINT"` DigitGroupingSymbol core.DigitGroupingSymbol `xorm:"TINYINT"`
DigitGrouping DigitGroupingType `xorm:"TINYINT"` DigitGrouping core.DigitGroupingType `xorm:"TINYINT"`
CurrencyDisplayType CurrencyDisplayType `xorm:"TINYINT"` CurrencyDisplayType CurrencyDisplayType `xorm:"TINYINT"`
ExpenseAmountColor AmountColorType `xorm:"TINYINT"` ExpenseAmountColor AmountColorType `xorm:"TINYINT"`
IncomeAmountColor AmountColorType `xorm:"TINYINT"` IncomeAmountColor AmountColorType `xorm:"TINYINT"`
Disabled bool Disabled bool
Deleted bool `xorm:"NOT NULL"` Deleted bool `xorm:"NOT NULL"`
EmailVerified bool `xorm:"NOT NULL"` EmailVerified bool `xorm:"NOT NULL"`
@@ -115,27 +116,27 @@ type User struct {
// UserBasicInfo represents a view-object of user basic info // UserBasicInfo represents a view-object of user basic info
type UserBasicInfo struct { type UserBasicInfo struct {
Username string `json:"username"` Username string `json:"username"`
Email string `json:"email"` Email string `json:"email"`
Nickname string `json:"nickname"` Nickname string `json:"nickname"`
AvatarUrl string `json:"avatar"` AvatarUrl string `json:"avatar"`
AvatarProvider string `json:"avatarProvider,omitempty"` AvatarProvider string `json:"avatarProvider,omitempty"`
DefaultAccountId int64 `json:"defaultAccountId,string"` DefaultAccountId int64 `json:"defaultAccountId,string"`
TransactionEditScope TransactionEditScope `json:"transactionEditScope"` TransactionEditScope TransactionEditScope `json:"transactionEditScope"`
Language string `json:"language"` Language string `json:"language"`
DefaultCurrency string `json:"defaultCurrency"` DefaultCurrency string `json:"defaultCurrency"`
FirstDayOfWeek WeekDay `json:"firstDayOfWeek"` FirstDayOfWeek WeekDay `json:"firstDayOfWeek"`
LongDateFormat LongDateFormat `json:"longDateFormat"` LongDateFormat LongDateFormat `json:"longDateFormat"`
ShortDateFormat ShortDateFormat `json:"shortDateFormat"` ShortDateFormat ShortDateFormat `json:"shortDateFormat"`
LongTimeFormat LongTimeFormat `json:"longTimeFormat"` LongTimeFormat LongTimeFormat `json:"longTimeFormat"`
ShortTimeFormat ShortTimeFormat `json:"shortTimeFormat"` ShortTimeFormat ShortTimeFormat `json:"shortTimeFormat"`
DecimalSeparator DecimalSeparator `json:"decimalSeparator"` DecimalSeparator core.DecimalSeparator `json:"decimalSeparator"`
DigitGroupingSymbol DigitGroupingSymbol `json:"digitGroupingSymbol"` DigitGroupingSymbol core.DigitGroupingSymbol `json:"digitGroupingSymbol"`
DigitGrouping DigitGroupingType `json:"digitGrouping"` DigitGrouping core.DigitGroupingType `json:"digitGrouping"`
CurrencyDisplayType CurrencyDisplayType `json:"currencyDisplayType"` CurrencyDisplayType CurrencyDisplayType `json:"currencyDisplayType"`
ExpenseAmountColor AmountColorType `json:"expenseAmountColor"` ExpenseAmountColor AmountColorType `json:"expenseAmountColor"`
IncomeAmountColor AmountColorType `json:"incomeAmountColor"` IncomeAmountColor AmountColorType `json:"incomeAmountColor"`
EmailVerified bool `json:"emailVerified"` EmailVerified bool `json:"emailVerified"`
} }
// UserLoginRequest represents all parameters of user login request // UserLoginRequest represents all parameters of user login request
@@ -175,25 +176,25 @@ type UserResendVerifyEmailRequest struct {
// UserProfileUpdateRequest represents all parameters of user updating profile request // UserProfileUpdateRequest represents all parameters of user updating profile request
type UserProfileUpdateRequest struct { type UserProfileUpdateRequest struct {
Email string `json:"email" binding:"omitempty,notBlank,max=100,validEmail"` Email string `json:"email" binding:"omitempty,notBlank,max=100,validEmail"`
Nickname string `json:"nickname" binding:"omitempty,notBlank,max=64"` Nickname string `json:"nickname" binding:"omitempty,notBlank,max=64"`
Password string `json:"password" binding:"omitempty,min=6,max=128"` Password string `json:"password" binding:"omitempty,min=6,max=128"`
OldPassword string `json:"oldPassword" binding:"omitempty,min=6,max=128"` OldPassword string `json:"oldPassword" binding:"omitempty,min=6,max=128"`
DefaultAccountId int64 `json:"defaultAccountId,string" binding:"omitempty,min=1"` DefaultAccountId int64 `json:"defaultAccountId,string" binding:"omitempty,min=1"`
TransactionEditScope *TransactionEditScope `json:"transactionEditScope" binding:"omitempty,min=0,max=6"` TransactionEditScope *TransactionEditScope `json:"transactionEditScope" binding:"omitempty,min=0,max=6"`
Language string `json:"language" binding:"omitempty,min=2,max=16"` Language string `json:"language" binding:"omitempty,min=2,max=16"`
DefaultCurrency string `json:"defaultCurrency" binding:"omitempty,len=3,validCurrency"` DefaultCurrency string `json:"defaultCurrency" binding:"omitempty,len=3,validCurrency"`
FirstDayOfWeek *WeekDay `json:"firstDayOfWeek" binding:"omitempty,min=0,max=6"` FirstDayOfWeek *WeekDay `json:"firstDayOfWeek" binding:"omitempty,min=0,max=6"`
LongDateFormat *LongDateFormat `json:"longDateFormat" binding:"omitempty,min=0,max=3"` LongDateFormat *LongDateFormat `json:"longDateFormat" binding:"omitempty,min=0,max=3"`
ShortDateFormat *ShortDateFormat `json:"shortDateFormat" binding:"omitempty,min=0,max=3"` ShortDateFormat *ShortDateFormat `json:"shortDateFormat" binding:"omitempty,min=0,max=3"`
LongTimeFormat *LongTimeFormat `json:"longTimeFormat" binding:"omitempty,min=0,max=3"` LongTimeFormat *LongTimeFormat `json:"longTimeFormat" binding:"omitempty,min=0,max=3"`
ShortTimeFormat *ShortTimeFormat `json:"shortTimeFormat" binding:"omitempty,min=0,max=3"` ShortTimeFormat *ShortTimeFormat `json:"shortTimeFormat" binding:"omitempty,min=0,max=3"`
DecimalSeparator *DecimalSeparator `json:"decimalSeparator" binding:"omitempty,min=0,max=3"` DecimalSeparator *core.DecimalSeparator `json:"decimalSeparator" binding:"omitempty,min=0,max=3"`
DigitGroupingSymbol *DigitGroupingSymbol `json:"digitGroupingSymbol" binding:"omitempty,min=0,max=4"` DigitGroupingSymbol *core.DigitGroupingSymbol `json:"digitGroupingSymbol" binding:"omitempty,min=0,max=4"`
DigitGrouping *DigitGroupingType `json:"digitGrouping" binding:"omitempty,min=0,max=2"` DigitGrouping *core.DigitGroupingType `json:"digitGrouping" binding:"omitempty,min=0,max=2"`
CurrencyDisplayType *CurrencyDisplayType `json:"currencyDisplayType" binding:"omitempty,min=0,max=11"` CurrencyDisplayType *CurrencyDisplayType `json:"currencyDisplayType" binding:"omitempty,min=0,max=11"`
ExpenseAmountColor *AmountColorType `json:"expenseAmountColor" binding:"omitempty,min=0,max=4"` ExpenseAmountColor *AmountColorType `json:"expenseAmountColor" binding:"omitempty,min=0,max=4"`
IncomeAmountColor *AmountColorType `json:"incomeAmountColor" binding:"omitempty,min=0,max=4"` IncomeAmountColor *AmountColorType `json:"incomeAmountColor" binding:"omitempty,min=0,max=4"`
} }
// UserProfileUpdateResponse represents the data returns to frontend after updating profile // UserProfileUpdateResponse represents the data returns to frontend after updating profile
+3 -3
View File
@@ -248,15 +248,15 @@ func (s *UserService) UpdateUser(c *core.Context, user *models.User, modifyUserL
updateCols = append(updateCols, "short_time_format") updateCols = append(updateCols, "short_time_format")
} }
if models.DECIMAL_SEPARATOR_DEFAULT <= user.DecimalSeparator && user.DecimalSeparator <= models.DECIMAL_SEPARATOR_SPACE { if core.DECIMAL_SEPARATOR_DEFAULT <= user.DecimalSeparator && user.DecimalSeparator <= core.DECIMAL_SEPARATOR_SPACE {
updateCols = append(updateCols, "decimal_separator") updateCols = append(updateCols, "decimal_separator")
} }
if models.DIGIT_GROUPING_SYMBOL_DEFAULT <= user.DigitGroupingSymbol && user.DigitGroupingSymbol <= models.DIGIT_GROUPING_SYMBOL_APOSTROPHE { if core.DIGIT_GROUPING_SYMBOL_DEFAULT <= user.DigitGroupingSymbol && user.DigitGroupingSymbol <= core.DIGIT_GROUPING_SYMBOL_APOSTROPHE {
updateCols = append(updateCols, "digit_grouping_symbol") updateCols = append(updateCols, "digit_grouping_symbol")
} }
if models.DIGIT_GROUPING_TYPE_DEFAULT <= user.DigitGrouping && user.DigitGrouping <= models.DIGIT_GROUPING_TYPE_THOUSANDS_SEPARATOR { if core.DIGIT_GROUPING_TYPE_DEFAULT <= user.DigitGrouping && user.DigitGrouping <= core.DIGIT_GROUPING_TYPE_THOUSANDS_SEPARATOR {
updateCols = append(updateCols, "digit_grouping") updateCols = append(updateCols, "digit_grouping")
} }