refactor: delete /group set server
All checks were successful
continuous-integration/drone/pr Build is passing
All checks were successful
continuous-integration/drone/pr Build is passing
This commit is contained in:
parent
dc27fb9214
commit
9d7769e8c7
|
@ -126,14 +126,6 @@ func (u updateGroupsParamsApplier) apply(q *bun.UpdateQuery) *bun.UpdateQuery {
|
|||
}
|
||||
}
|
||||
|
||||
if u.params.ServerKey.Valid {
|
||||
q = q.Set("server_key = ?", u.params.ServerKey.String)
|
||||
}
|
||||
|
||||
if u.params.VersionCode.Valid {
|
||||
q = q.Set("version_code = ?", u.params.VersionCode.String)
|
||||
}
|
||||
|
||||
return q
|
||||
}
|
||||
|
||||
|
|
|
@ -64,22 +64,12 @@ func TestGroup_Update(t *testing.T) {
|
|||
String: group.ChannelLosses + "update",
|
||||
Valid: true,
|
||||
},
|
||||
ServerKey: domain.NullString{
|
||||
String: group.ServerKey + "update",
|
||||
Valid: true,
|
||||
},
|
||||
VersionCode: domain.NullString{
|
||||
String: "update",
|
||||
Valid: true,
|
||||
},
|
||||
}
|
||||
|
||||
updatedGroup, err := repo.Update(context.Background(), group.ID.String(), group.ServerID, params)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, params.ChannelGains.String, updatedGroup.ChannelGains)
|
||||
assert.Equal(t, params.ChannelLosses.String, updatedGroup.ChannelLosses)
|
||||
assert.Equal(t, params.ServerKey.String, updatedGroup.ServerKey)
|
||||
assert.Equal(t, params.VersionCode.String, updatedGroup.VersionCode)
|
||||
})
|
||||
|
||||
t.Run("ERR: nothing to update", func(t *testing.T) {
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
|
||||
type GroupService interface {
|
||||
Create(ctx context.Context, params domain.CreateGroupParams) (domain.Group, error)
|
||||
SetTWServer(ctx context.Context, id, serverID, versionCode, serverKey string) (domain.Group, error)
|
||||
SetChannelGains(ctx context.Context, id, serverID, channel string) (domain.Group, error)
|
||||
SetChannelLosses(ctx context.Context, id, serverID, channel string) (domain.Group, error)
|
||||
List(ctx context.Context, params domain.ListGroupsParams) ([]domain.Group, error)
|
||||
|
|
|
@ -88,32 +88,6 @@ func (c *groupCommand) create(s *discordgo.Session) error {
|
|||
Description: "Sets various properties in a group configuration",
|
||||
Type: discordgo.ApplicationCommandOptionSubCommandGroup,
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Name: "server",
|
||||
Description: "Sets a Tribal Wars server for a group",
|
||||
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Name: "group",
|
||||
Description: "Group ID",
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "version",
|
||||
Description: "e.g. www.tribalwars.net, www.plemiona.pl",
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Choices: versionChoices,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "server",
|
||||
Description: "Tribal Wars server (e.g. en115, pl170)",
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "channel-gains",
|
||||
Description: "Specifies on which channel notifications of gained villages will appear",
|
||||
|
@ -349,9 +323,6 @@ func (c *groupCommand) handleList(s *discordgo.Session, i *discordgo.Interaction
|
|||
|
||||
func (c *groupCommand) handleSet(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
switch i.ApplicationCommandData().Options[0].Options[0].Name {
|
||||
case "server":
|
||||
c.handleSetServer(s, i)
|
||||
return
|
||||
case "channel-gains":
|
||||
c.handleSetChannelGains(s, i)
|
||||
return
|
||||
|
@ -362,45 +333,6 @@ func (c *groupCommand) handleSet(s *discordgo.Session, i *discordgo.InteractionC
|
|||
}
|
||||
}
|
||||
|
||||
func (c *groupCommand) handleSetServer(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
ctx := context.Background()
|
||||
|
||||
group := ""
|
||||
version := ""
|
||||
server := ""
|
||||
for _, opt := range i.ApplicationCommandData().Options[0].Options[0].Options {
|
||||
if opt == nil {
|
||||
continue
|
||||
}
|
||||
switch opt.Name {
|
||||
case "group":
|
||||
group = opt.StringValue()
|
||||
case "version":
|
||||
version = opt.StringValue()
|
||||
case "server":
|
||||
server = opt.StringValue()
|
||||
}
|
||||
}
|
||||
|
||||
_, err := c.svc.SetTWServer(ctx, group, i.GuildID, version, server)
|
||||
if err != nil {
|
||||
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: messageFromError(err),
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "group has been successfully updated",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (c *groupCommand) handleSetChannelGains(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
ctx := context.Background()
|
||||
|
||||
|
|
|
@ -74,15 +74,11 @@ func (c CreateGroupParams) ChannelLosses() string {
|
|||
type UpdateGroupParams struct {
|
||||
ChannelGains NullString
|
||||
ChannelLosses NullString
|
||||
ServerKey NullString
|
||||
VersionCode NullString
|
||||
}
|
||||
|
||||
func (u UpdateGroupParams) IsZero() bool {
|
||||
return !u.ChannelGains.Valid &&
|
||||
!u.ChannelLosses.Valid &&
|
||||
!u.VersionCode.Valid &&
|
||||
!u.ServerKey.Valid
|
||||
!u.ChannelLosses.Valid
|
||||
}
|
||||
|
||||
type ListGroupsParams struct {
|
||||
|
|
|
@ -98,14 +98,6 @@ func TestUpdateGroupParams_IsZero(t *testing.T) {
|
|||
String: "123",
|
||||
Valid: true,
|
||||
},
|
||||
ServerKey: domain.NullString{
|
||||
String: "123",
|
||||
Valid: true,
|
||||
},
|
||||
VersionCode: domain.NullString{
|
||||
String: "123",
|
||||
Valid: true,
|
||||
},
|
||||
},
|
||||
output: false,
|
||||
},
|
||||
|
@ -129,26 +121,6 @@ func TestUpdateGroupParams_IsZero(t *testing.T) {
|
|||
},
|
||||
output: false,
|
||||
},
|
||||
{
|
||||
name: "OK: ServerKey",
|
||||
params: domain.UpdateGroupParams{
|
||||
ServerKey: domain.NullString{
|
||||
String: "123",
|
||||
Valid: true,
|
||||
},
|
||||
},
|
||||
output: false,
|
||||
},
|
||||
{
|
||||
name: "OK: VersionCode",
|
||||
params: domain.UpdateGroupParams{
|
||||
VersionCode: domain.NullString{
|
||||
String: "123",
|
||||
Valid: true,
|
||||
},
|
||||
},
|
||||
output: false,
|
||||
},
|
||||
{
|
||||
name: "OK: empty struct",
|
||||
params: domain.UpdateGroupParams{},
|
||||
|
|
|
@ -57,28 +57,6 @@ func (g *Group) Create(ctx context.Context, params domain.CreateGroupParams) (do
|
|||
return group, nil
|
||||
}
|
||||
|
||||
func (g *Group) SetTWServer(ctx context.Context, id, serverID, versionCode, serverKey string) (domain.Group, error) {
|
||||
if err := g.checkTWServer(ctx, versionCode, serverKey); err != nil {
|
||||
return domain.Group{}, err
|
||||
}
|
||||
|
||||
group, err := g.repo.Update(ctx, id, serverID, domain.UpdateGroupParams{
|
||||
VersionCode: domain.NullString{
|
||||
String: versionCode,
|
||||
Valid: true,
|
||||
},
|
||||
ServerKey: domain.NullString{
|
||||
String: serverKey,
|
||||
Valid: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return domain.Group{}, fmt.Errorf("GroupRepository.Update: %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{
|
||||
|
|
|
@ -125,85 +125,3 @@ func TestGroup_Create(t *testing.T) {
|
|||
assert.Zero(t, g)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGroup_SetTWServer(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
id := uuid.NewString()
|
||||
serverID := uuid.NewString()
|
||||
versionCode := "pl"
|
||||
serverKey := "pl181"
|
||||
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
repo := &mock.FakeGroupRepository{}
|
||||
repo.UpdateCalls(func(_ context.Context, id, serverID string, p domain.UpdateGroupParams) (domain.Group, error) {
|
||||
return domain.Group{
|
||||
ID: id,
|
||||
ServerID: serverID,
|
||||
ChannelGains: p.ChannelGains.String,
|
||||
ChannelLosses: p.ChannelLosses.String,
|
||||
ServerKey: p.ServerKey.String,
|
||||
VersionCode: p.VersionCode.String,
|
||||
CreatedAt: time.Now(),
|
||||
}, nil
|
||||
})
|
||||
repo.ListReturns(nil, nil)
|
||||
|
||||
client := &mock.FakeTWHelpClient{}
|
||||
client.GetServerCalls(func(_ context.Context, _ string, server string) (twhelp.Server, error) {
|
||||
return twhelp.Server{
|
||||
Key: server,
|
||||
URL: fmt.Sprintf("https://%s.tribalwars.net", server),
|
||||
Open: true,
|
||||
}, nil
|
||||
})
|
||||
|
||||
g, err := service.NewGroup(repo, client).SetTWServer(context.Background(), id, serverID, versionCode, serverKey)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, id, g.ID)
|
||||
assert.Equal(t, serverID, g.ServerID)
|
||||
assert.Equal(t, serverKey, g.ServerKey)
|
||||
assert.Equal(t, versionCode, g.VersionCode)
|
||||
})
|
||||
|
||||
t.Run("ERR: server doesn't exist", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := &mock.FakeTWHelpClient{}
|
||||
client.GetServerCalls(func(_ context.Context, _ string, _ string) (twhelp.Server, error) {
|
||||
return twhelp.Server{}, twhelp.APIError{
|
||||
Code: twhelp.ErrorCodeEntityNotFound,
|
||||
Message: "server not found",
|
||||
}
|
||||
})
|
||||
|
||||
g, err := service.NewGroup(nil, client).SetTWServer(context.Background(), id, serverID, versionCode, serverKey)
|
||||
assert.ErrorIs(t, err, domain.ServerDoesNotExistError{
|
||||
VersionCode: versionCode,
|
||||
Key: serverKey,
|
||||
})
|
||||
assert.Zero(t, g)
|
||||
})
|
||||
|
||||
t.Run("ERR: server is closed", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := &mock.FakeTWHelpClient{}
|
||||
client.GetServerCalls(func(ctx context.Context, _ string, server string) (twhelp.Server, error) {
|
||||
return twhelp.Server{
|
||||
Key: server,
|
||||
URL: fmt.Sprintf("https://%s.tribalwars.net", server),
|
||||
Open: false,
|
||||
}, nil
|
||||
})
|
||||
|
||||
g, err := service.NewGroup(nil, client).SetTWServer(context.Background(), id, serverID, versionCode, serverKey)
|
||||
assert.ErrorIs(t, err, domain.ServerIsClosedError{
|
||||
VersionCode: versionCode,
|
||||
Key: serverKey,
|
||||
})
|
||||
assert.Zero(t, g)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user