feat: add a new command - /group monitor add
This commit is contained in:
parent
f02db1fc44
commit
bf531bd293
|
@ -79,12 +79,35 @@ func (g *Group) List(ctx context.Context, params domain.ListGroupsParams) ([]dom
|
||||||
}
|
}
|
||||||
|
|
||||||
result := make([]domain.Group, 0, len(groups))
|
result := make([]domain.Group, 0, len(groups))
|
||||||
for _, version := range groups {
|
for _, group := range groups {
|
||||||
result = append(result, version.ToDomain())
|
result = append(result, group.ToDomain())
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
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 {
|
func (g *Group) Delete(ctx context.Context, id, serverID string) error {
|
||||||
if _, err := uuid.Parse(id); err != nil {
|
if _, err := uuid.Parse(id); err != nil {
|
||||||
return domain.GroupNotFoundError{ID: id}
|
return domain.GroupNotFoundError{ID: id}
|
||||||
|
|
|
@ -80,45 +80,47 @@ func TestGroup_Update(t *testing.T) {
|
||||||
assert.Zero(t, updatedGroup)
|
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()
|
t.Parallel()
|
||||||
|
|
||||||
id := "12345"
|
tests := []struct {
|
||||||
updatedGroup, err := repo.Update(context.Background(), id, "", domain.UpdateGroupParams{
|
name string
|
||||||
ChannelGains: domain.NullString{
|
id string
|
||||||
String: "update",
|
serverID string
|
||||||
Valid: true,
|
}{
|
||||||
|
{
|
||||||
|
name: "ID - not UUID",
|
||||||
|
id: "test",
|
||||||
|
serverID: group.ServerID,
|
||||||
},
|
},
|
||||||
})
|
{
|
||||||
assert.ErrorIs(t, err, domain.GroupNotFoundError{ID: id})
|
name: "ID - random UUID",
|
||||||
assert.Zero(t, updatedGroup)
|
id: uuid.NewString(),
|
||||||
})
|
serverID: group.ServerID,
|
||||||
|
|
||||||
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,
|
|
||||||
},
|
},
|
||||||
})
|
{
|
||||||
assert.ErrorIs(t, err, domain.GroupNotFoundError{ID: id})
|
name: "ServerID - random UUID",
|
||||||
assert.Zero(t, updatedGroup)
|
id: group.ID.String(),
|
||||||
})
|
serverID: uuid.NewString(),
|
||||||
|
|
||||||
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,
|
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
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) {
|
func TestGroup_Delete(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
@ -200,32 +254,44 @@ func TestGroup_Delete(t *testing.T) {
|
||||||
assert.Len(t, groups, 0)
|
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()
|
t.Parallel()
|
||||||
|
|
||||||
id := "12345"
|
tests := []struct {
|
||||||
assert.ErrorIs(t, repo.Delete(context.Background(), id, ""), domain.GroupNotFoundError{ID: id})
|
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) {
|
for _, tt := range tests {
|
||||||
t.Parallel()
|
tt := tt
|
||||||
|
|
||||||
id := uuid.NewString()
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
assert.ErrorIs(
|
t.Parallel()
|
||||||
t,
|
|
||||||
repo.Delete(context.Background(), id, group.ServerID),
|
|
||||||
domain.GroupNotFoundError{ID: id},
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("ERR: group not found (unknown ServerID)", func(t *testing.T) {
|
assert.ErrorIs(
|
||||||
t.Parallel()
|
t,
|
||||||
|
repo.Delete(context.Background(), tt.id, tt.serverID),
|
||||||
assert.ErrorIs(
|
domain.GroupNotFoundError{ID: tt.id},
|
||||||
t,
|
)
|
||||||
repo.Delete(context.Background(), group.ID.String(), uuid.NewString()),
|
})
|
||||||
domain.GroupNotFoundError{ID: group.ID.String()},
|
}
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package bundb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
@ -47,6 +48,29 @@ func (m *Monitor) Create(ctx context.Context, params domain.CreateMonitorParams)
|
||||||
return monitor.ToDomain(), nil
|
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 {
|
func mapCreateMonitorError(err error, params domain.CreateMonitorParams) error {
|
||||||
var pgError pgdriver.Error
|
var pgError pgdriver.Error
|
||||||
if !errors.As(err, &pgError) {
|
if !errors.As(err, &pgError) {
|
||||||
|
|
|
@ -18,6 +18,7 @@ type GroupRepository interface {
|
||||||
Create(ctx context.Context, params domain.CreateGroupParams) (domain.Group, error)
|
Create(ctx context.Context, params domain.CreateGroupParams) (domain.Group, error)
|
||||||
Update(ctx context.Context, id, serverID string, params domain.UpdateGroupParams) (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)
|
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
|
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
|
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 {
|
func (g *Group) Delete(ctx context.Context, id, serverID string) error {
|
||||||
if err := g.repo.Delete(ctx, id, serverID); err != nil {
|
if err := g.repo.Delete(ctx, id, serverID); err != nil {
|
||||||
return fmt.Errorf("GroupRepository.Delete: %w", err)
|
return fmt.Errorf("GroupRepository.Delete: %w", err)
|
||||||
|
|
|
@ -2,6 +2,7 @@ package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
|
||||||
|
@ -12,16 +13,35 @@ type MonitorRepository interface {
|
||||||
Create(ctx context.Context, params domain.CreateMonitorParams) (domain.Monitor, error)
|
Create(ctx context.Context, params domain.CreateMonitorParams) (domain.Monitor, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Monitor struct {
|
//counterfeiter:generate -o internal/mock/group_getter.gen.go . GroupGetter
|
||||||
repo MonitorRepository
|
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
|
// 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 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)
|
monitor, err := m.repo.Create(ctx, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user