diff --git a/pkg/models/account.go b/pkg/models/account.go index bc58a9d4..7962c7f9 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -3,8 +3,10 @@ package models // Level-One Account const ACCOUNT_PARENT_ID_LEVEL_ONE = 0 +// AccountCategory represents account category type AccountCategory byte +// Account categories const ( ACCOUNT_CATEGORY_CASH AccountCategory = 1 ACCOUNT_CATEGORY_DEBIT_CARD AccountCategory = 2 @@ -15,7 +17,7 @@ const ( ACCOUNT_CATEGORY_INVESTMENT AccountCategory = 7 ) -var AssetAccountCategory = map[AccountCategory]bool{ +var assetAccountCategory = map[AccountCategory]bool{ ACCOUNT_CATEGORY_CASH: true, ACCOUNT_CATEGORY_DEBIT_CARD: true, ACCOUNT_CATEGORY_CREDIT_CARD: false, @@ -25,7 +27,7 @@ var AssetAccountCategory = map[AccountCategory]bool{ ACCOUNT_CATEGORY_INVESTMENT: true, } -var LiabilityAccountCategory = map[AccountCategory]bool{ +var liabilityAccountCategory = map[AccountCategory]bool{ ACCOUNT_CATEGORY_CASH: false, ACCOUNT_CATEGORY_DEBIT_CARD: false, ACCOUNT_CATEGORY_CREDIT_CARD: true, @@ -35,13 +37,16 @@ var LiabilityAccountCategory = map[AccountCategory]bool{ ACCOUNT_CATEGORY_INVESTMENT: false, } +// AccountType represents account type type AccountType byte +// Account types const ( ACCOUNT_TYPE_SINGLE_ACCOUNT AccountType = 1 ACCOUNT_TYPE_MULTI_SUB_ACCOUNTS AccountType = 2 ) +// Account represents account data stored in database type Account struct { AccountId int64 `xorm:"PK"` Uid int64 `xorm:"INDEX(IDX_account_uid_deleted_parent_account_id_order) NOT NULL"` @@ -62,6 +67,7 @@ type Account struct { DeletedUnixTime int64 } +// AccountCreateRequest represents all parameters of account creation request type AccountCreateRequest struct { Name string `json:"name" binding:"required,notBlank,max=32"` Category AccountCategory `json:"category" binding:"required"` @@ -74,6 +80,7 @@ type AccountCreateRequest struct { SubAccounts []*AccountCreateRequest `json:"subAccounts" binding:"omitempty"` } +// AccountModifyRequest represents all parameters of account modification request type AccountModifyRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` Name string `json:"name" binding:"required,notBlank,max=32"` @@ -85,32 +92,39 @@ type AccountModifyRequest struct { SubAccounts []*AccountModifyRequest `json:"subAccounts" binding:"omitempty"` } +// AccountListRequest represents all parameters of account listing request type AccountListRequest struct { VisibleOnly bool `form:"visible_only"` } +// AccountGetRequest represents all parameters of account getting request type AccountGetRequest struct { Id int64 `form:"id,string" binding:"required,min=1"` } +// AccountHideRequest represents all parameters of account hiding request type AccountHideRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` Hidden bool `json:"hidden"` } +// AccountMoveRequest represents all parameters of account moving request type AccountMoveRequest struct { NewDisplayOrders []*AccountNewDisplayOrderRequest `json:"newDisplayOrders"` } +// AccountNewDisplayOrderRequest represents a data pair of id and display order type AccountNewDisplayOrderRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` DisplayOrder int `json:"displayOrder"` } +// AccountDeleteRequest represents all parameters of account deleting request type AccountDeleteRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` } +// AccountInfoResponse represents a view-object of account type AccountInfoResponse struct { Id int64 `json:"id,string"` Name string `json:"name"` @@ -129,6 +143,7 @@ type AccountInfoResponse struct { SubAccounts AccountInfoResponseSlice `json:"subAccounts,omitempty"` } +// ToAccountInfoResponse returns a view-object according to database model func (a *Account) ToAccountInfoResponse() *AccountInfoResponse { return &AccountInfoResponse{ Id: a.AccountId, @@ -142,22 +157,26 @@ func (a *Account) ToAccountInfoResponse() *AccountInfoResponse { Balance: a.Balance, Comment: a.Comment, DisplayOrder: a.DisplayOrder, - IsAsset: AssetAccountCategory[a.Category], - IsLiability: LiabilityAccountCategory[a.Category], + IsAsset: assetAccountCategory[a.Category], + IsLiability: liabilityAccountCategory[a.Category], Hidden: a.Hidden, } } +// AccountInfoResponseSlice represents the slice data structure of AccountInfoResponse type AccountInfoResponseSlice []*AccountInfoResponse +// Len returns the count of items func (a AccountInfoResponseSlice) Len() int { return len(a) } +// Swap swaps two items func (a AccountInfoResponseSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +// Less reports whether the first item is less than the second one func (a AccountInfoResponseSlice) Less(i, j int) bool { if a[i].Category != a[j].Category { return a[i].Category < a[j].Category diff --git a/pkg/models/auth_response.go b/pkg/models/auth_response.go index 3b545351..10234861 100644 --- a/pkg/models/auth_response.go +++ b/pkg/models/auth_response.go @@ -1,5 +1,6 @@ package models +// AuthResponse returns a view-object of user authorization type AuthResponse struct { Token string `json:"token"` Need2FA bool `json:"need2FA"` diff --git a/pkg/models/exchange_rate.go b/pkg/models/exchange_rate.go index 922b819b..40649ac4 100644 --- a/pkg/models/exchange_rate.go +++ b/pkg/models/exchange_rate.go @@ -2,9 +2,10 @@ package models import "encoding/xml" -const EuroCentralBankDataSource = "European Central Bank" -const EuroCentralBankBaseCurrency = "EUR" +const euroCentralBankDataSource = "European Central Bank" +const euroCentralBankBaseCurrency = "EUR" +// LatestExchangeRateResponse returns a view-object which contains latest exchange rate type LatestExchangeRateResponse struct { DataSource string `json:"dataSource"` Date string `json:"date"` @@ -12,26 +13,31 @@ type LatestExchangeRateResponse struct { ExchangeRates []*LatestExchangeRate `json:"exchangeRates"` } +// LatestExchangeRate represents a data pair of currency and exchange rate type LatestExchangeRate struct { Currency string `json:"currency"` Rate string `json:"rate"` } +// EuroCentralBankExchangeRateData represents the whole data from euro central bank type EuroCentralBankExchangeRateData struct { XMLName xml.Name `xml:"Envelope"` AllExchangeRates []*EuroCentralBankExchangeRates `xml:"Cube>Cube"` } +// EuroCentralBankExchangeRates represents the exchange rates data from euro central bank type EuroCentralBankExchangeRates struct { Date string `xml:"time,attr"` ExchangeRates []*EuroCentralBankExchangeRate `xml:"Cube"` } +// EuroCentralBankExchangeRate represents the exchange rate data from euro central bank type EuroCentralBankExchangeRate struct { Currency string `xml:"currency,attr"` Rate string `xml:"rate,attr"` } +// ToLatestExchangeRateResponse returns a view-object according to original data from euro central bank func (e EuroCentralBankExchangeRateData) ToLatestExchangeRateResponse() *LatestExchangeRateResponse { if len(e.AllExchangeRates) < 1 { return nil @@ -50,15 +56,16 @@ func (e EuroCentralBankExchangeRateData) ToLatestExchangeRateResponse() *LatestE } latestExchangeRateResp := &LatestExchangeRateResponse{ - DataSource: EuroCentralBankDataSource, + DataSource: euroCentralBankDataSource, Date: latestEuroCentralBankExchangeRate.Date, - BaseCurrency: EuroCentralBankBaseCurrency, + BaseCurrency: euroCentralBankBaseCurrency, ExchangeRates: exchangeRates, } return latestExchangeRateResp } +// ToLatestExchangeRate returns a data pair according to original data from euro central bank func (e EuroCentralBankExchangeRate) ToLatestExchangeRate() *LatestExchangeRate { return &LatestExchangeRate{ Currency: e.Currency, diff --git a/pkg/models/token_record.go b/pkg/models/token_record.go index 3e48d4c2..4876e89b 100644 --- a/pkg/models/token_record.go +++ b/pkg/models/token_record.go @@ -2,8 +2,10 @@ package models import "github.com/mayswind/lab/pkg/core" +// The maximum size of user agent stored in database const TOKEN_USER_AGENT_MAX_LENGTH = 255 +// TokenRecord represents token data stored in database type TokenRecord struct { Uid int64 `xorm:"PK INDEX(IDX_token_record_uid_type_expired_time)"` UserTokenId int64 `xorm:"PK"` @@ -14,16 +16,19 @@ type TokenRecord struct { ExpiredUnixTime int64 `xorm:"INDEX(IDX_token_record_uid_type_expired_time)"` } +// TokenRevokeRequest represents all parameters of token revoking request type TokenRevokeRequest struct { TokenId string `json:"tokenId" binding:"required,notBlank"` } +// TokenRefreshResponse represents all parameters of token refreshing request type TokenRefreshResponse struct { NewToken string `json:"newToken"` OldTokenId string `json:"oldTokenId"` User *UserBasicInfo `json:"user"` } +// TokenInfoResponse represents a view-object of token type TokenInfoResponse struct { TokenId string `json:"tokenId"` TokenType core.TokenType `json:"tokenType"` @@ -33,16 +38,20 @@ type TokenInfoResponse struct { IsCurrent bool `json:"isCurrent"` } +// TokenInfoResponseSlice represents the slice data structure of TokenInfoResponse type TokenInfoResponseSlice []*TokenInfoResponse +// Len returns the count of items func (a TokenInfoResponseSlice) Len() int { return len(a) } +// Swap swaps two items func (a TokenInfoResponseSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +// Less reports whether the first item is less than the second one func (a TokenInfoResponseSlice) Less(i, j int) bool { return a[i].ExpiredAt > a[j].ExpiredAt } diff --git a/pkg/models/transaction.go b/pkg/models/transaction.go index 17bff999..93534ae6 100644 --- a/pkg/models/transaction.go +++ b/pkg/models/transaction.go @@ -2,8 +2,10 @@ package models import "github.com/mayswind/lab/pkg/utils" +// TransactionType represents transaction type type TransactionType byte +// Transaction types const ( TRANSACTION_TYPE_MODIFY_BALANCE TransactionType = 1 TRANSACTION_TYPE_INCOME TransactionType = 2 @@ -11,6 +13,7 @@ const ( TRANSACTION_TYPE_TRANSFER TransactionType = 4 ) +// Transaction represents transaction data stored in database type Transaction struct { TransactionId int64 `xorm:"PK"` Uid int64 `xorm:"UNIQUE(UQE_transaction_uid_transaction_time) INDEX(IDX_transaction_uid_deleted_transaction_time) INDEX(IDX_transaction_uid_deleted_type_time) INDEX(IDX_transaction_uid_deleted_category_id_time) NOT NULL"` @@ -28,6 +31,7 @@ type Transaction struct { DeletedUnixTime int64 } +// TransactionCreateRequest represents all parameters of transaction creation request type TransactionCreateRequest struct { Type TransactionType `json:"type" binding:"required"` CategoryId int64 `json:"categoryId,string"` @@ -40,6 +44,7 @@ type TransactionCreateRequest struct { Comment string `json:"comment" binding:"max=255"` } +// TransactionModifyRequest represents all parameters of transaction modification request type TransactionModifyRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` CategoryId int64 `json:"categoryId,string"` @@ -52,11 +57,13 @@ type TransactionModifyRequest struct { Comment string `json:"comment" binding:"max=255"` } +// TransactionListByMaxTimeRequest represents all parameters of transaction listing by max time request type TransactionListByMaxTimeRequest struct { MaxTime int64 `form:"max_time" binding:"required,min=1"` Count int `form:"count" binding:"required,min=1,max=50"` } +// TransactionListInMonthByPageRequest represents all parameters of transaction listing by month request type TransactionListInMonthByPageRequest struct { Year int `form:"year" binding:"required,min=1"` Month int `form:"month" binding:"required,min=1"` @@ -64,14 +71,17 @@ type TransactionListInMonthByPageRequest struct { Count int `form:"count" binding:"required,min=1,max=50"` } +// TransactionGetRequest represents all parameters of transaction getting request type TransactionGetRequest struct { Id int64 `form:"id,string" binding:"required,min=1"` } +// TransactionDeleteRequest represents all parameters of transaction deleting request type TransactionDeleteRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` } +// TransactionInfoResponse represents a view-object of transaction type TransactionInfoResponse struct { Id int64 `json:"id,string"` TimeSequenceId int64 `json:"timeSequenceId,string"` @@ -86,11 +96,13 @@ type TransactionInfoResponse struct { Comment string `json:"comment"` } +// TransactionInfoPageWrapperResponse represents a response of transaction which contains items and next id type TransactionInfoPageWrapperResponse struct { Items TransactionInfoResponseSlice `json:"items"` NextTimeSequenceId *int64 `json:"nextTimeSequenceId,string"` } +// ToTransactionInfoResponse returns a view-object according to database model func (c *Transaction) ToTransactionInfoResponse(tagIds []int64) *TransactionInfoResponse { return &TransactionInfoResponse{ Id: c.TransactionId, @@ -107,16 +119,20 @@ func (c *Transaction) ToTransactionInfoResponse(tagIds []int64) *TransactionInfo } } +// TransactionInfoResponseSlice represents the slice data structure of TransactionInfoResponse type TransactionInfoResponseSlice []*TransactionInfoResponse +// Len returns the count of items func (c TransactionInfoResponseSlice) Len() int { return len(c) } +// Swap swaps two items func (c TransactionInfoResponseSlice) Swap(i, j int) { c[i], c[j] = c[j], c[i] } +// Less reports whether the first item is less than the second one func (c TransactionInfoResponseSlice) Less(i, j int) bool { return c[i].Time < c[j].Time } diff --git a/pkg/models/transaction_category.go b/pkg/models/transaction_category.go index 10972525..14a0c871 100644 --- a/pkg/models/transaction_category.go +++ b/pkg/models/transaction_category.go @@ -3,14 +3,17 @@ package models // Level-One Transaction Category const TRANSACTION_PARENT_ID_LEVEL_ONE = 0 +// TransactionCategoryType represents transaction category type type TransactionCategoryType byte +// Transaction category types const ( CATEGORY_TYPE_INCOME TransactionCategoryType = 1 CATEGORY_TYPE_EXPENSE TransactionCategoryType = 2 CATEGORY_TYPE_TRANSFER TransactionCategoryType = 3 ) +// TransactionCategory represents transaction category data stored in database type TransactionCategory struct { CategoryId int64 `xorm:"PK"` Uid int64 `xorm:"INDEX(IDX_category_uid_deleted_type_parent_category_id_order) NOT NULL"` @@ -28,15 +31,18 @@ type TransactionCategory struct { DeletedUnixTime int64 } +// TransactionCategoryListRequest represents all parameters of transaction category listing request type TransactionCategoryListRequest struct { Type TransactionCategoryType `form:"type" binding:"min=0"` ParentId int64 `form:"parent_id,string,default=-1" binding:"min=-1"` } +// TransactionCategoryGetRequest represents all parameters of transaction category getting request type TransactionCategoryGetRequest struct { Id int64 `form:"id,string" binding:"required,min=1"` } +// TransactionCategoryCreateRequest represents all parameters of single transaction category creation request type TransactionCategoryCreateRequest struct { Name string `json:"name" binding:"required,notBlank,max=32"` Type TransactionCategoryType `json:"type" binding:"required"` @@ -46,10 +52,12 @@ type TransactionCategoryCreateRequest struct { Comment string `json:"comment" binding:"max=255"` } +// TransactionCategoryCreateBatchRequest represents all parameters of transaction category batch creation request type TransactionCategoryCreateBatchRequest struct { Categories []*TransactionCategoryCreateWithSubCategories `json:"categories" binding:"required"` } +// TransactionCategoryCreateWithSubCategories represents all parameters of multi transaction categories creation request type TransactionCategoryCreateWithSubCategories struct { Name string `json:"name" binding:"required,notBlank,max=32"` Type TransactionCategoryType `json:"type" binding:"required"` @@ -59,6 +67,7 @@ type TransactionCategoryCreateWithSubCategories struct { SubCategories []*TransactionCategoryCreateRequest `json:"subCategories" binding:"required"` } +// TransactionCategoryModifyRequest represents all parameters of transaction category modification request type TransactionCategoryModifyRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` Name string `json:"name" binding:"required,notBlank,max=32"` @@ -68,24 +77,29 @@ type TransactionCategoryModifyRequest struct { Hidden bool `json:"hidden"` } +// TransactionCategoryHideRequest represents all parameters of transaction category hiding request type TransactionCategoryHideRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` Hidden bool `json:"hidden"` } +// TransactionCategoryMoveRequest represents all parameters of transaction category moving request type TransactionCategoryMoveRequest struct { NewDisplayOrders []*TransactionCategoryNewDisplayOrderRequest `json:"newDisplayOrders"` } +// TransactionCategoryNewDisplayOrderRequest represents a data pair of id and display order type TransactionCategoryNewDisplayOrderRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` DisplayOrder int `json:"displayOrder"` } +// TransactionCategoryDeleteRequest represents all parameters of transaction category deleting request type TransactionCategoryDeleteRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` } +// TransactionCategoryInfoResponse represents a view-object of transaction category type TransactionCategoryInfoResponse struct { Id int64 `json:"id,string"` Name string `json:"name"` @@ -99,6 +113,7 @@ type TransactionCategoryInfoResponse struct { SubCategories TransactionCategoryInfoResponseSlice `json:"subCategories,omitempty"` } +// ToTransactionCategoryInfoResponse returns a view-object according to database model func (c *TransactionCategory) ToTransactionCategoryInfoResponse() *TransactionCategoryInfoResponse { return &TransactionCategoryInfoResponse{ Id: c.CategoryId, @@ -113,16 +128,20 @@ func (c *TransactionCategory) ToTransactionCategoryInfoResponse() *TransactionCa } } +// TransactionCategoryInfoResponseSlice represents the slice data structure of TransactionCategoryInfoResponse type TransactionCategoryInfoResponseSlice []*TransactionCategoryInfoResponse +// Len returns the count of items func (c TransactionCategoryInfoResponseSlice) Len() int { return len(c) } +// Swap swaps two items func (c TransactionCategoryInfoResponseSlice) Swap(i, j int) { c[i], c[j] = c[j], c[i] } +// Less reports whether the first item is less than the second one func (c TransactionCategoryInfoResponseSlice) Less(i, j int) bool { return c[i].DisplayOrder < c[j].DisplayOrder } diff --git a/pkg/models/transaction_tag.go b/pkg/models/transaction_tag.go index e5b4665a..3204bdcd 100644 --- a/pkg/models/transaction_tag.go +++ b/pkg/models/transaction_tag.go @@ -1,5 +1,6 @@ package models +// TransactionTag represents transaction tag data stored in database type TransactionTag struct { TagId int64 `xorm:"PK"` Uid int64 `xorm:"UNIQUE(UQE_tag_uid_name) NOT NULL"` @@ -10,37 +11,45 @@ type TransactionTag struct { UpdatedUnixTime int64 } +// TransactionTagGetRequest represents all parameters of transaction tag getting request type TransactionTagGetRequest struct { Id int64 `form:"id,string" binding:"required,min=1"` } +// TransactionTagCreateRequest represents all parameters of transaction tag creation request type TransactionTagCreateRequest struct { Name string `json:"name" binding:"required,notBlank,max=32"` } +// TransactionTagModifyRequest represents all parameters of transaction tag modification request type TransactionTagModifyRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` Name string `json:"name" binding:"required,notBlank,max=32"` } +// TransactionTagHideRequest represents all parameters of transaction tag hiding request type TransactionTagHideRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` Hidden bool `json:"hidden"` } +// TransactionTagMoveRequest represents all parameters of transaction tag moving request type TransactionTagMoveRequest struct { NewDisplayOrders []*TransactionTagNewDisplayOrderRequest `json:"newDisplayOrders"` } +// TransactionTagNewDisplayOrderRequest represents a data pair of id and display order type TransactionTagNewDisplayOrderRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` DisplayOrder int `json:"displayOrder"` } +// TransactionTagDeleteRequest represents all parameters of transaction tag deleting request type TransactionTagDeleteRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` } +// TransactionTagInfoResponse represents a view-object of transaction tag type TransactionTagInfoResponse struct { Id int64 `json:"id,string"` Name string `json:"name"` @@ -48,6 +57,7 @@ type TransactionTagInfoResponse struct { Hidden bool `json:"hidden"` } +// ToTransactionTagInfoResponse returns a view-object according to database model func (c *TransactionTag) ToTransactionTagInfoResponse() *TransactionTagInfoResponse { return &TransactionTagInfoResponse{ Id: c.TagId, @@ -57,16 +67,20 @@ func (c *TransactionTag) ToTransactionTagInfoResponse() *TransactionTagInfoRespo } } +// TransactionTagInfoResponseSlice represents the slice data structure of TransactionTagInfoResponse type TransactionTagInfoResponseSlice []*TransactionTagInfoResponse +// Len returns the count of items func (c TransactionTagInfoResponseSlice) Len() int { return len(c) } +// Swap swaps two items func (c TransactionTagInfoResponseSlice) Swap(i, j int) { c[i], c[j] = c[j], c[i] } +// Less reports whether the first item is less than the second one func (c TransactionTagInfoResponseSlice) Less(i, j int) bool { return c[i].DisplayOrder < c[j].DisplayOrder } diff --git a/pkg/models/transaction_tag_index.go b/pkg/models/transaction_tag_index.go index a3153506..d7a48e10 100644 --- a/pkg/models/transaction_tag_index.go +++ b/pkg/models/transaction_tag_index.go @@ -1,5 +1,6 @@ package models +// TransactionTagIndex represents transaction and transaction tag relation stored in database type TransactionTagIndex struct { Uid int64 `xorm:"PK INDEX(IDX_transaction_tag_index_uid_tag_id_transaction_time) INDEX(IDX_transaction_tag_index_uid_transaction_id)"` TagId int64 `xorm:"PK INDEX(IDX_transaction_tag_index_uid_tag_id_transaction_time)"` diff --git a/pkg/models/two_factor.go b/pkg/models/two_factor.go index 0922389c..473fecd9 100644 --- a/pkg/models/two_factor.go +++ b/pkg/models/two_factor.go @@ -1,38 +1,46 @@ package models +// TwoFactor represents user 2fa data stored in database type TwoFactor struct { Uid int64 `xorm:"PK"` Secret string `xorm:"VARCHAR(80) NOT NULL"` CreatedUnixTime int64 } +// TwoFactorLoginRequest represents all parameters of 2fa login request type TwoFactorLoginRequest struct { Passcode string `json:"passcode" binding:"required,notBlank,len=6"` } +// TwoFactorEnableConfirmRequest represents all parameters of 2fa confirm request type TwoFactorEnableConfirmRequest struct { Secret string `json:"secret" binding:"required,notBlank,len=32"` Passcode string `json:"passcode" binding:"required,notBlank,len=6"` } +// TwoFactorEnableResponse represents all response parameters when user requests to enable 2fa type TwoFactorEnableResponse struct { Secret string `json:"secret"` QRCode string `json:"qrcode"` } +// TwoFactorEnableConfirmResponse represents all response parameters after user have enabled 2fa type TwoFactorEnableConfirmResponse struct { Token string `json:"token,omitempty"` RecoveryCodes []string `json:"recoveryCodes"` } +// TwoFactorDisableRequest represents all parameters of 2fa disabling request type TwoFactorDisableRequest struct { Password string `json:"password" binding:"omitempty,min=6,max=128"` } +// TwoFactorRegenerateRecoveryCodeRequest represents all parameters of 2fa regenerating recovery codes request type TwoFactorRegenerateRecoveryCodeRequest struct { Password string `json:"password" binding:"omitempty,min=6,max=128"` } +// TwoFactorStatusResponse represents a view-object of 2fa status type TwoFactorStatusResponse struct { Enable bool `json:"enable"` CreatedAt int64 `json:"createdAt,omitempty"` diff --git a/pkg/models/two_factor_recovery_code.go b/pkg/models/two_factor_recovery_code.go index fea89303..4ddb9241 100644 --- a/pkg/models/two_factor_recovery_code.go +++ b/pkg/models/two_factor_recovery_code.go @@ -1,5 +1,6 @@ package models +// TwoFactorRecoveryCode represents user 2fa recovery codes stored in database type TwoFactorRecoveryCode struct { Uid int64 `xorm:"PK"` RecoveryCode string `xorm:"VARCHAR(64) PK"` @@ -8,6 +9,7 @@ type TwoFactorRecoveryCode struct { UsedUnixTime int64 } +// TwoFactorRecoveryCodeLoginRequest represents all parameters of 2fa login request via recovery code type TwoFactorRecoveryCodeLoginRequest struct { RecoveryCode string `json:"recoveryCode" binding:"required,notBlank,len=11"` } diff --git a/pkg/models/user.go b/pkg/models/user.go index 9e0e275e..c7415aba 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -1,13 +1,16 @@ package models +// UserType represents user type type UserType byte +// User types const ( USER_TYPE_NORMAL UserType = 0 USER_TYPE_ADMIN UserType = 63 USER_TYPE_SUPER_ADMIN UserType = 127 ) +// User represents user data stored in database type User struct { Uid int64 `xorm:"PK"` Username string `xorm:"VARCHAR(32) UNIQUE NOT NULL"` @@ -27,6 +30,7 @@ type User struct { LastLoginUnixTime int64 } +// UserBasicInfo represents a view-object of user basic info type UserBasicInfo struct { Username string `json:"username"` Email string `json:"email"` @@ -34,11 +38,13 @@ type UserBasicInfo struct { DefaultCurrency string `json:"defaultCurrency"` } +// UserLoginRequest represents all parameters of user login request type UserLoginRequest struct { LoginName string `json:"loginName" binding:"required,notBlank,max=100,validUsername|validEmail"` Password string `json:"password" binding:"required,min=6,max=128"` } +// 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"` @@ -47,6 +53,7 @@ type UserRegisterRequest struct { DefaultCurrency string `json:"defaultCurrency" binding:"required,len=3,validCurrency"` } +// UserProfileUpdateRequest represents all parameters of user updating profile request type UserProfileUpdateRequest struct { Email string `json:"email" binding:"omitempty,notBlank,max=100,validEmail"` Nickname string `json:"nickname" binding:"omitempty,notBlank,max=64"` @@ -55,11 +62,13 @@ type UserProfileUpdateRequest struct { DefaultCurrency string `json:"defaultCurrency" binding:"required,len=3,validCurrency"` } +// UserProfileUpdateResponse represents the data returns to frontend after updating profile type UserProfileUpdateResponse struct { User *UserBasicInfo `json:"user"` NewToken string `json:"newToken,omitempty"` } +// UserProfileResponse represents a view-object of user profile type UserProfileResponse struct { Username string `json:"username"` Email string `json:"email"` @@ -69,6 +78,7 @@ type UserProfileResponse struct { LastLoginAt int64 `json:"lastLoginAt"` } +// ToUserBasicInfo returns a user basic view-object according to database model func (u User) ToUserBasicInfo() *UserBasicInfo { return &UserBasicInfo{ Username: u.Username, @@ -78,6 +88,7 @@ func (u User) ToUserBasicInfo() *UserBasicInfo { } } +// ToUserProfileResponse returns a user profile view-object according to database model func (u User) ToUserProfileResponse() *UserProfileResponse { return &UserProfileResponse{ Username: u.Username,