1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00
wakapi/services/alias.go

55 lines
1.1 KiB
Go
Raw Normal View History

2019-07-06 18:53:20 +03:00
package services
import (
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/repositories"
2019-10-10 17:47:19 +03:00
"sync"
2019-07-06 18:53:20 +03:00
2020-03-31 13:22:17 +03:00
"github.com/muety/wakapi/models"
2019-07-06 18:53:20 +03:00
)
type AliasService struct {
config *config.Config
repository repositories.IAliasRepository
2019-07-06 18:53:20 +03:00
}
func NewAliasService(aliasRepo repositories.IAliasRepository) *AliasService {
return &AliasService{
config: config.Get(),
repository: aliasRepo,
}
}
2019-07-06 18:53:20 +03:00
var userAliases sync.Map
2020-02-20 16:28:55 +03:00
func (srv *AliasService) LoadUserAliases(userId string) error {
aliases, err := srv.repository.GetByUser(userId)
if err == nil {
userAliases.Store(userId, aliases)
2019-07-06 18:53:20 +03:00
}
return err
2019-07-06 18:53:20 +03:00
}
func (srv *AliasService) GetAliasOrDefault(userId string, summaryType uint8, value string) (string, error) {
if !srv.IsInitialized(userId) {
if err := srv.LoadUserAliases(userId); err != nil {
return "", err
2019-07-06 18:53:20 +03:00
}
}
aliases, _ := userAliases.Load(userId)
for _, a := range aliases.([]*models.Alias) {
if a.Type == summaryType && a.Value == value {
return a.Key, nil
}
}
return value, nil
2019-07-06 18:53:20 +03:00
}
func (srv *AliasService) IsInitialized(userId string) bool {
2019-10-10 17:47:19 +03:00
if _, ok := userAliases.Load(userId); ok {
2019-07-06 18:53:20 +03:00
return true
}
return false
}