feat: add new commands - group set channel-losses/channel-gains (#13)
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #13
This commit is contained in:
Dawid Wysokiński 2022-10-11 05:05:35 +00:00
parent 0436c56813
commit aec3a3736d
9 changed files with 299 additions and 133 deletions

View File

@ -22,11 +22,11 @@ func NewGroup(db *bun.DB) *Group {
func (g *Group) Create(ctx context.Context, params domain.CreateGroupParams) (domain.Group, error) {
group := model.Group{
ServerID: params.ServerID(),
VersionCode: params.VersionCode(),
ChannelGainedVillages: params.ChannelGainedVillages(),
ChannelLostVillages: params.ChannelLostVillages(),
ServerKey: params.ServerKey(),
ServerID: params.ServerID(),
VersionCode: params.VersionCode(),
ChannelGains: params.ChannelGains(),
ChannelLosses: params.ChannelLosses(),
ServerKey: params.ServerKey(),
}
if _, err := g.db.NewInsert().
@ -89,12 +89,12 @@ type updateGroupsParamsApplier struct {
}
func (u updateGroupsParamsApplier) apply(q *bun.UpdateQuery) *bun.UpdateQuery {
if u.params.ChannelGainedVillages.Valid {
q = q.Set("channel_gained_villages = ?", u.params.ChannelGainedVillages.String)
if u.params.ChannelGains.Valid {
q = q.Set("channel_gains = ?", u.params.ChannelGains.String)
}
if u.params.ChannelLostVillages.Valid {
q = q.Set("channel_lost_villages = ?", u.params.ChannelLostVillages.String)
if u.params.ChannelLosses.Valid {
q = q.Set("channel_losses = ?", u.params.ChannelLosses.String)
}
if u.params.ServerKey.Valid {

View File

@ -38,8 +38,8 @@ func TestGroup_Create(t *testing.T) {
assert.Equal(t, params.ServerID(), group.ServerID)
assert.Equal(t, params.ServerKey(), group.ServerKey)
assert.Equal(t, params.VersionCode(), group.VersionCode)
assert.Equal(t, params.ChannelGainedVillages(), group.ChannelGainedVillages)
assert.Equal(t, params.ChannelLostVillages(), group.ChannelLostVillages)
assert.Equal(t, params.ChannelGains(), group.ChannelGains)
assert.Equal(t, params.ChannelLosses(), group.ChannelLosses)
assert.WithinDuration(t, time.Now(), group.CreatedAt, 1*time.Second)
})
}
@ -56,12 +56,12 @@ func TestGroup_UpdateByID(t *testing.T) {
group := getGroupFromFixture(t, fixture, "group-server-1-1")
params := domain.UpdateGroupParams{
ChannelGainedVillages: domain.NullString{
String: group.ChannelGainedVillages + "update",
ChannelGains: domain.NullString{
String: group.ChannelGains + "update",
Valid: true,
},
ChannelLostVillages: domain.NullString{
String: group.ChannelLostVillages + "update",
ChannelLosses: domain.NullString{
String: group.ChannelLosses + "update",
Valid: true,
},
ServerKey: domain.NullString{
@ -76,8 +76,8 @@ func TestGroup_UpdateByID(t *testing.T) {
updatedGroup, err := repo.UpdateByID(context.Background(), group.ID.String(), params)
assert.NoError(t, err)
assert.Equal(t, params.ChannelGainedVillages.String, updatedGroup.ChannelGainedVillages)
assert.Equal(t, params.ChannelLostVillages.String, updatedGroup.ChannelLostVillages)
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)
})
@ -95,7 +95,7 @@ func TestGroup_UpdateByID(t *testing.T) {
id := "12345"
updatedGroup, err := repo.UpdateByID(context.Background(), id, domain.UpdateGroupParams{
ChannelGainedVillages: domain.NullString{
ChannelGains: domain.NullString{
String: "update",
Valid: true,
},
@ -109,7 +109,7 @@ func TestGroup_UpdateByID(t *testing.T) {
id := uuid.NewString()
updatedGroup, err := repo.UpdateByID(context.Background(), id, domain.UpdateGroupParams{
ChannelGainedVillages: domain.NullString{
ChannelGains: domain.NullString{
String: "update",
Valid: true,
},

View File

@ -11,23 +11,23 @@ import (
type Group struct {
bun.BaseModel `bun:"base_model,table:groups,alias:group"`
ID uuid.UUID `bun:"id,type:uuid,pk,default:gen_random_uuid()"`
ServerID string `bun:"server_id,type:varchar(100),notnull"`
ChannelGainedVillages string `bun:"channel_gained_villages,type:varchar(100),nullzero"`
ChannelLostVillages string `bun:"channel_lost_villages,type:varchar(100),nullzero"`
ServerKey string `bun:"server_key,type:varchar(50),notnull"`
VersionCode string `bun:"version_code,type:varchar(6),notnull"`
CreatedAt time.Time `bun:"created_at,nullzero,notnull,default:current_timestamp"`
ID uuid.UUID `bun:"id,type:uuid,pk,default:gen_random_uuid()"`
ServerID string `bun:"server_id,type:varchar(100),notnull"`
ChannelGains string `bun:"channel_gains,type:varchar(100),nullzero"`
ChannelLosses string `bun:"channel_losses,type:varchar(100),nullzero"`
ServerKey string `bun:"server_key,type:varchar(50),notnull"`
VersionCode string `bun:"version_code,type:varchar(6),notnull"`
CreatedAt time.Time `bun:"created_at,nullzero,notnull,default:current_timestamp"`
}
func (g Group) ToDomain() domain.Group {
return domain.Group{
ID: g.ID.String(),
ServerID: g.ServerID,
ChannelGainedVillages: g.ChannelGainedVillages,
ChannelLostVillages: g.ChannelLostVillages,
ServerKey: g.ServerKey,
VersionCode: g.VersionCode,
CreatedAt: g.CreatedAt,
ID: g.ID.String(),
ServerID: g.ServerID,
ChannelGains: g.ChannelGains,
ChannelLosses: g.ChannelLosses,
ServerKey: g.ServerKey,
VersionCode: g.VersionCode,
CreatedAt: g.CreatedAt,
}
}

View File

@ -12,6 +12,8 @@ import (
type GroupService interface {
Create(ctx context.Context, params domain.CreateGroupParams) (domain.Group, error)
SetTWServer(ctx context.Context, id, versionCode, serverKey string) (domain.Group, error)
SetChannelGains(ctx context.Context, id, channel string) (domain.Group, error)
SetChannelLosses(ctx context.Context, id, channel string) (domain.Group, error)
List(ctx context.Context, params domain.ListGroupsParams) ([]domain.Group, error)
}

View File

@ -59,7 +59,7 @@ func (c *groupCommand) create(s *discordgo.Session) error {
Required: true,
},
{
Name: "channel-gained-villages",
Name: "channel-gains",
Description: "Specifies on which channel notifications of gained villages will appear",
Type: discordgo.ApplicationCommandOptionChannel,
ChannelTypes: []discordgo.ChannelType{
@ -68,7 +68,7 @@ func (c *groupCommand) create(s *discordgo.Session) error {
Required: false,
},
{
Name: "channel-lost-villages",
Name: "channel-losses",
Description: "Specifies on which channel notifications of lost villages will appear",
Type: discordgo.ApplicationCommandOptionChannel,
ChannelTypes: []discordgo.ChannelType{
@ -84,28 +84,79 @@ func (c *groupCommand) create(s *discordgo.Session) error {
Type: discordgo.ApplicationCommandOptionSubCommand,
},
{
Name: "set-server",
Description: "Sets a Tribal Wars server (e.g. en115, pl170) for a group",
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "set",
Description: "Sets various properties in a group configuration",
Type: discordgo.ApplicationCommandOptionSubCommandGroup,
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,
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",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "group",
Description: "Group ID",
Type: discordgo.ApplicationCommandOptionString,
Required: true,
},
{
Name: "channel",
Description: "Channel",
Type: discordgo.ApplicationCommandOptionChannel,
ChannelTypes: []discordgo.ChannelType{
discordgo.ChannelTypeGuildText,
},
Required: true,
},
},
},
{
Name: "channel-losses",
Description: "Specifies on which channel notifications of lost villages will appear",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "group",
Description: "Group ID",
Type: discordgo.ApplicationCommandOptionString,
Required: true,
},
{
Name: "channel",
Description: "Channel ID",
Type: discordgo.ApplicationCommandOptionChannel,
ChannelTypes: []discordgo.ChannelType{
discordgo.ChannelTypeGuildText,
},
Required: true,
},
},
},
},
},
@ -147,8 +198,8 @@ func (c *groupCommand) handle(s *discordgo.Session, i *discordgo.InteractionCrea
case "list":
c.handleList(s, i)
return
case "set-server":
c.handleSetServer(s, i)
case "set":
c.handleSet(s, i)
return
default:
}
@ -159,8 +210,8 @@ func (c *groupCommand) handleCreate(s *discordgo.Session, i *discordgo.Interacti
version := ""
server := ""
channelGainedVillages := ""
channelLostVillages := ""
channelGains := ""
channelLosses := ""
for _, opt := range i.ApplicationCommandData().Options[0].Options {
if opt == nil {
continue
@ -170,10 +221,10 @@ func (c *groupCommand) handleCreate(s *discordgo.Session, i *discordgo.Interacti
version = opt.StringValue()
case "server":
server = opt.StringValue()
case "channel-gained-villages":
channelGainedVillages = opt.ChannelValue(s).ID
case "channel-lost-villages":
channelLostVillages = opt.ChannelValue(s).ID
case "channel-gains":
channelGains = opt.ChannelValue(s).ID
case "channel-losses":
channelLosses = opt.ChannelValue(s).ID
}
}
@ -181,8 +232,8 @@ func (c *groupCommand) handleCreate(s *discordgo.Session, i *discordgo.Interacti
i.GuildID,
version,
server,
channelGainedVillages,
channelLostVillages,
channelGains,
channelLosses,
)
if err != nil {
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
@ -244,13 +295,28 @@ 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
case "channel-losses":
c.handleSetChannelLosses(s, i)
return
default:
}
}
func (c *groupCommand) handleSetServer(s *discordgo.Session, i *discordgo.InteractionCreate) {
ctx := context.Background()
group := ""
version := ""
server := ""
for _, opt := range i.ApplicationCommandData().Options[0].Options {
for _, opt := range i.ApplicationCommandData().Options[0].Options[0].Options {
if opt == nil {
continue
}
@ -283,6 +349,78 @@ func (c *groupCommand) handleSetServer(s *discordgo.Session, i *discordgo.Intera
})
}
func (c *groupCommand) handleSetChannelGains(s *discordgo.Session, i *discordgo.InteractionCreate) {
ctx := context.Background()
group := ""
channel := ""
for _, opt := range i.ApplicationCommandData().Options[0].Options[0].Options {
if opt == nil {
continue
}
switch opt.Name {
case "group":
group = opt.StringValue()
case "channel":
channel = opt.ChannelValue(s).ID
}
}
_, err := c.svc.SetChannelGains(ctx, group, channel)
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) handleSetChannelLosses(s *discordgo.Session, i *discordgo.InteractionCreate) {
ctx := context.Background()
group := ""
channel := ""
for _, opt := range i.ApplicationCommandData().Options[0].Options[0].Options {
if opt == nil {
continue
}
switch opt.Name {
case "group":
group = opt.StringValue()
case "channel":
channel = opt.ChannelValue(s).ID
}
}
_, err := c.svc.SetChannelLosses(ctx, group, channel)
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 buildGroupListDescription(groups []domain.Group) string {
description := "**ID** - **Version** - **Server**"
for i, g := range groups {

View File

@ -6,24 +6,24 @@ import (
)
type Group struct {
ID string
ServerID string // Discord server ID
ChannelGainedVillages string
ChannelLostVillages string
ServerKey string // Tribal Wars server key
VersionCode string
CreatedAt time.Time
ID string
ServerID string // Discord server ID
ChannelGains string
ChannelLosses string
ServerKey string // Tribal Wars server key
VersionCode string
CreatedAt time.Time
}
type CreateGroupParams struct {
serverID string
serverKey string
versionCode string
channelGainedVillages string
channelLostVillages string
serverID string
serverKey string
versionCode string
channelGains string
channelLosses string
}
func NewCreateGroupParams(serverID, versionCode, serverKey, channelGainedVillages, channelLostVillages string) (CreateGroupParams, error) {
func NewCreateGroupParams(serverID, versionCode, serverKey, channelGains, channelLosses string) (CreateGroupParams, error) {
if serverID == "" {
return CreateGroupParams{}, RequiredError{
Field: "ServerID",
@ -43,11 +43,11 @@ func NewCreateGroupParams(serverID, versionCode, serverKey, channelGainedVillage
}
return CreateGroupParams{
serverID: serverID,
serverKey: serverKey,
versionCode: versionCode,
channelGainedVillages: channelGainedVillages,
channelLostVillages: channelLostVillages,
serverID: serverID,
serverKey: serverKey,
versionCode: versionCode,
channelGains: channelGains,
channelLosses: channelLosses,
}, nil
}
@ -63,24 +63,24 @@ func (c CreateGroupParams) ServerKey() string {
return c.serverKey
}
func (c CreateGroupParams) ChannelGainedVillages() string {
return c.channelGainedVillages
func (c CreateGroupParams) ChannelGains() string {
return c.channelGains
}
func (c CreateGroupParams) ChannelLostVillages() string {
return c.channelLostVillages
func (c CreateGroupParams) ChannelLosses() string {
return c.channelLosses
}
type UpdateGroupParams struct {
ChannelGainedVillages NullString
ChannelLostVillages NullString
ServerKey NullString
VersionCode NullString
ChannelGains NullString
ChannelLosses NullString
ServerKey NullString
VersionCode NullString
}
func (u UpdateGroupParams) IsZero() bool {
return !u.ChannelGainedVillages.Valid &&
!u.ChannelLostVillages.Valid &&
return !u.ChannelGains.Valid &&
!u.ChannelLosses.Valid &&
!u.VersionCode.Valid &&
!u.ServerKey.Valid
}

View File

@ -13,22 +13,22 @@ func TestNewCreateGroupParams(t *testing.T) {
t.Parallel()
tests := []struct {
name string
serverID string
versionCode string
serverKey string
channelGainedVillages string
channelLostVillages string
err error
name string
serverID string
versionCode string
serverKey string
channelGains string
channelLosses string
err error
}{
{
name: "OK",
serverID: "123441",
versionCode: "en",
serverKey: "en113",
channelGainedVillages: "1234",
channelLostVillages: "1234",
err: nil,
name: "OK",
serverID: "123441",
versionCode: "en",
serverKey: "en113",
channelGains: "1234",
channelLosses: "1234",
err: nil,
},
{
name: "ERR: ServerID cannot be blank",
@ -61,8 +61,8 @@ func TestNewCreateGroupParams(t *testing.T) {
tt.serverID,
tt.versionCode,
tt.serverKey,
tt.channelGainedVillages,
tt.channelLostVillages,
tt.channelGains,
tt.channelLosses,
)
if tt.err != nil {
assert.ErrorIs(t, err, tt.err)
@ -73,8 +73,8 @@ func TestNewCreateGroupParams(t *testing.T) {
assert.Equal(t, tt.serverID, res.ServerID())
assert.Equal(t, tt.serverKey, res.ServerKey())
assert.Equal(t, tt.versionCode, res.VersionCode())
assert.Equal(t, tt.channelGainedVillages, res.ChannelGainedVillages())
assert.Equal(t, tt.channelLostVillages, res.ChannelLostVillages())
assert.Equal(t, tt.channelGains, res.ChannelGains())
assert.Equal(t, tt.channelLosses, res.ChannelLosses())
})
}
}
@ -90,11 +90,11 @@ func TestUpdateGroupParams_IsZero(t *testing.T) {
{
name: "OK: all",
params: domain.UpdateGroupParams{
ChannelGainedVillages: domain.NullString{
ChannelGains: domain.NullString{
String: "123",
Valid: true,
},
ChannelLostVillages: domain.NullString{
ChannelLosses: domain.NullString{
String: "123",
Valid: true,
},
@ -110,9 +110,9 @@ func TestUpdateGroupParams_IsZero(t *testing.T) {
output: false,
},
{
name: "OK: ChannelGainedVillages",
name: "OK: ChannelGains",
params: domain.UpdateGroupParams{
ChannelGainedVillages: domain.NullString{
ChannelGains: domain.NullString{
String: "123",
Valid: true,
},
@ -120,9 +120,9 @@ func TestUpdateGroupParams_IsZero(t *testing.T) {
output: false,
},
{
name: "OK: ChannelLostVillages",
name: "OK: ChannelLosses",
params: domain.UpdateGroupParams{
ChannelLostVillages: domain.NullString{
ChannelLosses: domain.NullString{
String: "123",
Valid: true,
},

View File

@ -93,6 +93,32 @@ func (g *Group) SetTWServer(ctx context.Context, id, versionCode, serverKey stri
return group, nil
}
func (g *Group) SetChannelGains(ctx context.Context, id, channel string) (domain.Group, error) {
group, err := g.repo.UpdateByID(ctx, id, domain.UpdateGroupParams{
ChannelGains: domain.NullString{
String: channel,
Valid: true,
},
})
if err != nil {
return domain.Group{}, fmt.Errorf("GroupRepository.UpdateByID: %w", err)
}
return group, nil
}
func (g *Group) SetChannelLosses(ctx context.Context, id, channel string) (domain.Group, error) {
group, err := g.repo.UpdateByID(ctx, id, domain.UpdateGroupParams{
ChannelLosses: domain.NullString{
String: channel,
Valid: true,
},
})
if err != nil {
return domain.Group{}, fmt.Errorf("GroupRepository.UpdateByID: %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 {

View File

@ -33,13 +33,13 @@ func TestGroup_Create(t *testing.T) {
repo := &mock.FakeGroupRepository{}
repo.CreateCalls(func(_ context.Context, p domain.CreateGroupParams) (domain.Group, error) {
return domain.Group{
ID: uuid.NewString(),
ServerID: p.ServerID(),
ChannelGainedVillages: p.ChannelGainedVillages(),
ChannelLostVillages: p.ChannelLostVillages(),
ServerKey: p.ServerKey(),
VersionCode: p.VersionCode(),
CreatedAt: time.Now(),
ID: uuid.NewString(),
ServerID: p.ServerID(),
ChannelGains: p.ChannelGains(),
ChannelLosses: p.ChannelLosses(),
ServerKey: p.ServerKey(),
VersionCode: p.VersionCode(),
CreatedAt: time.Now(),
}, nil
})
repo.ListReturns(nil, nil)
@ -59,8 +59,8 @@ func TestGroup_Create(t *testing.T) {
assert.Equal(t, params.ServerID(), g.ServerID)
assert.Equal(t, params.ServerKey(), g.ServerKey)
assert.Equal(t, params.VersionCode(), g.VersionCode)
assert.Equal(t, params.ChannelGainedVillages(), g.ChannelGainedVillages)
assert.Equal(t, params.ChannelLostVillages(), g.ChannelLostVillages)
assert.Equal(t, params.ChannelGains(), g.ChannelGains)
assert.Equal(t, params.ChannelLosses(), g.ChannelLosses)
assert.NotEmpty(t, g.CreatedAt)
})
@ -139,13 +139,13 @@ func TestGroup_SetTWServer(t *testing.T) {
repo := &mock.FakeGroupRepository{}
repo.UpdateByIDCalls(func(_ context.Context, id string, p domain.UpdateGroupParams) (domain.Group, error) {
return domain.Group{
ID: id,
ServerID: uuid.NewString(),
ChannelGainedVillages: p.ChannelGainedVillages.String,
ChannelLostVillages: p.ChannelLostVillages.String,
ServerKey: p.ServerKey.String,
VersionCode: p.VersionCode.String,
CreatedAt: time.Now(),
ID: id,
ServerID: uuid.NewString(),
ChannelGains: p.ChannelGains.String,
ChannelLosses: p.ChannelLosses.String,
ServerKey: p.ServerKey.String,
VersionCode: p.VersionCode.String,
CreatedAt: time.Now(),
}, nil
})
repo.ListReturns(nil, nil)