From ea2171729f09dab2ba010de2c84df59a5496fc15 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Tue, 17 Dec 2024 13:14:09 +0100 Subject: [PATCH] add dbutil for FindMulti/Single --- db/chat_log.go | 8 ++------ db/config.go | 7 +------ db/feature.go | 11 ++--------- db/log.go | 4 +--- db/mesecons.go | 11 ++--------- db/mod.go | 18 +++--------------- db/util.go | 19 +++++++++++++++++++ 7 files changed, 30 insertions(+), 48 deletions(-) create mode 100644 db/util.go diff --git a/db/chat_log.go b/db/chat_log.go index fa789073..1a1c175d 100644 --- a/db/chat_log.go +++ b/db/chat_log.go @@ -25,15 +25,11 @@ func (r *ChatLogRepository) Insert(l *types.ChatLog) error { } func (r *ChatLogRepository) Search(channel string, from, to int64) ([]*types.ChatLog, error) { - var list []*types.ChatLog - err := r.g.Where("timestamp > ?", from).Where("timestamp < ?", to).Where(types.ChatLog{Channel: channel}).Find(&list).Error - return list, err + return FindMulti[types.ChatLog](r.g.Where("timestamp > ?", from).Where("timestamp < ?", to).Where(types.ChatLog{Channel: channel})) } func (r *ChatLogRepository) GetLatest(channel string, limit int) ([]*types.ChatLog, error) { - var list []*types.ChatLog - err := r.g.Where(types.ChatLog{Channel: channel}).Order("timestamp ASC").Limit(limit).Find(&list).Error - return list, err + return FindMulti[types.ChatLog](r.g.Where(types.ChatLog{Channel: channel}).Order("timestamp ASC")) } func (r *ChatLogRepository) DeleteBefore(timestamp int64) error { diff --git a/db/config.go b/db/config.go index cb41f057..6d874e86 100644 --- a/db/config.go +++ b/db/config.go @@ -12,12 +12,7 @@ type ConfigRepository struct { } func (r *ConfigRepository) GetByKey(key types.ConfigKey) (*types.ConfigEntry, error) { - var list []*types.ConfigEntry - err := r.g.Where(types.ConfigEntry{Key: key}).Limit(1).Find(&list).Error - if len(list) == 0 { - return nil, err - } - return list[0], err + return FindSingle[types.ConfigEntry](r.g.Where(types.ConfigEntry{Key: key})) } func (r *ConfigRepository) Set(c *types.ConfigEntry) error { diff --git a/db/feature.go b/db/feature.go index 3c14a0d4..76a10667 100644 --- a/db/feature.go +++ b/db/feature.go @@ -19,16 +19,9 @@ func (r *FeatureRepository) Set(m *types.Feature) error { } func (r *FeatureRepository) GetAll() ([]*types.Feature, error) { - var list []*types.Feature - err := r.g.Find(&list).Error - return list, err + return FindMulti[types.Feature](r.g) } func (r *FeatureRepository) GetByName(name string) (*types.Feature, error) { - var list []*types.Feature - err := r.g.Where(types.Feature{Name: name}).Limit(1).Find(&list).Error - if len(list) == 0 { - return nil, err - } - return list[0], err + return FindSingle[types.Feature](r.g.Where(types.Feature{Name: name})) } diff --git a/db/log.go b/db/log.go index cf259f6e..6bc5ef04 100644 --- a/db/log.go +++ b/db/log.go @@ -109,9 +109,7 @@ func (r *LogRepository) query(s *types.LogSearch) *gorm.DB { } func (r *LogRepository) Search(s *types.LogSearch) ([]*types.Log, error) { - var list []*types.Log - err := r.query(s).Find(&list).Error - return list, err + return FindMulti[types.Log](r.query(s)) } func (r *LogRepository) Count(s *types.LogSearch) (int, error) { diff --git a/db/mesecons.go b/db/mesecons.go index b5ef7534..e581a9f9 100644 --- a/db/mesecons.go +++ b/db/mesecons.go @@ -19,18 +19,11 @@ func (r *MeseconsRepository) Save(m *types.Mesecons) error { } func (r *MeseconsRepository) GetByPlayerName(playername string) ([]*types.Mesecons, error) { - var list []*types.Mesecons - err := r.g.Where(types.Mesecons{PlayerName: playername}).Order("order_id ASC").Find(&list).Error - return list, err + return FindMulti[types.Mesecons](r.g.Where(types.Mesecons{PlayerName: playername}).Order("order_id ASC")) } func (r *MeseconsRepository) GetByPoskey(poskey string) (*types.Mesecons, error) { - var list []*types.Mesecons - err := r.g.Where(types.Mesecons{PosKey: poskey}).Find(&list).Error - if len(list) == 0 { - return nil, err - } - return list[0], err + return FindSingle[types.Mesecons](r.g.Where(types.Mesecons{PosKey: poskey})) } func (r *MeseconsRepository) Remove(poskey string) error { diff --git a/db/mod.go b/db/mod.go index bcaf8329..0f06e9d5 100644 --- a/db/mod.go +++ b/db/mod.go @@ -19,27 +19,15 @@ func (r *ModRepository) Create(m *types.Mod) error { } func (r *ModRepository) GetAll() ([]*types.Mod, error) { - var list []*types.Mod - err := r.g.Find(&list).Error - return list, err + return FindMulti[types.Mod](r.g) } func (r *ModRepository) GetByName(name string) (*types.Mod, error) { - var list []*types.Mod - err := r.g.Where(types.Mod{Name: name}).Limit(1).Find(&list).Error - if len(list) == 0 { - return nil, err - } - return list[0], err + return FindSingle[types.Mod](r.g.Where(types.Mod{Name: name})) } func (r *ModRepository) GetByID(id string) (*types.Mod, error) { - var list []*types.Mod - err := r.g.Where(&types.Mod{ID: id}).Limit(1).Find(&list).Error - if len(list) == 0 { - return nil, err - } - return list[0], err + return FindSingle[types.Mod](r.g.Where(types.Mod{ID: id})) } func (r *ModRepository) Update(m *types.Mod) error { diff --git a/db/util.go b/db/util.go new file mode 100644 index 00000000..310de63e --- /dev/null +++ b/db/util.go @@ -0,0 +1,19 @@ +package db + +import "gorm.io/gorm" + +func FindSingle[T any](g *gorm.DB) (*T, error) { + list := make([]*T, 0) + err := g.Limit(1).Find(&list).Error + if err != nil || len(list) == 0 { + return nil, err + } else { + return list[0], err + } +} + +func FindMulti[T any](g *gorm.DB) ([]*T, error) { + list := make([]*T, 0) + err := g.Find(&list).Error + return list, err +}