From 76eac1c9dcb8a02e01ed45ed543c716d136ebb98 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sat, 26 Dec 2020 21:47:51 +0800 Subject: [PATCH] add comments --- pkg/services/accounts.go | 11 +++++++++++ pkg/services/base.go | 8 ++++++++ pkg/services/tokens.go | 13 +++++++++++++ pkg/services/transaction_categories.go | 12 ++++++++++++ pkg/services/transaction_tags.go | 12 ++++++++++++ pkg/services/transactions.go | 10 ++++++++++ pkg/services/twofactor_authorizations.go | 11 +++++++++++ pkg/services/users.go | 12 ++++++++++++ 8 files changed, 89 insertions(+) diff --git a/pkg/services/accounts.go b/pkg/services/accounts.go index 6b9ca560..c862ed6e 100644 --- a/pkg/services/accounts.go +++ b/pkg/services/accounts.go @@ -12,11 +12,13 @@ import ( "github.com/mayswind/lab/pkg/uuid" ) +// AccountService represents account service type AccountService struct { ServiceUsingDB ServiceUsingUuid } +// Initialize a account service singleton instance var ( Accounts = &AccountService{ ServiceUsingDB: ServiceUsingDB{ @@ -28,6 +30,7 @@ var ( } ) +// GetAllAccountsByUid returns all account models of user func (s *AccountService) GetAllAccountsByUid(uid int64) ([]*models.Account, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid @@ -39,6 +42,7 @@ func (s *AccountService) GetAllAccountsByUid(uid int64) ([]*models.Account, erro return accounts, err } +// GetAccountAndSubAccountsByAccountId returns account model and sub account models according to account id func (s *AccountService) GetAccountAndSubAccountsByAccountId(uid int64, accountId int64) ([]*models.Account, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid @@ -54,6 +58,7 @@ func (s *AccountService) GetAccountAndSubAccountsByAccountId(uid int64, accountI return accounts, err } +// GetMaxDisplayOrder returns the max display order according to account category func (s *AccountService) GetMaxDisplayOrder(uid int64, category models.AccountCategory) (int, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid @@ -73,6 +78,7 @@ func (s *AccountService) GetMaxDisplayOrder(uid int64, category models.AccountCa } } +// GetMaxSubAccountDisplayOrder returns the max display order of sub account according to account category and parent account id func (s *AccountService) GetMaxSubAccountDisplayOrder(uid int64, category models.AccountCategory, parentAccountId int64) (int, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid @@ -96,6 +102,7 @@ func (s *AccountService) GetMaxSubAccountDisplayOrder(uid int64, category models } } +// CreateAccounts saves a new account model to database func (s *AccountService) CreateAccounts(mainAccount *models.Account, childrenAccounts []*models.Account) error { if mainAccount.Uid <= 0 { return errs.ErrUserIdInvalid @@ -171,6 +178,7 @@ func (s *AccountService) CreateAccounts(mainAccount *models.Account, childrenAcc }) } +// ModifyAccounts saves an existed account model to database func (s *AccountService) ModifyAccounts(uid int64, accounts []*models.Account) error { if uid <= 0 { return errs.ErrUserIdInvalid @@ -198,6 +206,7 @@ func (s *AccountService) ModifyAccounts(uid int64, accounts []*models.Account) e }) } +// HideAccount updates hidden field of given accounts func (s *AccountService) HideAccount(uid int64, ids []int64, hidden bool) error { if uid <= 0 { return errs.ErrUserIdInvalid @@ -223,6 +232,7 @@ func (s *AccountService) HideAccount(uid int64, ids []int64, hidden bool) error }) } +// ModifyAccountDisplayOrders updates display order of given accounts func (s *AccountService) ModifyAccountDisplayOrders(uid int64, accounts []*models.Account) error { if uid <= 0 { return errs.ErrUserIdInvalid @@ -248,6 +258,7 @@ func (s *AccountService) ModifyAccountDisplayOrders(uid int64, accounts []*model }) } +// DeleteAccount deletes an existed account from database func (s *AccountService) DeleteAccount(uid int64, accountId int64) error { if uid <= 0 { return errs.ErrUserIdInvalid diff --git a/pkg/services/base.go b/pkg/services/base.go index 79ce892d..f754d3de 100644 --- a/pkg/services/base.go +++ b/pkg/services/base.go @@ -6,34 +6,42 @@ import ( "github.com/mayswind/lab/pkg/uuid" ) +// ServiceUsingDB represents a service that need to use db type ServiceUsingDB struct { container *datastore.DataStoreContainer } +// UserDB returns the datastore which contains user func (s *ServiceUsingDB) UserDB() *datastore.Database { return s.container.UserStore.Choose(0) } +// TokenDB returns the datastore which contains user token func (s *ServiceUsingDB) TokenDB(uid int64) *datastore.Database { return s.container.TokenStore.Choose(uid) } +// UserDataDB returns the datastore which contains user data func (s *ServiceUsingDB) UserDataDB(uid int64) *datastore.Database { return s.container.UserDataStore.Choose(uid) } +// ServiceUsingConfig represents a service that need to use config type ServiceUsingConfig struct { container *settings.ConfigContainer } +// CurrentConfig returns the current config func (s *ServiceUsingConfig) CurrentConfig() *settings.Config { return s.container.Current } +// ServiceUsingUuid represents a service that need to use uuid type ServiceUsingUuid struct { container *uuid.UuidContainer } +// GenerateUuid generates a new uuid according to given uuid type func (s *ServiceUsingUuid) GenerateUuid(uuidType uuid.UuidType) int64 { return s.container.GenerateUuid(uuidType) } diff --git a/pkg/services/tokens.go b/pkg/services/tokens.go index 60ee8649..dfea2195 100644 --- a/pkg/services/tokens.go +++ b/pkg/services/tokens.go @@ -19,11 +19,13 @@ import ( "github.com/mayswind/lab/pkg/utils" ) +// TokenService represents user token service type TokenService struct { ServiceUsingDB ServiceUsingConfig } +// Initialize a user token service singleton instance var ( Tokens = &TokenService{ ServiceUsingDB: ServiceUsingDB{ @@ -35,6 +37,7 @@ var ( } ) +// GetAllTokensByUid returns all token models of given user func (s *TokenService) GetAllTokensByUid(uid int64) ([]*models.TokenRecord, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid @@ -46,6 +49,7 @@ func (s *TokenService) GetAllTokensByUid(uid int64) ([]*models.TokenRecord, erro return tokenRecords, err } +// GetAllUnexpiredNormalTokensByUid returns all available token models of given user func (s *TokenService) GetAllUnexpiredNormalTokensByUid(uid int64) ([]*models.TokenRecord, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid @@ -59,6 +63,7 @@ func (s *TokenService) GetAllUnexpiredNormalTokensByUid(uid int64) ([]*models.To return tokenRecords, err } +// ParseToken returns the token model according to request data func (s *TokenService) ParseToken(c *core.Context) (*jwt.Token, *core.UserTokenClaims, error) { claims := &core.UserTokenClaims{} @@ -101,14 +106,17 @@ func (s *TokenService) ParseToken(c *core.Context) (*jwt.Token, *core.UserTokenC return token, claims, err } +// CreateToken generates a new normal token and saves to database func (s *TokenService) CreateToken(user *models.User, ctx *core.Context) (string, *core.UserTokenClaims, error) { return s.createToken(user, core.USER_TOKEN_TYPE_NORMAL, s.getUserAgent(ctx), s.CurrentConfig().TokenExpiredTimeDuration) } +// CreateRequire2FAToken generates a new token requiring user to verify 2fa passcode and saves to database func (s *TokenService) CreateRequire2FAToken(user *models.User, ctx *core.Context) (string, *core.UserTokenClaims, error) { return s.createToken(user, core.USER_TOKEN_TYPE_REQUIRE_2FA, s.getUserAgent(ctx), s.CurrentConfig().TemporaryTokenExpiredTimeDuration) } +// DeleteToken deletes given token from database func (s *TokenService) DeleteToken(tokenRecord *models.TokenRecord) error { if tokenRecord.Uid <= 0 { return errs.ErrUserIdInvalid @@ -131,6 +139,7 @@ func (s *TokenService) DeleteToken(tokenRecord *models.TokenRecord) error { }) } +// DeleteTokens deletes given tokens from database func (s *TokenService) DeleteTokens(uid int64, tokenRecords []*models.TokenRecord) error { if uid <= 0 { return errs.ErrUserIdInvalid @@ -152,6 +161,7 @@ func (s *TokenService) DeleteTokens(uid int64, tokenRecords []*models.TokenRecor }) } +// DeleteTokenByClaims deletes given token from database func (s *TokenService) DeleteTokenByClaims(claims *core.UserTokenClaims) error { uid, err := utils.StringToInt64(claims.Id) @@ -168,6 +178,7 @@ func (s *TokenService) DeleteTokenByClaims(claims *core.UserTokenClaims) error { return s.DeleteToken(&models.TokenRecord{Uid: uid, UserTokenId: userTokenId, CreatedUnixTime: claims.IssuedAt}) } +// DeleteTokensBeforeTime deletes tokens that is created before specific tim func (s *TokenService) DeleteTokensBeforeTime(uid int64, expireTime int64) error { if uid <= 0 { return errs.ErrUserIdInvalid @@ -179,6 +190,7 @@ func (s *TokenService) DeleteTokensBeforeTime(uid int64, expireTime int64) error }) } +// ParseFromTokenId returns token model according to token id func (s *TokenService) ParseFromTokenId(tokenId string) (*models.TokenRecord, error) { pairs := strings.Split(tokenId, ":") @@ -213,6 +225,7 @@ func (s *TokenService) ParseFromTokenId(tokenId string) (*models.TokenRecord, er return tokenRecord, nil } +// GenerateTokenId generates token id according to token model func (s *TokenService) GenerateTokenId(tokenRecord *models.TokenRecord) string { return fmt.Sprintf("%d:%d:%d", tokenRecord.Uid, tokenRecord.CreatedUnixTime, tokenRecord.UserTokenId) } diff --git a/pkg/services/transaction_categories.go b/pkg/services/transaction_categories.go index fa8b74be..f450594e 100644 --- a/pkg/services/transaction_categories.go +++ b/pkg/services/transaction_categories.go @@ -11,11 +11,13 @@ import ( "github.com/mayswind/lab/pkg/uuid" ) +// TransactionCategoryService represents transaction category service type TransactionCategoryService struct { ServiceUsingDB ServiceUsingUuid } +// Initialize a transaction category service singleton instance var ( TransactionCategories = &TransactionCategoryService{ ServiceUsingDB: ServiceUsingDB{ @@ -27,6 +29,7 @@ var ( } ) +// GetAllCategoriesByUid returns all transaction category models of user func (s *TransactionCategoryService) GetAllCategoriesByUid(uid int64, categoryType models.TransactionCategoryType, parentCategoryId int64) ([]*models.TransactionCategory, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid @@ -53,6 +56,7 @@ func (s *TransactionCategoryService) GetAllCategoriesByUid(uid int64, categoryTy return categories, err } +// GetCategoryByCategoryId returns a transaction category model according to transaction category id func (s *TransactionCategoryService) GetCategoryByCategoryId(uid int64, categoryId int64) (*models.TransactionCategory, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid @@ -74,6 +78,7 @@ func (s *TransactionCategoryService) GetCategoryByCategoryId(uid int64, category return category, nil } +// GetMaxDisplayOrder returns the max display order according to transaction category type func (s *TransactionCategoryService) GetMaxDisplayOrder(uid int64, categoryType models.TransactionCategoryType) (int, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid @@ -93,6 +98,7 @@ func (s *TransactionCategoryService) GetMaxDisplayOrder(uid int64, categoryType } } +// GetMaxSubCategoryDisplayOrder returns the max display order of sub transaction category according to transaction category type and parent transaction category id func (s *TransactionCategoryService) GetMaxSubCategoryDisplayOrder(uid int64, categoryType models.TransactionCategoryType, parentCategoryId int64) (int, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid @@ -116,6 +122,7 @@ func (s *TransactionCategoryService) GetMaxSubCategoryDisplayOrder(uid int64, ca } } +// CreateCategory saves a new transaction category model to database func (s *TransactionCategoryService) CreateCategory(category *models.TransactionCategory) error { if category.Uid <= 0 { return errs.ErrUserIdInvalid @@ -133,6 +140,7 @@ func (s *TransactionCategoryService) CreateCategory(category *models.Transaction }) } +// CreateCategories saves a few transaction category models to database func (s *TransactionCategoryService) CreateCategories(uid int64, categories map[*models.TransactionCategory][]*models.TransactionCategory) ([]*models.TransactionCategory, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid @@ -186,6 +194,7 @@ func (s *TransactionCategoryService) CreateCategories(uid int64, categories map[ return allCategories, nil } +// ModifyCategory saves an existed transaction category model to database func (s *TransactionCategoryService) ModifyCategory(category *models.TransactionCategory) error { if category.Uid <= 0 { return errs.ErrUserIdInvalid @@ -206,6 +215,7 @@ func (s *TransactionCategoryService) ModifyCategory(category *models.Transaction }) } +// HideCategory updates hidden field of given transaction categories func (s *TransactionCategoryService) HideCategory(uid int64, ids []int64, hidden bool) error { if uid <= 0 { return errs.ErrUserIdInvalid @@ -231,6 +241,7 @@ func (s *TransactionCategoryService) HideCategory(uid int64, ids []int64, hidden }) } +// ModifyCategoryDisplayOrders updates display order of given transaction categories func (s *TransactionCategoryService) ModifyCategoryDisplayOrders(uid int64, categories []*models.TransactionCategory) error { if uid <= 0 { return errs.ErrUserIdInvalid @@ -256,6 +267,7 @@ func (s *TransactionCategoryService) ModifyCategoryDisplayOrders(uid int64, cate }) } +// DeleteCategory deletes an existed transaction category from database func (s *TransactionCategoryService) DeleteCategory(uid int64, categoryId int64) error { if uid <= 0 { return errs.ErrUserIdInvalid diff --git a/pkg/services/transaction_tags.go b/pkg/services/transaction_tags.go index c8514256..da66f684 100644 --- a/pkg/services/transaction_tags.go +++ b/pkg/services/transaction_tags.go @@ -11,11 +11,13 @@ import ( "github.com/mayswind/lab/pkg/uuid" ) +// TransactionTagService represents transaction tag service type TransactionTagService struct { ServiceUsingDB ServiceUsingUuid } +// Initialize a transaction tag service singleton instance var ( TransactionTags = &TransactionTagService{ ServiceUsingDB: ServiceUsingDB{ @@ -27,6 +29,7 @@ var ( } ) +// GetAllTagsByUid returns all transaction tag models of user func (s *TransactionTagService) GetAllTagsByUid(uid int64) ([]*models.TransactionTag, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid @@ -38,6 +41,7 @@ func (s *TransactionTagService) GetAllTagsByUid(uid int64) ([]*models.Transactio return tags, err } +// GetTagByTagId returns a transaction tag model according to transaction tag id func (s *TransactionTagService) GetTagByTagId(uid int64, tagId int64) (*models.TransactionTag, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid @@ -59,6 +63,7 @@ func (s *TransactionTagService) GetTagByTagId(uid int64, tagId int64) (*models.T return tag, nil } +// GetMaxDisplayOrder returns the max display order func (s *TransactionTagService) GetMaxDisplayOrder(uid int64) (int, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid @@ -78,6 +83,7 @@ func (s *TransactionTagService) GetMaxDisplayOrder(uid int64) (int, error) { } } +// GetAllTagIdsOfTransactions returns transaction tag ids for given transactions func (s *TransactionTagService) GetAllTagIdsOfTransactions(uid int64, transactionIds []int64) (map[int64][]int64, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid @@ -104,6 +110,7 @@ func (s *TransactionTagService) GetAllTagIdsOfTransactions(uid int64, transactio return allTransactionTagIds, err } +// CreateTag saves a new transaction tag model to database func (s *TransactionTagService) CreateTag(tag *models.TransactionTag) error { if tag.Uid <= 0 { return errs.ErrUserIdInvalid @@ -128,6 +135,7 @@ func (s *TransactionTagService) CreateTag(tag *models.TransactionTag) error { }) } +// ModifyTag saves an existed transaction tag model to database func (s *TransactionTagService) ModifyTag(tag *models.TransactionTag) error { if tag.Uid <= 0 { return errs.ErrUserIdInvalid @@ -156,6 +164,7 @@ func (s *TransactionTagService) ModifyTag(tag *models.TransactionTag) error { }) } +// HideTag updates hidden field of given transaction tags func (s *TransactionTagService) HideTag(uid int64, ids []int64, hidden bool) error { if uid <= 0 { return errs.ErrUserIdInvalid @@ -181,6 +190,7 @@ func (s *TransactionTagService) HideTag(uid int64, ids []int64, hidden bool) err }) } +// ModifyTagDisplayOrders updates display order of given transaction tags func (s *TransactionTagService) ModifyTagDisplayOrders(uid int64, tags []*models.TransactionTag) error { if uid <= 0 { return errs.ErrUserIdInvalid @@ -206,6 +216,7 @@ func (s *TransactionTagService) ModifyTagDisplayOrders(uid int64, tags []*models }) } +// DeleteTag deletes an existed transaction tag from database func (s *TransactionTagService) DeleteTag(uid int64, tagId int64) error { if uid <= 0 { return errs.ErrUserIdInvalid @@ -232,6 +243,7 @@ func (s *TransactionTagService) DeleteTag(uid int64, tagId int64) error { }) } +// ExistsTagName returns whether the given tag name exists func (s *TransactionTagService) ExistsTagName(uid int64, name string) (bool, error) { if name == "" { return false, errs.ErrTransactionTagNameIsEmpty diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index 98f6e3ff..d9b67478 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -13,11 +13,13 @@ import ( "github.com/mayswind/lab/pkg/uuid" ) +// TransactionService represents transaction service type TransactionService struct { ServiceUsingDB ServiceUsingUuid } +// Initialize a transaction service singleton instance var ( Transactions = &TransactionService{ ServiceUsingDB: ServiceUsingDB{ @@ -29,6 +31,7 @@ var ( } ) +// GetTransactionsByMaxTime returns transactions before given time func (s *TransactionService) GetTransactionsByMaxTime(uid int64, maxTime int64, count int) ([]*models.Transaction, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid @@ -44,6 +47,7 @@ func (s *TransactionService) GetTransactionsByMaxTime(uid int64, maxTime int64, return transactions, err } +// GetTransactionsInMonthByPage returns transactions in given year and month func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int, month int, page int, count int) ([]*models.Transaction, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid @@ -74,6 +78,7 @@ func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int, m return transactions, err } +// GetTransactionByTransactionId returns a transaction model according to transaction id func (s *TransactionService) GetTransactionByTransactionId(uid int64, transactionId int64) (*models.Transaction, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid @@ -95,6 +100,7 @@ func (s *TransactionService) GetTransactionByTransactionId(uid int64, transactio return transaction, nil } +// GetAllTransactionCount returns total count of transactions func (s *TransactionService) GetAllTransactionCount(uid int64) (int64, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid @@ -103,6 +109,7 @@ func (s *TransactionService) GetAllTransactionCount(uid int64) (int64, error) { return s.UserDataDB(uid).Where("uid=? AND deleted=?", uid, false).Count(&models.Transaction{}) } +// GetMonthTransactionCount returns total count of transactions in given year and month func (s *TransactionService) GetMonthTransactionCount(uid int64, year int64, month int64) (int64, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid @@ -122,6 +129,7 @@ func (s *TransactionService) GetMonthTransactionCount(uid int64, year int64, mon return s.UserDataDB(uid).Where("uid=? AND deleted=? AND transaction_time>=? AND transaction_time