mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-18 00:34:28 +08:00
add comments
This commit is contained in:
@@ -12,11 +12,13 @@ import (
|
|||||||
"github.com/mayswind/lab/pkg/uuid"
|
"github.com/mayswind/lab/pkg/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AccountService represents account service
|
||||||
type AccountService struct {
|
type AccountService struct {
|
||||||
ServiceUsingDB
|
ServiceUsingDB
|
||||||
ServiceUsingUuid
|
ServiceUsingUuid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize a account service singleton instance
|
||||||
var (
|
var (
|
||||||
Accounts = &AccountService{
|
Accounts = &AccountService{
|
||||||
ServiceUsingDB: ServiceUsingDB{
|
ServiceUsingDB: ServiceUsingDB{
|
||||||
@@ -28,6 +30,7 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetAllAccountsByUid returns all account models of user
|
||||||
func (s *AccountService) GetAllAccountsByUid(uid int64) ([]*models.Account, error) {
|
func (s *AccountService) GetAllAccountsByUid(uid int64) ([]*models.Account, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -39,6 +42,7 @@ func (s *AccountService) GetAllAccountsByUid(uid int64) ([]*models.Account, erro
|
|||||||
return accounts, err
|
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) {
|
func (s *AccountService) GetAccountAndSubAccountsByAccountId(uid int64, accountId int64) ([]*models.Account, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -54,6 +58,7 @@ func (s *AccountService) GetAccountAndSubAccountsByAccountId(uid int64, accountI
|
|||||||
return accounts, err
|
return accounts, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetMaxDisplayOrder returns the max display order according to account category
|
||||||
func (s *AccountService) GetMaxDisplayOrder(uid int64, category models.AccountCategory) (int, error) {
|
func (s *AccountService) GetMaxDisplayOrder(uid int64, category models.AccountCategory) (int, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return 0, errs.ErrUserIdInvalid
|
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) {
|
func (s *AccountService) GetMaxSubAccountDisplayOrder(uid int64, category models.AccountCategory, parentAccountId int64) (int, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return 0, errs.ErrUserIdInvalid
|
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 {
|
func (s *AccountService) CreateAccounts(mainAccount *models.Account, childrenAccounts []*models.Account) error {
|
||||||
if mainAccount.Uid <= 0 {
|
if mainAccount.Uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *AccountService) ModifyAccounts(uid int64, accounts []*models.Account) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *AccountService) HideAccount(uid int64, ids []int64, hidden bool) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *AccountService) ModifyAccountDisplayOrders(uid int64, accounts []*models.Account) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *AccountService) DeleteAccount(uid int64, accountId int64) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
return errs.ErrUserIdInvalid
|
||||||
|
|||||||
@@ -6,34 +6,42 @@ import (
|
|||||||
"github.com/mayswind/lab/pkg/uuid"
|
"github.com/mayswind/lab/pkg/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ServiceUsingDB represents a service that need to use db
|
||||||
type ServiceUsingDB struct {
|
type ServiceUsingDB struct {
|
||||||
container *datastore.DataStoreContainer
|
container *datastore.DataStoreContainer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UserDB returns the datastore which contains user
|
||||||
func (s *ServiceUsingDB) UserDB() *datastore.Database {
|
func (s *ServiceUsingDB) UserDB() *datastore.Database {
|
||||||
return s.container.UserStore.Choose(0)
|
return s.container.UserStore.Choose(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TokenDB returns the datastore which contains user token
|
||||||
func (s *ServiceUsingDB) TokenDB(uid int64) *datastore.Database {
|
func (s *ServiceUsingDB) TokenDB(uid int64) *datastore.Database {
|
||||||
return s.container.TokenStore.Choose(uid)
|
return s.container.TokenStore.Choose(uid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UserDataDB returns the datastore which contains user data
|
||||||
func (s *ServiceUsingDB) UserDataDB(uid int64) *datastore.Database {
|
func (s *ServiceUsingDB) UserDataDB(uid int64) *datastore.Database {
|
||||||
return s.container.UserDataStore.Choose(uid)
|
return s.container.UserDataStore.Choose(uid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServiceUsingConfig represents a service that need to use config
|
||||||
type ServiceUsingConfig struct {
|
type ServiceUsingConfig struct {
|
||||||
container *settings.ConfigContainer
|
container *settings.ConfigContainer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CurrentConfig returns the current config
|
||||||
func (s *ServiceUsingConfig) CurrentConfig() *settings.Config {
|
func (s *ServiceUsingConfig) CurrentConfig() *settings.Config {
|
||||||
return s.container.Current
|
return s.container.Current
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServiceUsingUuid represents a service that need to use uuid
|
||||||
type ServiceUsingUuid struct {
|
type ServiceUsingUuid struct {
|
||||||
container *uuid.UuidContainer
|
container *uuid.UuidContainer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateUuid generates a new uuid according to given uuid type
|
||||||
func (s *ServiceUsingUuid) GenerateUuid(uuidType uuid.UuidType) int64 {
|
func (s *ServiceUsingUuid) GenerateUuid(uuidType uuid.UuidType) int64 {
|
||||||
return s.container.GenerateUuid(uuidType)
|
return s.container.GenerateUuid(uuidType)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,11 +19,13 @@ import (
|
|||||||
"github.com/mayswind/lab/pkg/utils"
|
"github.com/mayswind/lab/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TokenService represents user token service
|
||||||
type TokenService struct {
|
type TokenService struct {
|
||||||
ServiceUsingDB
|
ServiceUsingDB
|
||||||
ServiceUsingConfig
|
ServiceUsingConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize a user token service singleton instance
|
||||||
var (
|
var (
|
||||||
Tokens = &TokenService{
|
Tokens = &TokenService{
|
||||||
ServiceUsingDB: ServiceUsingDB{
|
ServiceUsingDB: ServiceUsingDB{
|
||||||
@@ -35,6 +37,7 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetAllTokensByUid returns all token models of given user
|
||||||
func (s *TokenService) GetAllTokensByUid(uid int64) ([]*models.TokenRecord, error) {
|
func (s *TokenService) GetAllTokensByUid(uid int64) ([]*models.TokenRecord, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -46,6 +49,7 @@ func (s *TokenService) GetAllTokensByUid(uid int64) ([]*models.TokenRecord, erro
|
|||||||
return tokenRecords, err
|
return tokenRecords, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAllUnexpiredNormalTokensByUid returns all available token models of given user
|
||||||
func (s *TokenService) GetAllUnexpiredNormalTokensByUid(uid int64) ([]*models.TokenRecord, error) {
|
func (s *TokenService) GetAllUnexpiredNormalTokensByUid(uid int64) ([]*models.TokenRecord, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -59,6 +63,7 @@ func (s *TokenService) GetAllUnexpiredNormalTokensByUid(uid int64) ([]*models.To
|
|||||||
return tokenRecords, err
|
return tokenRecords, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseToken returns the token model according to request data
|
||||||
func (s *TokenService) ParseToken(c *core.Context) (*jwt.Token, *core.UserTokenClaims, error) {
|
func (s *TokenService) ParseToken(c *core.Context) (*jwt.Token, *core.UserTokenClaims, error) {
|
||||||
claims := &core.UserTokenClaims{}
|
claims := &core.UserTokenClaims{}
|
||||||
|
|
||||||
@@ -101,14 +106,17 @@ func (s *TokenService) ParseToken(c *core.Context) (*jwt.Token, *core.UserTokenC
|
|||||||
return token, claims, err
|
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) {
|
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)
|
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) {
|
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)
|
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 {
|
func (s *TokenService) DeleteToken(tokenRecord *models.TokenRecord) error {
|
||||||
if tokenRecord.Uid <= 0 {
|
if tokenRecord.Uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *TokenService) DeleteTokens(uid int64, tokenRecords []*models.TokenRecord) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *TokenService) DeleteTokenByClaims(claims *core.UserTokenClaims) error {
|
||||||
uid, err := utils.StringToInt64(claims.Id)
|
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})
|
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 {
|
func (s *TokenService) DeleteTokensBeforeTime(uid int64, expireTime int64) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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) {
|
func (s *TokenService) ParseFromTokenId(tokenId string) (*models.TokenRecord, error) {
|
||||||
pairs := strings.Split(tokenId, ":")
|
pairs := strings.Split(tokenId, ":")
|
||||||
|
|
||||||
@@ -213,6 +225,7 @@ func (s *TokenService) ParseFromTokenId(tokenId string) (*models.TokenRecord, er
|
|||||||
return tokenRecord, nil
|
return tokenRecord, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateTokenId generates token id according to token model
|
||||||
func (s *TokenService) GenerateTokenId(tokenRecord *models.TokenRecord) string {
|
func (s *TokenService) GenerateTokenId(tokenRecord *models.TokenRecord) string {
|
||||||
return fmt.Sprintf("%d:%d:%d", tokenRecord.Uid, tokenRecord.CreatedUnixTime, tokenRecord.UserTokenId)
|
return fmt.Sprintf("%d:%d:%d", tokenRecord.Uid, tokenRecord.CreatedUnixTime, tokenRecord.UserTokenId)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,11 +11,13 @@ import (
|
|||||||
"github.com/mayswind/lab/pkg/uuid"
|
"github.com/mayswind/lab/pkg/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TransactionCategoryService represents transaction category service
|
||||||
type TransactionCategoryService struct {
|
type TransactionCategoryService struct {
|
||||||
ServiceUsingDB
|
ServiceUsingDB
|
||||||
ServiceUsingUuid
|
ServiceUsingUuid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize a transaction category service singleton instance
|
||||||
var (
|
var (
|
||||||
TransactionCategories = &TransactionCategoryService{
|
TransactionCategories = &TransactionCategoryService{
|
||||||
ServiceUsingDB: ServiceUsingDB{
|
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) {
|
func (s *TransactionCategoryService) GetAllCategoriesByUid(uid int64, categoryType models.TransactionCategoryType, parentCategoryId int64) ([]*models.TransactionCategory, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -53,6 +56,7 @@ func (s *TransactionCategoryService) GetAllCategoriesByUid(uid int64, categoryTy
|
|||||||
return categories, err
|
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) {
|
func (s *TransactionCategoryService) GetCategoryByCategoryId(uid int64, categoryId int64) (*models.TransactionCategory, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -74,6 +78,7 @@ func (s *TransactionCategoryService) GetCategoryByCategoryId(uid int64, category
|
|||||||
return category, nil
|
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) {
|
func (s *TransactionCategoryService) GetMaxDisplayOrder(uid int64, categoryType models.TransactionCategoryType) (int, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return 0, errs.ErrUserIdInvalid
|
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) {
|
func (s *TransactionCategoryService) GetMaxSubCategoryDisplayOrder(uid int64, categoryType models.TransactionCategoryType, parentCategoryId int64) (int, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return 0, errs.ErrUserIdInvalid
|
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 {
|
func (s *TransactionCategoryService) CreateCategory(category *models.TransactionCategory) error {
|
||||||
if category.Uid <= 0 {
|
if category.Uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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) {
|
func (s *TransactionCategoryService) CreateCategories(uid int64, categories map[*models.TransactionCategory][]*models.TransactionCategory) ([]*models.TransactionCategory, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -186,6 +194,7 @@ func (s *TransactionCategoryService) CreateCategories(uid int64, categories map[
|
|||||||
return allCategories, nil
|
return allCategories, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ModifyCategory saves an existed transaction category model to database
|
||||||
func (s *TransactionCategoryService) ModifyCategory(category *models.TransactionCategory) error {
|
func (s *TransactionCategoryService) ModifyCategory(category *models.TransactionCategory) error {
|
||||||
if category.Uid <= 0 {
|
if category.Uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *TransactionCategoryService) HideCategory(uid int64, ids []int64, hidden bool) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *TransactionCategoryService) ModifyCategoryDisplayOrders(uid int64, categories []*models.TransactionCategory) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *TransactionCategoryService) DeleteCategory(uid int64, categoryId int64) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
return errs.ErrUserIdInvalid
|
||||||
|
|||||||
@@ -11,11 +11,13 @@ import (
|
|||||||
"github.com/mayswind/lab/pkg/uuid"
|
"github.com/mayswind/lab/pkg/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TransactionTagService represents transaction tag service
|
||||||
type TransactionTagService struct {
|
type TransactionTagService struct {
|
||||||
ServiceUsingDB
|
ServiceUsingDB
|
||||||
ServiceUsingUuid
|
ServiceUsingUuid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize a transaction tag service singleton instance
|
||||||
var (
|
var (
|
||||||
TransactionTags = &TransactionTagService{
|
TransactionTags = &TransactionTagService{
|
||||||
ServiceUsingDB: ServiceUsingDB{
|
ServiceUsingDB: ServiceUsingDB{
|
||||||
@@ -27,6 +29,7 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetAllTagsByUid returns all transaction tag models of user
|
||||||
func (s *TransactionTagService) GetAllTagsByUid(uid int64) ([]*models.TransactionTag, error) {
|
func (s *TransactionTagService) GetAllTagsByUid(uid int64) ([]*models.TransactionTag, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -38,6 +41,7 @@ func (s *TransactionTagService) GetAllTagsByUid(uid int64) ([]*models.Transactio
|
|||||||
return tags, err
|
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) {
|
func (s *TransactionTagService) GetTagByTagId(uid int64, tagId int64) (*models.TransactionTag, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -59,6 +63,7 @@ func (s *TransactionTagService) GetTagByTagId(uid int64, tagId int64) (*models.T
|
|||||||
return tag, nil
|
return tag, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetMaxDisplayOrder returns the max display order
|
||||||
func (s *TransactionTagService) GetMaxDisplayOrder(uid int64) (int, error) {
|
func (s *TransactionTagService) GetMaxDisplayOrder(uid int64) (int, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return 0, errs.ErrUserIdInvalid
|
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) {
|
func (s *TransactionTagService) GetAllTagIdsOfTransactions(uid int64, transactionIds []int64) (map[int64][]int64, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -104,6 +110,7 @@ func (s *TransactionTagService) GetAllTagIdsOfTransactions(uid int64, transactio
|
|||||||
return allTransactionTagIds, err
|
return allTransactionTagIds, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateTag saves a new transaction tag model to database
|
||||||
func (s *TransactionTagService) CreateTag(tag *models.TransactionTag) error {
|
func (s *TransactionTagService) CreateTag(tag *models.TransactionTag) error {
|
||||||
if tag.Uid <= 0 {
|
if tag.Uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *TransactionTagService) ModifyTag(tag *models.TransactionTag) error {
|
||||||
if tag.Uid <= 0 {
|
if tag.Uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *TransactionTagService) HideTag(uid int64, ids []int64, hidden bool) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *TransactionTagService) ModifyTagDisplayOrders(uid int64, tags []*models.TransactionTag) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *TransactionTagService) DeleteTag(uid int64, tagId int64) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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) {
|
func (s *TransactionTagService) ExistsTagName(uid int64, name string) (bool, error) {
|
||||||
if name == "" {
|
if name == "" {
|
||||||
return false, errs.ErrTransactionTagNameIsEmpty
|
return false, errs.ErrTransactionTagNameIsEmpty
|
||||||
|
|||||||
@@ -13,11 +13,13 @@ import (
|
|||||||
"github.com/mayswind/lab/pkg/uuid"
|
"github.com/mayswind/lab/pkg/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TransactionService represents transaction service
|
||||||
type TransactionService struct {
|
type TransactionService struct {
|
||||||
ServiceUsingDB
|
ServiceUsingDB
|
||||||
ServiceUsingUuid
|
ServiceUsingUuid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize a transaction service singleton instance
|
||||||
var (
|
var (
|
||||||
Transactions = &TransactionService{
|
Transactions = &TransactionService{
|
||||||
ServiceUsingDB: ServiceUsingDB{
|
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) {
|
func (s *TransactionService) GetTransactionsByMaxTime(uid int64, maxTime int64, count int) ([]*models.Transaction, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -44,6 +47,7 @@ func (s *TransactionService) GetTransactionsByMaxTime(uid int64, maxTime int64,
|
|||||||
return transactions, err
|
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) {
|
func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int, month int, page int, count int) ([]*models.Transaction, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -74,6 +78,7 @@ func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int, m
|
|||||||
return transactions, err
|
return transactions, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetTransactionByTransactionId returns a transaction model according to transaction id
|
||||||
func (s *TransactionService) GetTransactionByTransactionId(uid int64, transactionId int64) (*models.Transaction, error) {
|
func (s *TransactionService) GetTransactionByTransactionId(uid int64, transactionId int64) (*models.Transaction, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -95,6 +100,7 @@ func (s *TransactionService) GetTransactionByTransactionId(uid int64, transactio
|
|||||||
return transaction, nil
|
return transaction, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAllTransactionCount returns total count of transactions
|
||||||
func (s *TransactionService) GetAllTransactionCount(uid int64) (int64, error) {
|
func (s *TransactionService) GetAllTransactionCount(uid int64) (int64, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return 0, errs.ErrUserIdInvalid
|
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{})
|
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) {
|
func (s *TransactionService) GetMonthTransactionCount(uid int64, year int64, month int64) (int64, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return 0, errs.ErrUserIdInvalid
|
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{})
|
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 {
|
func (s *TransactionService) CreateTransaction(transaction *models.Transaction, tagIds []int64) error {
|
||||||
if transaction.Uid <= 0 {
|
if transaction.Uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *TransactionService) ModifyTransaction(transaction *models.Transaction, addTagIds []int64, removeTagIds []int64) error {
|
||||||
if transaction.Uid <= 0 {
|
if transaction.Uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
return errs.ErrUserIdInvalid
|
||||||
@@ -630,6 +639,7 @@ func (s *TransactionService) ModifyTransaction(transaction *models.Transaction,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteTransaction deletes an existed transaction from database
|
||||||
func (s *TransactionService) DeleteTransaction(uid int64, transactionId int64) error {
|
func (s *TransactionService) DeleteTransaction(uid int64, transactionId int64) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
return errs.ErrUserIdInvalid
|
||||||
|
|||||||
@@ -22,12 +22,14 @@ const (
|
|||||||
twoFactorRecoveryCodeLength int = 10 // bytes
|
twoFactorRecoveryCodeLength int = 10 // bytes
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TwoFactorAuthorizationService represents 2fa service
|
||||||
type TwoFactorAuthorizationService struct {
|
type TwoFactorAuthorizationService struct {
|
||||||
ServiceUsingDB
|
ServiceUsingDB
|
||||||
ServiceUsingConfig
|
ServiceUsingConfig
|
||||||
ServiceUsingUuid
|
ServiceUsingUuid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize a 2fa service singleton instance
|
||||||
var (
|
var (
|
||||||
TwoFactorAuthorizations = &TwoFactorAuthorizationService{
|
TwoFactorAuthorizations = &TwoFactorAuthorizationService{
|
||||||
ServiceUsingDB: ServiceUsingDB{
|
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) {
|
func (s *TwoFactorAuthorizationService) GetUserTwoFactorSettingByUid(uid int64) (*models.TwoFactor, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -65,6 +68,7 @@ func (s *TwoFactorAuthorizationService) GetUserTwoFactorSettingByUid(uid int64)
|
|||||||
return twoFactor, nil
|
return twoFactor, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateTwoFactorSecret generates a new 2fa secret
|
||||||
func (s *TwoFactorAuthorizationService) GenerateTwoFactorSecret(user *models.User) (*otp.Key, error) {
|
func (s *TwoFactorAuthorizationService) GenerateTwoFactorSecret(user *models.User) (*otp.Key, error) {
|
||||||
if user == nil {
|
if user == nil {
|
||||||
return nil, errs.ErrUserNotFound
|
return nil, errs.ErrUserNotFound
|
||||||
@@ -80,6 +84,7 @@ func (s *TwoFactorAuthorizationService) GenerateTwoFactorSecret(user *models.Use
|
|||||||
return key, err
|
return key, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateTwoFactorSetting saves a new 2fa setting to database
|
||||||
func (s *TwoFactorAuthorizationService) CreateTwoFactorSetting(twoFactor *models.TwoFactor) error {
|
func (s *TwoFactorAuthorizationService) CreateTwoFactorSetting(twoFactor *models.TwoFactor) error {
|
||||||
if twoFactor.Uid <= 0 {
|
if twoFactor.Uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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 {
|
func (s *TwoFactorAuthorizationService) DeleteTwoFactorSetting(uid int64) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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) {
|
func (s *TwoFactorAuthorizationService) ExistsTwoFactorSetting(uid int64) (bool, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return false, errs.ErrUserIdInvalid
|
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{})
|
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 {
|
func (s *TwoFactorAuthorizationService) GetAndUseUserTwoFactorRecoveryCode(uid int64, recoveryCode string, salt string) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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) {
|
func (s *TwoFactorAuthorizationService) GenerateTwoFactorRecoveryCodes() ([]string, error) {
|
||||||
recoveryCodes := make([]string, twoFactorRecoveryCodeCount)
|
recoveryCodes := make([]string, twoFactorRecoveryCodeCount)
|
||||||
|
|
||||||
@@ -162,6 +171,7 @@ func (s *TwoFactorAuthorizationService) GenerateTwoFactorRecoveryCodes() ([]stri
|
|||||||
return recoveryCodes, nil
|
return recoveryCodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateTwoFactorRecoveryCodes saves new 2fa recovery codes to database
|
||||||
func (s *TwoFactorAuthorizationService) CreateTwoFactorRecoveryCodes(uid int64, recoveryCodes []string, salt string) error {
|
func (s *TwoFactorAuthorizationService) CreateTwoFactorRecoveryCodes(uid int64, recoveryCodes []string, salt string) error {
|
||||||
twoFactorRecoveryCodes := make([]*models.TwoFactorRecoveryCode, len(recoveryCodes))
|
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 {
|
func (s *TwoFactorAuthorizationService) DeleteTwoFactorRecoveryCodes(uid int64) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
return errs.ErrUserIdInvalid
|
||||||
|
|||||||
@@ -12,11 +12,13 @@ import (
|
|||||||
"github.com/mayswind/lab/pkg/uuid"
|
"github.com/mayswind/lab/pkg/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// UserService represents user service
|
||||||
type UserService struct {
|
type UserService struct {
|
||||||
ServiceUsingDB
|
ServiceUsingDB
|
||||||
ServiceUsingUuid
|
ServiceUsingUuid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize a user service singleton instance
|
||||||
var (
|
var (
|
||||||
Users = &UserService{
|
Users = &UserService{
|
||||||
ServiceUsingDB: ServiceUsingDB{
|
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) {
|
func (s *UserService) GetUserByUsernameOrEmailAndPassword(loginname string, password string) (*models.User, error) {
|
||||||
var user *models.User
|
var user *models.User
|
||||||
var err error
|
var err error
|
||||||
@@ -51,6 +54,7 @@ func (s *UserService) GetUserByUsernameOrEmailAndPassword(loginname string, pass
|
|||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetUserById returns the user model according to user uid
|
||||||
func (s *UserService) GetUserById(uid int64) (*models.User, error) {
|
func (s *UserService) GetUserById(uid int64) (*models.User, error) {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return nil, errs.ErrUserIdInvalid
|
return nil, errs.ErrUserIdInvalid
|
||||||
@@ -68,6 +72,7 @@ func (s *UserService) GetUserById(uid int64) (*models.User, error) {
|
|||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetUserByUsername returns the user model according to user name
|
||||||
func (s *UserService) GetUserByUsername(username string) (*models.User, error) {
|
func (s *UserService) GetUserByUsername(username string) (*models.User, error) {
|
||||||
if username == "" {
|
if username == "" {
|
||||||
return nil, errs.ErrUsernameIsEmpty
|
return nil, errs.ErrUsernameIsEmpty
|
||||||
@@ -85,6 +90,7 @@ func (s *UserService) GetUserByUsername(username string) (*models.User, error) {
|
|||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetUserByEmail returns the user model according to user email
|
||||||
func (s *UserService) GetUserByEmail(email string) (*models.User, error) {
|
func (s *UserService) GetUserByEmail(email string) (*models.User, error) {
|
||||||
if email == "" {
|
if email == "" {
|
||||||
return nil, errs.ErrEmailIsEmpty
|
return nil, errs.ErrEmailIsEmpty
|
||||||
@@ -102,6 +108,7 @@ func (s *UserService) GetUserByEmail(email string) (*models.User, error) {
|
|||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateUser saves a new user model to database
|
||||||
func (s *UserService) CreateUser(user *models.User) error {
|
func (s *UserService) CreateUser(user *models.User) error {
|
||||||
exists, err := s.ExistsUsername(user.Username)
|
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) {
|
func (s *UserService) UpdateUser(user *models.User) (keyProfileUpdated bool, err error) {
|
||||||
if user.Uid <= 0 {
|
if user.Uid <= 0 {
|
||||||
return false, errs.ErrUserIdInvalid
|
return false, errs.ErrUserIdInvalid
|
||||||
@@ -208,6 +216,7 @@ func (s *UserService) UpdateUser(user *models.User) (keyProfileUpdated bool, err
|
|||||||
return keyProfileUpdated, nil
|
return keyProfileUpdated, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateUserLastLoginTime updates the last login time field
|
||||||
func (s *UserService) UpdateUserLastLoginTime(uid int64) error {
|
func (s *UserService) UpdateUserLastLoginTime(uid int64) error {
|
||||||
if uid <= 0 {
|
if uid <= 0 {
|
||||||
return errs.ErrUserIdInvalid
|
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) {
|
func (s *UserService) ExistsUsername(username string) (bool, error) {
|
||||||
if username == "" {
|
if username == "" {
|
||||||
return false, errs.ErrUsernameIsEmpty
|
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{})
|
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) {
|
func (s *UserService) ExistsEmail(email string) (bool, error) {
|
||||||
if email == "" {
|
if email == "" {
|
||||||
return false, errs.ErrEmailIsEmpty
|
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{})
|
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 {
|
func (s *UserService) IsPasswordEqualsUserPassword(password string, user *models.User) bool {
|
||||||
return user.Password == utils.EncodePassword(password, user.Salt)
|
return user.Password == utils.EncodePassword(password, user.Salt)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user