From dbb1843285e0489433bb34ef3eca60120ed315b7 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Mon, 2 Sep 2024 23:37:33 +0800 Subject: [PATCH] code refactor --- pkg/api/users.go | 10 ++++---- pkg/cli/user_data.go | 2 +- pkg/{models => core}/datetime.go | 2 +- pkg/models/user.go | 44 ++++++++++++++++---------------- pkg/services/users.go | 10 ++++---- 5 files changed, 34 insertions(+), 34 deletions(-) rename pkg/{models => core}/datetime.go (99%) diff --git a/pkg/api/users.go b/pkg/api/users.go index 0ed76fbc..001d871f 100644 --- a/pkg/api/users.go +++ b/pkg/api/users.go @@ -330,7 +330,7 @@ func (a *UsersApi) UserUpdateProfileHandler(c *core.WebContext) (any, *errs.Erro userNew.FirstDayOfWeek = *userUpdateReq.FirstDayOfWeek anythingUpdate = true } else { - userNew.FirstDayOfWeek = models.WEEKDAY_INVALID + userNew.FirstDayOfWeek = core.WEEKDAY_INVALID } if userUpdateReq.LongDateFormat != nil && *userUpdateReq.LongDateFormat != user.LongDateFormat { @@ -338,7 +338,7 @@ func (a *UsersApi) UserUpdateProfileHandler(c *core.WebContext) (any, *errs.Erro userNew.LongDateFormat = *userUpdateReq.LongDateFormat anythingUpdate = true } else { - userNew.LongDateFormat = models.LONG_DATE_FORMAT_INVALID + userNew.LongDateFormat = core.LONG_DATE_FORMAT_INVALID } if userUpdateReq.ShortDateFormat != nil && *userUpdateReq.ShortDateFormat != user.ShortDateFormat { @@ -346,7 +346,7 @@ func (a *UsersApi) UserUpdateProfileHandler(c *core.WebContext) (any, *errs.Erro userNew.ShortDateFormat = *userUpdateReq.ShortDateFormat anythingUpdate = true } else { - userNew.ShortDateFormat = models.SHORT_DATE_FORMAT_INVALID + userNew.ShortDateFormat = core.SHORT_DATE_FORMAT_INVALID } if userUpdateReq.LongTimeFormat != nil && *userUpdateReq.LongTimeFormat != user.LongTimeFormat { @@ -354,7 +354,7 @@ func (a *UsersApi) UserUpdateProfileHandler(c *core.WebContext) (any, *errs.Erro userNew.LongTimeFormat = *userUpdateReq.LongTimeFormat anythingUpdate = true } else { - userNew.LongTimeFormat = models.LONG_TIME_FORMAT_INVALID + userNew.LongTimeFormat = core.LONG_TIME_FORMAT_INVALID } if userUpdateReq.ShortTimeFormat != nil && *userUpdateReq.ShortTimeFormat != user.ShortTimeFormat { @@ -362,7 +362,7 @@ func (a *UsersApi) UserUpdateProfileHandler(c *core.WebContext) (any, *errs.Erro userNew.ShortTimeFormat = *userUpdateReq.ShortTimeFormat anythingUpdate = true } else { - userNew.ShortTimeFormat = models.SHORT_TIME_FORMAT_INVALID + userNew.ShortTimeFormat = core.SHORT_TIME_FORMAT_INVALID } if userUpdateReq.DecimalSeparator != nil && *userUpdateReq.DecimalSeparator != user.DecimalSeparator { diff --git a/pkg/cli/user_data.go b/pkg/cli/user_data.go index e970f18a..f2ea8120 100644 --- a/pkg/cli/user_data.go +++ b/pkg/cli/user_data.go @@ -89,7 +89,7 @@ func (l *UserDataCli) AddNewUser(c *core.CliContext, username string, email stri Nickname: nickname, Password: password, DefaultCurrency: defaultCurrency, - FirstDayOfWeek: models.WEEKDAY_SUNDAY, + FirstDayOfWeek: core.WEEKDAY_SUNDAY, TransactionEditScope: models.TRANSACTION_EDIT_SCOPE_ALL, } diff --git a/pkg/models/datetime.go b/pkg/core/datetime.go similarity index 99% rename from pkg/models/datetime.go rename to pkg/core/datetime.go index 1003fe7c..42e7a977 100644 --- a/pkg/models/datetime.go +++ b/pkg/core/datetime.go @@ -1,4 +1,4 @@ -package models +package core import "fmt" diff --git a/pkg/models/user.go b/pkg/models/user.go index fdd7ea04..9d267399 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -93,11 +93,11 @@ type User struct { TransactionEditScope TransactionEditScope `xorm:"TINYINT NOT NULL"` Language string `xorm:"VARCHAR(10)"` DefaultCurrency string `xorm:"VARCHAR(3) NOT NULL"` - FirstDayOfWeek WeekDay `xorm:"TINYINT NOT NULL"` - LongDateFormat LongDateFormat `xorm:"TINYINT"` - ShortDateFormat ShortDateFormat `xorm:"TINYINT"` - LongTimeFormat LongTimeFormat `xorm:"TINYINT"` - ShortTimeFormat ShortTimeFormat `xorm:"TINYINT"` + FirstDayOfWeek core.WeekDay `xorm:"TINYINT NOT NULL"` + LongDateFormat core.LongDateFormat `xorm:"TINYINT"` + ShortDateFormat core.ShortDateFormat `xorm:"TINYINT"` + LongTimeFormat core.LongTimeFormat `xorm:"TINYINT"` + ShortTimeFormat core.ShortTimeFormat `xorm:"TINYINT"` DecimalSeparator core.DecimalSeparator `xorm:"TINYINT"` DigitGroupingSymbol core.DigitGroupingSymbol `xorm:"TINYINT"` DigitGrouping core.DigitGroupingType `xorm:"TINYINT"` @@ -124,11 +124,11 @@ type UserBasicInfo struct { TransactionEditScope TransactionEditScope `json:"transactionEditScope"` Language string `json:"language"` DefaultCurrency string `json:"defaultCurrency"` - FirstDayOfWeek WeekDay `json:"firstDayOfWeek"` - LongDateFormat LongDateFormat `json:"longDateFormat"` - ShortDateFormat ShortDateFormat `json:"shortDateFormat"` - LongTimeFormat LongTimeFormat `json:"longTimeFormat"` - ShortTimeFormat ShortTimeFormat `json:"shortTimeFormat"` + FirstDayOfWeek core.WeekDay `json:"firstDayOfWeek"` + LongDateFormat core.LongDateFormat `json:"longDateFormat"` + ShortDateFormat core.ShortDateFormat `json:"shortDateFormat"` + LongTimeFormat core.LongTimeFormat `json:"longTimeFormat"` + ShortTimeFormat core.ShortTimeFormat `json:"shortTimeFormat"` DecimalSeparator core.DecimalSeparator `json:"decimalSeparator"` DigitGroupingSymbol core.DigitGroupingSymbol `json:"digitGroupingSymbol"` DigitGrouping core.DigitGroupingType `json:"digitGrouping"` @@ -146,13 +146,13 @@ type UserLoginRequest struct { // UserRegisterRequest represents all parameters of user registering request type UserRegisterRequest struct { - Username string `json:"username" binding:"required,notBlank,max=32,validUsername"` - Email string `json:"email" binding:"required,notBlank,max=100,validEmail"` - Nickname string `json:"nickname" binding:"required,notBlank,max=64"` - Password string `json:"password" binding:"required,min=6,max=128"` - Language string `json:"language" binding:"required,min=2,max=16"` - DefaultCurrency string `json:"defaultCurrency" binding:"required,len=3,validCurrency"` - FirstDayOfWeek WeekDay `json:"firstDayOfWeek" binding:"min=0,max=6"` + Username string `json:"username" binding:"required,notBlank,max=32,validUsername"` + Email string `json:"email" binding:"required,notBlank,max=100,validEmail"` + Nickname string `json:"nickname" binding:"required,notBlank,max=64"` + Password string `json:"password" binding:"required,min=6,max=128"` + Language string `json:"language" binding:"required,min=2,max=16"` + DefaultCurrency string `json:"defaultCurrency" binding:"required,len=3,validCurrency"` + FirstDayOfWeek core.WeekDay `json:"firstDayOfWeek" binding:"min=0,max=6"` TransactionCategoryCreateBatchRequest } @@ -184,11 +184,11 @@ type UserProfileUpdateRequest struct { TransactionEditScope *TransactionEditScope `json:"transactionEditScope" binding:"omitempty,min=0,max=6"` Language string `json:"language" binding:"omitempty,min=2,max=16"` DefaultCurrency string `json:"defaultCurrency" binding:"omitempty,len=3,validCurrency"` - FirstDayOfWeek *WeekDay `json:"firstDayOfWeek" binding:"omitempty,min=0,max=6"` - LongDateFormat *LongDateFormat `json:"longDateFormat" 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"` - ShortTimeFormat *ShortTimeFormat `json:"shortTimeFormat" binding:"omitempty,min=0,max=3"` + FirstDayOfWeek *core.WeekDay `json:"firstDayOfWeek" binding:"omitempty,min=0,max=6"` + LongDateFormat *core.LongDateFormat `json:"longDateFormat" binding:"omitempty,min=0,max=3"` + ShortDateFormat *core.ShortDateFormat `json:"shortDateFormat" binding:"omitempty,min=0,max=3"` + LongTimeFormat *core.LongTimeFormat `json:"longTimeFormat" binding:"omitempty,min=0,max=3"` + ShortTimeFormat *core.ShortTimeFormat `json:"shortTimeFormat" binding:"omitempty,min=0,max=3"` DecimalSeparator *core.DecimalSeparator `json:"decimalSeparator" binding:"omitempty,min=0,max=3"` DigitGroupingSymbol *core.DigitGroupingSymbol `json:"digitGroupingSymbol" binding:"omitempty,min=0,max=4"` DigitGrouping *core.DigitGroupingType `json:"digitGrouping" binding:"omitempty,min=0,max=2"` diff --git a/pkg/services/users.go b/pkg/services/users.go index 091a4cae..155b37c4 100644 --- a/pkg/services/users.go +++ b/pkg/services/users.go @@ -281,23 +281,23 @@ func (s *UserService) UpdateUser(c core.Context, user *models.User, modifyUserLa updateCols = append(updateCols, "default_currency") } - if models.WEEKDAY_SUNDAY <= user.FirstDayOfWeek && user.FirstDayOfWeek <= models.WEEKDAY_SATURDAY { + if core.WEEKDAY_SUNDAY <= user.FirstDayOfWeek && user.FirstDayOfWeek <= core.WEEKDAY_SATURDAY { updateCols = append(updateCols, "first_day_of_week") } - if models.LONG_DATE_FORMAT_DEFAULT <= user.LongDateFormat && user.LongDateFormat <= models.LONG_DATE_FORMAT_D_M_YYYY { + if core.LONG_DATE_FORMAT_DEFAULT <= user.LongDateFormat && user.LongDateFormat <= core.LONG_DATE_FORMAT_D_M_YYYY { updateCols = append(updateCols, "long_date_format") } - if models.SHORT_DATE_FORMAT_DEFAULT <= user.ShortDateFormat && user.ShortDateFormat <= models.SHORT_DATE_FORMAT_D_M_YYYY { + if core.SHORT_DATE_FORMAT_DEFAULT <= user.ShortDateFormat && user.ShortDateFormat <= core.SHORT_DATE_FORMAT_D_M_YYYY { updateCols = append(updateCols, "short_date_format") } - if models.LONG_TIME_FORMAT_DEFAULT <= user.LongTimeFormat && user.LongTimeFormat <= models.LONG_TIME_FORMAT_HH_MM_SS_A { + if core.LONG_TIME_FORMAT_DEFAULT <= user.LongTimeFormat && user.LongTimeFormat <= core.LONG_TIME_FORMAT_HH_MM_SS_A { updateCols = append(updateCols, "long_time_format") } - if models.SHORT_TIME_FORMAT_DEFAULT <= user.ShortTimeFormat && user.ShortTimeFormat <= models.SHORT_TIME_FORMAT_HH_MM_A { + if core.SHORT_TIME_FORMAT_DEFAULT <= user.ShortTimeFormat && user.ShortTimeFormat <= core.SHORT_TIME_FORMAT_HH_MM_A { updateCols = append(updateCols, "short_time_format") }