dcbot/internal/service/group.go

72 lines
1.8 KiB
Go

package service
import (
"context"
"errors"
"fmt"
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
"gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp"
)
const (
maxGroupsPerServer = 10
)
//counterfeiter:generate -o internal/mock/group_repository.gen.go . GroupRepository
type GroupRepository interface {
Create(ctx context.Context, params domain.CreateGroupParams) (domain.Group, error)
List(ctx context.Context, params domain.ListGroupsParams) ([]domain.Group, error)
}
type Group struct {
repo GroupRepository
client TWHelpClient
}
func NewGroup(repo GroupRepository, client TWHelpClient) *Group {
return &Group{repo: repo, client: client}
}
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) >= maxGroupsPerServer {
return domain.Group{}, domain.GroupLimitReachedError{
Current: len(groups),
Limit: maxGroupsPerServer,
}
}
server, err := g.client.GetServer(ctx, params.VersionCode(), params.ServerKey())
if err != nil {
var apiErr twhelp.APIError
if !errors.As(err, &apiErr) {
return domain.Group{}, fmt.Errorf("TWHelpClient.GetServer: %w", err)
}
return domain.Group{}, domain.ServerDoesNotExistError{
VersionCode: params.VersionCode(),
Key: params.ServerKey(),
}
}
if !server.Open {
return domain.Group{}, domain.ServerIsClosedError{
VersionCode: params.VersionCode(),
Key: params.ServerKey(),
}
}
group, err := g.repo.Create(ctx, params)
if err != nil {
return domain.Group{}, fmt.Errorf("GroupRepository.Create: %w", err)
}
return group, nil
}