feat: add a new command - /group monitor add

This commit is contained in:
Dawid Wysokiński 2022-10-22 17:19:49 +02:00
parent f02db1fc44
commit bf531bd293
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
5 changed files with 203 additions and 61 deletions

View File

@ -79,12 +79,35 @@ func (g *Group) List(ctx context.Context, params domain.ListGroupsParams) ([]dom
}
result := make([]domain.Group, 0, len(groups))
for _, version := range groups {
result = append(result, version.ToDomain())
for _, group := range groups {
result = append(result, group.ToDomain())
}
return result, nil
}
func (g *Group) Get(ctx context.Context, id, serverID string) (domain.Group, error) {
if _, err := uuid.Parse(id); err != nil {
return domain.Group{}, domain.GroupNotFoundError{ID: id}
}
var group model.Group
err := g.db.NewSelect().
Model(&group).
Where("id = ?", id).
Where("server_id = ?", serverID).
Scan(ctx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return domain.Group{}, domain.GroupNotFoundError{ID: id}
}
return domain.Group{}, fmt.Errorf("couldn't select group (id=%s,serverID=%s) from the db: %w", id, serverID, err)
}
return group.ToDomain(), nil
}
func (g *Group) Delete(ctx context.Context, id, serverID string) error {
if _, err := uuid.Parse(id); err != nil {
return domain.GroupNotFoundError{ID: id}

View File

@ -80,45 +80,47 @@ func TestGroup_Update(t *testing.T) {
assert.Zero(t, updatedGroup)
})
t.Run("ERR: invalid UUID", func(t *testing.T) {
t.Run("ERR: group not found", func(t *testing.T) {
t.Parallel()
id := "12345"
updatedGroup, err := repo.Update(context.Background(), id, "", domain.UpdateGroupParams{
ChannelGains: domain.NullString{
String: "update",
Valid: true,
tests := []struct {
name string
id string
serverID string
}{
{
name: "ID - not UUID",
id: "test",
serverID: group.ServerID,
},
})
assert.ErrorIs(t, err, domain.GroupNotFoundError{ID: id})
assert.Zero(t, updatedGroup)
})
t.Run("ERR: group not found (unknown ID)", func(t *testing.T) {
t.Parallel()
id := uuid.NewString()
updatedGroup, err := repo.Update(context.Background(), id, group.ServerID, domain.UpdateGroupParams{
ChannelGains: domain.NullString{
String: "update",
Valid: true,
{
name: "ID - random UUID",
id: uuid.NewString(),
serverID: group.ServerID,
},
})
assert.ErrorIs(t, err, domain.GroupNotFoundError{ID: id})
assert.Zero(t, updatedGroup)
})
t.Run("ERR: group not found (unknown ServerID)", func(t *testing.T) {
t.Parallel()
updatedGroup, err := repo.Update(context.Background(), group.ID.String(), uuid.NewString(), domain.UpdateGroupParams{
ChannelGains: domain.NullString{
String: "update",
Valid: true,
{
name: "ServerID - random UUID",
id: group.ID.String(),
serverID: uuid.NewString(),
},
})
assert.ErrorIs(t, err, domain.GroupNotFoundError{ID: group.ID.String()})
assert.Zero(t, updatedGroup)
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
updatedGroup, err := repo.Update(context.Background(), tt.id, tt.serverID, domain.UpdateGroupParams{
ChannelGains: domain.NullString{
String: "update",
Valid: true,
},
})
assert.ErrorIs(t, err, domain.GroupNotFoundError{ID: tt.id})
assert.Zero(t, updatedGroup)
})
}
})
}
@ -180,6 +182,58 @@ func TestGroup_List(t *testing.T) {
}
}
func TestGroup_Get(t *testing.T) {
t.Parallel()
db := newDB(t)
fixture := loadFixtures(t, db)
repo := bundb.NewGroup(db)
group := getGroupFromFixture(t, fixture, "group-server-1-1")
t.Run("OK", func(t *testing.T) {
t.Parallel()
})
t.Run("ERR: group not found (unknown ID)", func(t *testing.T) {
t.Parallel()
tests := []struct {
name string
id string
serverID string
}{
{
name: "ID - not UUID",
id: "test",
serverID: group.ServerID,
},
{
name: "ID - random UUID",
id: uuid.NewString(),
serverID: group.ServerID,
},
{
name: "ServerID - random UUID",
id: group.ID.String(),
serverID: uuid.NewString(),
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
g, err := repo.Get(context.Background(), tt.id, tt.serverID)
assert.ErrorIs(t, err, domain.GroupNotFoundError{ID: tt.id})
assert.Zero(t, g)
})
}
})
}
func TestGroup_Delete(t *testing.T) {
t.Parallel()
@ -200,32 +254,44 @@ func TestGroup_Delete(t *testing.T) {
assert.Len(t, groups, 0)
})
t.Run("ERR: invalid UUID", func(t *testing.T) {
t.Run("ERR: group not found", func(t *testing.T) {
t.Parallel()
id := "12345"
assert.ErrorIs(t, repo.Delete(context.Background(), id, ""), domain.GroupNotFoundError{ID: id})
})
tests := []struct {
name string
id string
serverID string
}{
{
name: "ID - not UUID",
id: "test",
serverID: group.ServerID,
},
{
name: "ID - random UUID",
id: uuid.NewString(),
serverID: group.ServerID,
},
{
name: "ServerID - random UUID",
id: group.ID.String(),
serverID: uuid.NewString(),
},
}
t.Run("ERR: group not found (unknown ID)", func(t *testing.T) {
t.Parallel()
for _, tt := range tests {
tt := tt
id := uuid.NewString()
assert.ErrorIs(
t,
repo.Delete(context.Background(), id, group.ServerID),
domain.GroupNotFoundError{ID: id},
)
})
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
t.Run("ERR: group not found (unknown ServerID)", func(t *testing.T) {
t.Parallel()
assert.ErrorIs(
t,
repo.Delete(context.Background(), group.ID.String(), uuid.NewString()),
domain.GroupNotFoundError{ID: group.ID.String()},
)
assert.ErrorIs(
t,
repo.Delete(context.Background(), tt.id, tt.serverID),
domain.GroupNotFoundError{ID: tt.id},
)
})
}
})
}

