upda/server/service_secret.go
2024-04-20 19:10:17 +02:00

88 lines
1.6 KiB
Go

package server
import (
"errors"
"go.uber.org/zap"
)
type secretService struct {
repo secretRepository
}
func newSecretService(r secretRepository) *secretService {
return &secretService{
repo: r,
}
}
func (s *secretService) get(id string) (*Secret, error) {
if id == "" {
return nil, errorValidationNotBlank
}
return s.repo.findById(id)
}
func (s *secretService) getValueByKey(key string) (string, error) {
if key == "" {
return "", errorValidationNotBlank
}
var err error
var e *Secret
if e, err = s.repo.findByKey(key); err != nil {
return "", err
}
return e.Value, nil
}
func (s *secretService) getAll() ([]*Secret, error) {
return s.repo.findAll()
}
func (s *secretService) upsert(key string, value string) (*Secret, error) {
if key == "" || value == "" {
return nil, errorValidationNotBlank
}
var e *Secret
var err error
e, err = s.repo.findByKey(key)
if err != nil && !errors.Is(err, errorResourceNotFound) {
return nil, err
} else if err != nil && errors.Is(err, errorResourceNotFound) {
if e, err = s.repo.create(key, value); err != nil {
return nil, err
}
zap.L().Sugar().Infof("Created secret '%s'", e.Key)
} else {
if e, err = s.repo.update(e.ID.String(), value); err != nil {
return nil, err
}
zap.L().Sugar().Infof("Updated secret '%s'", e.Key)
}
return e, err
}
func (s *secretService) delete(id string) error {
if id == "" {
return errorValidationNotBlank
}
var err error
if _, err = s.get(id); err != nil {
return err
}
if _, err = s.repo.delete(id); err != nil {
return err
}
zap.L().Sugar().Infof("Deleted secret '%v'", id)
return nil
}