twitch-bot/internal/actors/counter/database.go
Knut Ahlers 0a937a65d0
prepare release v3.0.0
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2022-11-02 22:54:52 +01:00

51 lines
1 KiB
Go

package counter
import (
"github.com/pkg/errors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/Luzifer/twitch-bot/v3/pkg/database"
)
type (
counter struct {
Name string `gorm:"primaryKey"`
Value int64
}
)
func GetCounterValue(db database.Connector, counterName string) (int64, error) {
var c counter
err := db.DB().First(&c, "name = ?", counterName).Error
switch {
case err == nil:
return c.Value, nil
case errors.Is(err, gorm.ErrRecordNotFound):
return 0, nil
default:
return 0, errors.Wrap(err, "querying counter")
}
}
func UpdateCounter(db database.Connector, counterName string, value int64, absolute bool) error {
if !absolute {
cv, err := GetCounterValue(db, counterName)
if err != nil {
return errors.Wrap(err, "getting previous value")
}
value += cv
}
return errors.Wrap(
db.DB().Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "name"}},
DoUpdates: clause.AssignmentColumns([]string{"value"}),
}).Create(counter{Name: counterName, Value: value}).Error,
"storing counter value",
)
}