mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(http): implement missing findByID methods (#1635)
* feat(http): implement missing methods * general cleanup * unify param handling * handle not found errors * unify err handlers * fix(http): fmt type
This commit is contained in:
parent
accc875960
commit
acb91e8709
15 changed files with 379 additions and 422 deletions
|
@ -29,14 +29,14 @@ type Service interface {
|
|||
type service struct {
|
||||
log zerolog.Logger
|
||||
repo domain.NotificationRepo
|
||||
senders []domain.NotificationSender
|
||||
senders map[int]domain.NotificationSender
|
||||
}
|
||||
|
||||
func NewService(log logger.Logger, repo domain.NotificationRepo) Service {
|
||||
s := &service{
|
||||
log: log.With().Str("module", "notification").Logger(),
|
||||
repo: repo,
|
||||
senders: []domain.NotificationSender{},
|
||||
senders: make(map[int]domain.NotificationSender),
|
||||
}
|
||||
|
||||
s.registerSenders()
|
||||
|
@ -45,53 +45,47 @@ func NewService(log logger.Logger, repo domain.NotificationRepo) Service {
|
|||
}
|
||||
|
||||
func (s *service) Find(ctx context.Context, params domain.NotificationQueryParams) ([]domain.Notification, int, error) {
|
||||
n, count, err := s.repo.Find(ctx, params)
|
||||
notifications, count, err := s.repo.Find(ctx, params)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("could not find notification with params: %+v", params)
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return n, count, err
|
||||
return notifications, count, err
|
||||
}
|
||||
|
||||
func (s *service) FindByID(ctx context.Context, id int) (*domain.Notification, error) {
|
||||
n, err := s.repo.FindByID(ctx, id)
|
||||
notification, err := s.repo.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("could not find notification by id: %v", id)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return n, err
|
||||
return notification, err
|
||||
}
|
||||
|
||||
func (s *service) Store(ctx context.Context, n domain.Notification) (*domain.Notification, error) {
|
||||
_, err := s.repo.Store(ctx, n)
|
||||
func (s *service) Store(ctx context.Context, notification domain.Notification) (*domain.Notification, error) {
|
||||
_, err := s.repo.Store(ctx, notification)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("could not store notification: %+v", n)
|
||||
s.log.Error().Err(err).Msgf("could not store notification: %+v", notification)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// reset senders
|
||||
s.senders = []domain.NotificationSender{}
|
||||
|
||||
// re register senders
|
||||
s.registerSenders()
|
||||
// register sender
|
||||
s.registerSender(notification)
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *service) Update(ctx context.Context, n domain.Notification) (*domain.Notification, error) {
|
||||
_, err := s.repo.Update(ctx, n)
|
||||
func (s *service) Update(ctx context.Context, notification domain.Notification) (*domain.Notification, error) {
|
||||
_, err := s.repo.Update(ctx, notification)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("could not update notification: %+v", n)
|
||||
s.log.Error().Err(err).Msgf("could not update notification: %+v", notification)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// reset senders
|
||||
s.senders = []domain.NotificationSender{}
|
||||
|
||||
// re register senders
|
||||
s.registerSenders()
|
||||
// register sender
|
||||
s.registerSender(notification)
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -103,42 +97,46 @@ func (s *service) Delete(ctx context.Context, id int) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// reset senders
|
||||
s.senders = []domain.NotificationSender{}
|
||||
|
||||
// re register senders
|
||||
s.registerSenders()
|
||||
// delete sender
|
||||
delete(s.senders, id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) registerSenders() {
|
||||
senders, err := s.repo.List(context.Background())
|
||||
notificationSenders, err := s.repo.List(context.Background())
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msg("could not find notifications")
|
||||
return
|
||||
}
|
||||
|
||||
for _, n := range senders {
|
||||
if n.Enabled {
|
||||
switch n.Type {
|
||||
case domain.NotificationTypeDiscord:
|
||||
s.senders = append(s.senders, NewDiscordSender(s.log, n))
|
||||
case domain.NotificationTypeGotify:
|
||||
s.senders = append(s.senders, NewGotifySender(s.log, n))
|
||||
case domain.NotificationTypeLunaSea:
|
||||
s.senders = append(s.senders, NewLunaSeaSender(s.log, n))
|
||||
case domain.NotificationTypeNotifiarr:
|
||||
s.senders = append(s.senders, NewNotifiarrSender(s.log, n))
|
||||
case domain.NotificationTypeNtfy:
|
||||
s.senders = append(s.senders, NewNtfySender(s.log, n))
|
||||
case domain.NotificationTypePushover:
|
||||
s.senders = append(s.senders, NewPushoverSender(s.log, n))
|
||||
case domain.NotificationTypeShoutrrr:
|
||||
s.senders = append(s.senders, NewShoutrrrSender(s.log, n))
|
||||
case domain.NotificationTypeTelegram:
|
||||
s.senders = append(s.senders, NewTelegramSender(s.log, n))
|
||||
}
|
||||
for _, notificationSender := range notificationSenders {
|
||||
s.registerSender(notificationSender)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// registerSender registers an enabled notification via it's id
|
||||
func (s *service) registerSender(notification domain.Notification) {
|
||||
if notification.Enabled {
|
||||
switch notification.Type {
|
||||
case domain.NotificationTypeDiscord:
|
||||
s.senders[notification.ID] = NewDiscordSender(s.log, notification)
|
||||
case domain.NotificationTypeGotify:
|
||||
s.senders[notification.ID] = NewGotifySender(s.log, notification)
|
||||
case domain.NotificationTypeLunaSea:
|
||||
s.senders[notification.ID] = NewLunaSeaSender(s.log, notification)
|
||||
case domain.NotificationTypeNotifiarr:
|
||||
s.senders[notification.ID] = NewNotifiarrSender(s.log, notification)
|
||||
case domain.NotificationTypeNtfy:
|
||||
s.senders[notification.ID] = NewNtfySender(s.log, notification)
|
||||
case domain.NotificationTypePushover:
|
||||
s.senders[notification.ID] = NewPushoverSender(s.log, notification)
|
||||
case domain.NotificationTypeShoutrrr:
|
||||
s.senders[notification.ID] = NewShoutrrrSender(s.log, notification)
|
||||
case domain.NotificationTypeTelegram:
|
||||
s.senders[notification.ID] = NewTelegramSender(s.log, notification)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue