add comments
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<?", uid, false, startUnixTime, endUnixTime).Count(&models.Transaction{})
|
||||
}
|
||||
|
||||
// CreateTransaction saves a new transaction to database
|
||||
func (s *TransactionService) CreateTransaction(transaction *models.Transaction, tagIds []int64) error {
|
||||
if transaction.Uid <= 0 {
|
||||
return errs.ErrUserIdInvalid
|
||||
@@ -346,6 +354,7 @@ func (s *TransactionService) CreateTransaction(transaction *models.Transaction,
|
||||
})
|
||||
}
|
||||
|
||||
// ModifyTransaction saves an existed transaction to database
|
||||
func (s *TransactionService) ModifyTransaction(transaction *models.Transaction, addTagIds []int64, removeTagIds []int64) error {
|
||||
if transaction.Uid <= 0 {
|
||||
return errs.ErrUserIdInvalid
|
||||
@@ -630,6 +639,7 @@ func (s *TransactionService) ModifyTransaction(transaction *models.Transaction,
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteTransaction deletes an existed transaction from database
|
||||
func (s *TransactionService) DeleteTransaction(uid int64, transactionId int64) error {
|
||||
if uid <= 0 {
|
||||
return errs.ErrUserIdInvalid
|
||||
|
||||
@@ -22,12 +22,14 @@ const (
|
||||
twoFactorRecoveryCodeLength int = 10 // bytes
|
||||
)
|
||||
|
||||
// TwoFactorAuthorizationService represents 2fa service
|
||||
type TwoFactorAuthorizationService struct {
|
||||
ServiceUsingDB
|
||||
ServiceUsingConfig
|
||||
ServiceUsingUuid
|
||||
}
|
||||
|
||||
// Initialize a 2fa service singleton instance
|
||||
var (
|
||||
TwoFactorAuthorizations = &TwoFactorAuthorizationService{
|
||||
ServiceUsingDB: ServiceUsingDB{
|
||||
@@ -42,6 +44,7 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
// GetUserTwoFactorSettingByUid returns the 2fa setting model according to user uid
|
||||
func (s *TwoFactorAuthorizationService) GetUserTwoFactorSettingByUid(uid int64) (*models.TwoFactor, error) {
|
||||
if uid <= 0 {
|
||||
return nil, errs.ErrUserIdInvalid
|
||||
@@ -65,6 +68,7 @@ func (s *TwoFactorAuthorizationService) GetUserTwoFactorSettingByUid(uid int64)
|
||||
return twoFactor, nil
|
||||
}
|
||||
|
||||
// GenerateTwoFactorSecret generates a new 2fa secret
|
||||
func (s *TwoFactorAuthorizationService) GenerateTwoFactorSecret(user *models.User) (*otp.Key, error) {
|
||||
if user == nil {
|
||||
return nil, errs.ErrUserNotFound
|
||||
@@ -80,6 +84,7 @@ func (s *TwoFactorAuthorizationService) GenerateTwoFactorSecret(user *models.Use
|
||||
return key, err
|
||||
}
|
||||
|
||||
// CreateTwoFactorSetting saves a new 2fa setting to database
|
||||
func (s *TwoFactorAuthorizationService) CreateTwoFactorSetting(twoFactor *models.TwoFactor) error {
|
||||
if twoFactor.Uid <= 0 {
|
||||
return errs.ErrUserIdInvalid
|
||||
@@ -100,6 +105,7 @@ func (s *TwoFactorAuthorizationService) CreateTwoFactorSetting(twoFactor *models
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteTwoFactorSetting deletes an existed 2fa setting from database
|
||||
func (s *TwoFactorAuthorizationService) DeleteTwoFactorSetting(uid int64) error {
|
||||
if uid <= 0 {
|
||||
return errs.ErrUserIdInvalid
|
||||
@@ -118,6 +124,7 @@ func (s *TwoFactorAuthorizationService) DeleteTwoFactorSetting(uid int64) error
|
||||
})
|
||||
}
|
||||
|
||||
// ExistsTwoFactorSetting returns whether the given user has existed 2fa setting
|
||||
func (s *TwoFactorAuthorizationService) ExistsTwoFactorSetting(uid int64) (bool, error) {
|
||||
if uid <= 0 {
|
||||
return false, errs.ErrUserIdInvalid
|
||||
@@ -126,6 +133,7 @@ func (s *TwoFactorAuthorizationService) ExistsTwoFactorSetting(uid int64) (bool,
|
||||
return s.UserDB().Cols("uid").Where("uid=?", uid).Exist(&models.TwoFactor{})
|
||||
}
|
||||
|
||||
// GetAndUseUserTwoFactorRecoveryCode checks whether the given 2fa recovery code exists and marks it used
|
||||
func (s *TwoFactorAuthorizationService) GetAndUseUserTwoFactorRecoveryCode(uid int64, recoveryCode string, salt string) error {
|
||||
if uid <= 0 {
|
||||
return errs.ErrUserIdInvalid
|
||||
@@ -146,6 +154,7 @@ func (s *TwoFactorAuthorizationService) GetAndUseUserTwoFactorRecoveryCode(uid i
|
||||
})
|
||||
}
|
||||
|
||||
// GenerateTwoFactorRecoveryCodes generates new 2fa recovery codes
|
||||
func (s *TwoFactorAuthorizationService) GenerateTwoFactorRecoveryCodes() ([]string, error) {
|
||||
recoveryCodes := make([]string, twoFactorRecoveryCodeCount)
|
||||
|
||||
@@ -162,6 +171,7 @@ func (s *TwoFactorAuthorizationService) GenerateTwoFactorRecoveryCodes() ([]stri
|
||||
return recoveryCodes, nil
|
||||
}
|
||||
|
||||
// CreateTwoFactorRecoveryCodes saves new 2fa recovery codes to database
|
||||
func (s *TwoFactorAuthorizationService) CreateTwoFactorRecoveryCodes(uid int64, recoveryCodes []string, salt string) error {
|
||||
twoFactorRecoveryCodes := make([]*models.TwoFactorRecoveryCode, len(recoveryCodes))
|
||||
|
||||
@@ -194,6 +204,7 @@ func (s *TwoFactorAuthorizationService) CreateTwoFactorRecoveryCodes(uid int64,
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteTwoFactorRecoveryCodes deletes existed 2fa recovery codes from database
|
||||
func (s *TwoFactorAuthorizationService) DeleteTwoFactorRecoveryCodes(uid int64) error {
|
||||
if uid <= 0 {
|
||||
return errs.ErrUserIdInvalid
|
||||
|
||||
@@ -12,11 +12,13 @@ import (
|
||||
"github.com/mayswind/lab/pkg/uuid"
|
||||
)
|
||||
|
||||
// UserService represents user service
|
||||
type UserService struct {
|
||||
ServiceUsingDB
|
||||
ServiceUsingUuid
|
||||
}
|
||||
|
||||
// Initialize a user service singleton instance
|
||||
var (
|
||||
Users = &UserService{
|
||||
ServiceUsingDB: ServiceUsingDB{
|
||||
@@ -28,6 +30,7 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
// GetUserByUsernameOrEmailAndPassword returns the user model according to login name and password
|
||||
func (s *UserService) GetUserByUsernameOrEmailAndPassword(loginname string, password string) (*models.User, error) {
|
||||
var user *models.User
|
||||
var err error
|
||||
@@ -51,6 +54,7 @@ func (s *UserService) GetUserByUsernameOrEmailAndPassword(loginname string, pass
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// GetUserById returns the user model according to user uid
|
||||
func (s *UserService) GetUserById(uid int64) (*models.User, error) {
|
||||
if uid <= 0 {
|
||||
return nil, errs.ErrUserIdInvalid
|
||||
@@ -68,6 +72,7 @@ func (s *UserService) GetUserById(uid int64) (*models.User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// GetUserByUsername returns the user model according to user name
|
||||
func (s *UserService) GetUserByUsername(username string) (*models.User, error) {
|
||||
if username == "" {
|
||||
return nil, errs.ErrUsernameIsEmpty
|
||||
@@ -85,6 +90,7 @@ func (s *UserService) GetUserByUsername(username string) (*models.User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// GetUserByEmail returns the user model according to user email
|
||||
func (s *UserService) GetUserByEmail(email string) (*models.User, error) {
|
||||
if email == "" {
|
||||
return nil, errs.ErrEmailIsEmpty
|
||||
@@ -102,6 +108,7 @@ func (s *UserService) GetUserByEmail(email string) (*models.User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// CreateUser saves a new user model to database
|
||||
func (s *UserService) CreateUser(user *models.User) error {
|
||||
exists, err := s.ExistsUsername(user.Username)
|
||||
|
||||
@@ -146,6 +153,7 @@ func (s *UserService) CreateUser(user *models.User) error {
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateUser saves an existed user model to database
|
||||
func (s *UserService) UpdateUser(user *models.User) (keyProfileUpdated bool, err error) {
|
||||
if user.Uid <= 0 {
|
||||
return false, errs.ErrUserIdInvalid
|
||||
@@ -208,6 +216,7 @@ func (s *UserService) UpdateUser(user *models.User) (keyProfileUpdated bool, err
|
||||
return keyProfileUpdated, nil
|
||||
}
|
||||
|
||||
// UpdateUserLastLoginTime updates the last login time field
|
||||
func (s *UserService) UpdateUserLastLoginTime(uid int64) error {
|
||||
if uid <= 0 {
|
||||
return errs.ErrUserIdInvalid
|
||||
@@ -219,6 +228,7 @@ func (s *UserService) UpdateUserLastLoginTime(uid int64) error {
|
||||
})
|
||||
}
|
||||
|
||||
// ExistsUsername returns whether the given user name exists
|
||||
func (s *UserService) ExistsUsername(username string) (bool, error) {
|
||||
if username == "" {
|
||||
return false, errs.ErrUsernameIsEmpty
|
||||
@@ -227,6 +237,7 @@ func (s *UserService) ExistsUsername(username string) (bool, error) {
|
||||
return s.UserDB().Cols("username").Where("username=? AND deleted=?", username, false).Exist(&models.User{})
|
||||
}
|
||||
|
||||
// ExistsEmail returns whether the given user email exists
|
||||
func (s *UserService) ExistsEmail(email string) (bool, error) {
|
||||
if email == "" {
|
||||
return false, errs.ErrEmailIsEmpty
|
||||
@@ -235,6 +246,7 @@ func (s *UserService) ExistsEmail(email string) (bool, error) {
|
||||
return s.UserDB().Cols("email").Where("email=? AND deleted=?", email, false).Exist(&models.User{})
|
||||
}
|
||||
|
||||
// IsPasswordEqualsUserPassword returns whether the given password is correct
|
||||
func (s *UserService) IsPasswordEqualsUserPassword(password string, user *models.User) bool {
|
||||
return user.Password == utils.EncodePassword(password, user.Salt)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user