autobrr/internal/download_client/service.go
Ludvig Lundgren 3185832708
feat: add postgres support (#215)
* feat: add postgres support and refactor

* feat: improve releases find

* fix: autobrrctl create user
2022-04-02 19:24:23 +02:00

73 lines
2 KiB
Go

package download_client
import (
"context"
"errors"
"github.com/autobrr/autobrr/internal/domain"
)
type Service interface {
List(ctx context.Context) ([]domain.DownloadClient, error)
FindByID(ctx context.Context, id int32) (*domain.DownloadClient, error)
Store(ctx context.Context, client domain.DownloadClient) (*domain.DownloadClient, error)
Update(ctx context.Context, client domain.DownloadClient) (*domain.DownloadClient, error)
Delete(ctx context.Context, clientID int) error
Test(client domain.DownloadClient) error
}
type service struct {
repo domain.DownloadClientRepo
}
func NewService(repo domain.DownloadClientRepo) Service {
return &service{repo: repo}
}
func (s *service) List(ctx context.Context) ([]domain.DownloadClient, error) {
return s.repo.List(ctx)
}
func (s *service) FindByID(ctx context.Context, id int32) (*domain.DownloadClient, error) {
return s.repo.FindByID(ctx, id)
}
func (s *service) Store(ctx context.Context, client domain.DownloadClient) (*domain.DownloadClient, error) {
// validate data
if client.Host == "" {
return nil, errors.New("validation error: no host")
} else if client.Type == "" {
return nil, errors.New("validation error: no type")
}
// store
return s.repo.Store(ctx, client)
}
func (s *service) Update(ctx context.Context, client domain.DownloadClient) (*domain.DownloadClient, error) {
// validate data
if client.Host == "" {
return nil, errors.New("validation error: no host")
} else if client.Type == "" {
return nil, errors.New("validation error: no type")
}
// store
return s.repo.Update(ctx, client)
}
func (s *service) Delete(ctx context.Context, clientID int) error {
return s.repo.Delete(ctx, clientID)
}
func (s *service) Test(client domain.DownloadClient) error {
// basic validation of client
if client.Host == "" {
return errors.New("validation error: no host")
} else if client.Type == "" {
return errors.New("validation error: no type")
}
// test
return s.testConnection(client)
}