Skip to content

Commit

Permalink
refactor: gorm warnings from First method usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Nov 1, 2023
1 parent 4e61410 commit 0a70073
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions app/configurator/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package configurator

import (
"darkbot/app/configurator/models"
"darkbot/app/settings/logus"
"darkbot/app/settings/types"
)

Expand Down Expand Up @@ -120,7 +121,8 @@ func (c IConfiguratorAlertThreshold[T]) Unset(channelID types.DiscordChannelID)

func (c IConfiguratorAlertThreshold[T]) Status(channelID types.DiscordChannelID) (int, error) {
var obj T
result := c.db.Where("channel_id = ?", channelID).First(&obj)
result := c.db.Where("channel_id = ?", channelID).Limit(1).Find(&obj)

if result.Error != nil {
return 0, result.Error
}
Expand Down Expand Up @@ -155,7 +157,7 @@ func (c IConfiguratorAlertBool[T]) Disable(channelID types.DiscordChannelID) err

func (c IConfiguratorAlertBool[T]) Status(channelID types.DiscordChannelID) (bool, error) {
obj := T{}
result := c.db.Where("channel_id = ?", channelID).First(&obj)
result := c.db.Where("channel_id = ?", channelID).Limit(1).Find(&obj)

return result.Error == nil && result.RowsAffected == 1, result.Error
}
Expand All @@ -175,13 +177,14 @@ func (c IConfiguratorAlertString[T]) Set(channelID types.DiscordChannelID, value
func (c IConfiguratorAlertString[T]) Unset(channelID types.DiscordChannelID) error {
objs := []T{}
result := c.db.Unscoped().Where("channel_id = ?", channelID).Find(&objs)
logus.CheckWarn(result.Error, "attempted to unset with errors", logus.GormResult(result))
result = c.db.Unscoped().Delete(&objs)
return result.Error
}

func (c IConfiguratorAlertString[T]) Status(channelID types.DiscordChannelID) (types.PingMessage, error) {
var obj T
result := c.db.Where("channel_id = ?", channelID).First(&obj)
result := c.db.Where("channel_id = ?", channelID).Limit(1).Find(&obj)
if result.Error != nil {
return "", result.Error
}
Expand Down
2 changes: 1 addition & 1 deletion app/configurator/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type ErrorZeroAffectedRows struct {
}

func (z ErrorZeroAffectedRows) Error() string {
return "Zero affected rows. Expected more." + z.ExtraMsg
return "Zero affected rows. not found records. Expected more." + z.ExtraMsg
}

/////////////////////
Expand Down

0 comments on commit 0a70073

Please sign in to comment.