2020-11-01 22:14:10 +03:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2021-01-21 02:26:52 +03:00
|
|
|
"errors"
|
2020-11-01 22:14:10 +03:00
|
|
|
"github.com/muety/wakapi/config"
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"github.com/muety/wakapi/repositories"
|
|
|
|
"github.com/patrickmn/go-cache"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LanguageMappingService struct {
|
|
|
|
config *config.Config
|
|
|
|
cache *cache.Cache
|
2020-11-08 12:12:49 +03:00
|
|
|
repository repositories.ILanguageMappingRepository
|
2020-11-01 22:14:10 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:12:49 +03:00
|
|
|
func NewLanguageMappingService(languageMappingsRepo repositories.ILanguageMappingRepository) *LanguageMappingService {
|
2020-11-01 22:14:10 +03:00
|
|
|
return &LanguageMappingService{
|
|
|
|
config: config.Get(),
|
|
|
|
repository: languageMappingsRepo,
|
2021-01-21 02:26:52 +03:00
|
|
|
cache: cache.New(24*time.Hour, 24*time.Hour),
|
2020-11-01 22:14:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *LanguageMappingService) GetById(id uint) (*models.LanguageMapping, error) {
|
|
|
|
return srv.repository.GetById(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *LanguageMappingService) GetByUser(userId string) ([]*models.LanguageMapping, error) {
|
|
|
|
if mappings, found := srv.cache.Get(userId); found {
|
|
|
|
return mappings.([]*models.LanguageMapping), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mappings, err := srv.repository.GetByUser(userId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srv.cache.Set(userId, mappings, cache.DefaultExpiration)
|
|
|
|
return mappings, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *LanguageMappingService) ResolveByUser(userId string) (map[string]string, error) {
|
|
|
|
mappings := srv.getServerMappings()
|
|
|
|
userMappings, err := srv.GetByUser(userId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range userMappings {
|
|
|
|
mappings[m.Extension] = m.Language
|
|
|
|
}
|
|
|
|
return mappings, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *LanguageMappingService) Create(mapping *models.LanguageMapping) (*models.LanguageMapping, error) {
|
|
|
|
result, err := srv.repository.Insert(mapping)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
srv.cache.Delete(result.UserID)
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *LanguageMappingService) Delete(mapping *models.LanguageMapping) error {
|
2021-01-21 02:26:52 +03:00
|
|
|
if mapping.UserID == "" {
|
|
|
|
return errors.New("no user id specified")
|
|
|
|
}
|
2020-11-01 22:14:10 +03:00
|
|
|
err := srv.repository.Delete(mapping.ID)
|
|
|
|
srv.cache.Delete(mapping.UserID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-07 12:52:49 +03:00
|
|
|
func (srv *LanguageMappingService) getServerMappings() map[string]string {
|
|
|
|
// https://dave.cheney.net/2017/04/30/if-a-map-isnt-a-reference-variable-what-is-it
|
|
|
|
return srv.config.App.GetCustomLanguages()
|
2020-11-01 22:14:10 +03:00
|
|
|
}
|