View File

@ -2,6 +2,7 @@ package bundb
import (
"context"
"database/sql"
"errors"
"fmt"
@ -47,6 +48,29 @@ func (m *Monitor) Create(ctx context.Context, params domain.CreateMonitorParams)
return monitor.ToDomain(), nil
}
func (m *Monitor) List(ctx context.Context, groupID string) ([]domain.Monitor, error) {
if _, err := uuid.Parse(groupID); err != nil {
return nil, nil
}
var monitors []model.Monitor
if err := m.db.NewSelect().
Model(&monitors).
Order("created_at ASC").
Where("group_id = ?", groupID).
Scan(ctx); err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, fmt.Errorf("couldn't select monitors from the db: %w", err)
}
result := make([]domain.Monitor, 0, len(monitors))
for _, monitor := range monitors {
result = append(result, monitor.ToDomain())
}
return result, nil
}
func mapCreateMonitorError(err error, params domain.CreateMonitorParams) error {
var pgError pgdriver.Error
if !errors.As(err, &pgError) {

View File

@ -18,6 +18,7 @@ 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
}
@ -91,6 +92,14 @@ func (g *Group) List(ctx context.Context, params domain.ListGroupsParams) ([]dom
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)

View File

@ -2,6 +2,7 @@ package service
import (
"context"
"errors"
"fmt"
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
@ -12,16 +13,35 @@ type MonitorRepository interface {
Create(ctx context.Context, params domain.CreateMonitorParams) (domain.Monitor, error)
}
type Monitor struct {
repo MonitorRepository
//counterfeiter:generate -o internal/mock/group_getter.gen.go . GroupGetter
type GroupGetter interface {
Get(ctx context.Context, id, serverID string) (domain.Group, error)
}
func (m *Monitor) Create(ctx context.Context, serverID string, params domain.CreateMonitorParams) (domain.Monitor, error) {
type Monitor struct {
repo MonitorRepository
groupSvc GroupGetter
}
func (m *Monitor) Create(ctx context.Context, groupID, serverID, tribeTag string) (domain.Monitor, error) {
// check if group exists
group, err := m.groupSvc.Get(ctx, groupID, serverID)
if err != nil {
if errors.Is(err, domain.GroupNotFoundError{ID: groupID}) {
return domain.Monitor{}, domain.GroupDoesNotExistError{ID: groupID}
}
return domain.Monitor{}, fmt.Errorf("GroupService.Get: %w", err)
}
// check limit
// check if tribe exists
// check limit
const temp = 123
params, err := domain.NewCreateMonitorParams(group.ID, temp)
if err != nil {
return domain.Monitor{}, fmt.Errorf("domain.NewCreateMonitorParams: %w", err)
}
monitor, err := m.repo.Create(ctx, params)
if err != nil {