add comments, code refactor

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