2020-05-30 21:41:27 +03:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2020-09-29 19:55:07 +03:00
|
|
|
"github.com/muety/wakapi/config"
|
2020-05-30 21:41:27 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2020-11-01 18:56:36 +03:00
|
|
|
"github.com/muety/wakapi/repositories"
|
2020-05-30 21:41:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type KeyValueService struct {
|
2020-11-01 18:56:36 +03:00
|
|
|
config *config.Config
|
2020-11-08 12:12:49 +03:00
|
|
|
repository repositories.IKeyValueRepository
|
2020-05-30 21:41:27 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:12:49 +03:00
|
|
|
func NewKeyValueService(keyValueRepo repositories.IKeyValueRepository) *KeyValueService {
|
2020-05-30 21:41:27 +03:00
|
|
|
return &KeyValueService{
|
2020-11-01 18:56:36 +03:00
|
|
|
config: config.Get(),
|
|
|
|
repository: keyValueRepo,
|
2020-05-30 21:41:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *KeyValueService) GetString(key string) (*models.KeyStringValue, error) {
|
2020-11-01 18:56:36 +03:00
|
|
|
return srv.repository.GetString(key)
|
2020-05-30 21:41:27 +03:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:41:32 +03:00
|
|
|
func (srv *KeyValueService) GetByPrefix(prefix string) ([]*models.KeyStringValue, error) {
|
|
|
|
return srv.repository.Search(prefix + "%")
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
func (srv *KeyValueService) MustGetString(key string) *models.KeyStringValue {
|
|
|
|
kv, err := srv.repository.GetString(key)
|
|
|
|
if err != nil {
|
|
|
|
return &models.KeyStringValue{
|
|
|
|
Key: key,
|
|
|
|
Value: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return kv
|
|
|
|
}
|
|
|
|
|
2020-05-30 21:41:27 +03:00
|
|
|
func (srv *KeyValueService) PutString(kv *models.KeyStringValue) error {
|
2020-11-01 18:56:36 +03:00
|
|
|
return srv.repository.PutString(kv)
|
2020-05-30 21:41:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *KeyValueService) DeleteString(key string) error {
|
2020-11-01 18:56:36 +03:00
|
|
|
return srv.repository.DeleteString(key)
|
2020-05-30 21:41:27 +03:00
|
|
|
}
|