Skip to content

Commit

Permalink
add dbutil for FindMulti/Single
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Dec 17, 2024
1 parent 2a14f5b commit ea21717
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 48 deletions.
8 changes: 2 additions & 6 deletions db/chat_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 2 additions & 9 deletions db/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
}
4 changes: 1 addition & 3 deletions db/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 2 additions & 9 deletions db/mesecons.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 3 additions & 15 deletions db/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions db/util.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ea21717

Please sign in to comment.