dcbot/internal/service/group.go
Dawid Wysokiński de19857b34
All checks were successful
continuous-integration/drone/push Build is passing
refactor: maxMonitorsPerGroup, maxGroupsPerServer -> ENVs (#27)
Reviewed-on: #27
2022-10-27 12:12:29 +00:00

133 lines
3.6 KiB
Go

package service
import (
"context"
"errors"
"fmt"
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
"gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp"
)
//counterfeiter:generate -o internal/mock/group_repository.gen.go . GroupRepository
type GroupRepository interface {
Create(ctx context.Context, params domain.CreateGroupParams) (domain.Group, error)
Update(ctx context.Context, id, serverID string, params domain.UpdateGroupParams) (domain.Group, error)
List(ctx context.Context, params domain.ListGroupsParams) ([]domain.Group, error)
Get(ctx context.Context, id, serverID string) (domain.Group, error)
Delete(ctx context.Context, id, serverID string) error
}
type Group struct {
repo GroupRepository
client TWHelpClient
maxGroupsPerServer int
}
func NewGroup(repo GroupRepository, client TWHelpClient, maxGroupsPerServer int) *Group {
return &Group{
repo: repo,
client: client,
maxGroupsPerServer: maxGroupsPerServer,
}
}
func (g *Group) Create(ctx context.Context, params domain.CreateGroupParams) (domain.Group, error) {
groups, err := g.repo.List(ctx, domain.ListGroupsParams{
ServerIDs: []string{params.ServerID()},
})
if err != nil {
return domain.Group{}, fmt.Errorf("GroupRepository.List: %w", err)
}
if len(groups) >= g.maxGroupsPerServer {
return domain.Group{}, domain.GroupLimitReachedError{
Current: len(groups),
Limit: g.maxGroupsPerServer,
}
}
if err = g.checkTWServer(ctx, params.VersionCode(), params.ServerKey()); err != nil {
return domain.Group{}, err
}
group, err := g.repo.Create(ctx, params)
if err != nil {
return domain.Group{}, fmt.Errorf("GroupRepository.Create: %w", err)
}
return group, nil
}
func (g *Group) SetChannelGains(ctx context.Context, id, serverID, channel string) (domain.Group, error) {
group, err := g.repo.Update(ctx, id, serverID, domain.UpdateGroupParams{
ChannelGains: domain.NullString{
String: channel,
Valid: true,
},
})
if err != nil {
return domain.Group{}, fmt.Errorf("GroupRepository.Update: %w", err)
}
return group, nil
}
func (g *Group) SetChannelLosses(ctx context.Context, id, serverID, channel string) (domain.Group, error) {
group, err := g.repo.Update(ctx, id, serverID, domain.UpdateGroupParams{
ChannelLosses: domain.NullString{
String: channel,
Valid: true,
},
})
if err != nil {
return domain.Group{}, fmt.Errorf("GroupRepository.Update: %w", err)
}
return group, nil
}
func (g *Group) List(ctx context.Context, params domain.ListGroupsParams) ([]domain.Group, error) {
groups, err := g.repo.List(ctx, params)
if err != nil {
return nil, fmt.Errorf("GroupRepository.List: %w", err)
}
return groups, nil
}
func (g *Group) Get(ctx context.Context, id, serverID string) (domain.Group, error) {
group, err := g.repo.Get(ctx, id, serverID)
if err != nil {
return domain.Group{}, fmt.Errorf("GroupRepository.Get: %w", err)
}
return group, nil
}
func (g *Group) Delete(ctx context.Context, id, serverID string) error {
if err := g.repo.Delete(ctx, id, serverID); err != nil {
return fmt.Errorf("GroupRepository.Delete: %w", err)
}
return nil
}
func (g *Group) checkTWServer(ctx context.Context, versionCode, serverKey string) error {
server, err := g.client.GetServer(ctx, versionCode, serverKey)
if err != nil {
var apiErr twhelp.APIError
if !errors.As(err, &apiErr) {
return fmt.Errorf("TWHelpClient.GetServer: %w", err)
}
return domain.ServerDoesNotExistError{
VersionCode: versionCode,
Key: serverKey,
}
}
if !server.Open {
return domain.ServerIsClosedError{
VersionCode: versionCode,
Key: serverKey,
}
}
return nil
}