discordBots/cmd/dealsbot/repository/repository.go

124 lines
2.6 KiB
Go
Raw Normal View History

2023-03-06 19:53:07 +00:00
package repository
2023-03-04 11:54:08 +00:00
import (
"encoding/json"
"github.com/dgraph-io/badger/v4"
2023-03-06 19:53:07 +00:00
"grow.rievo.dev/discordBots/cmd/dealsbot/api"
2023-11-05 16:03:52 +00:00
"log/slog"
2023-03-04 11:54:08 +00:00
)
type Repository interface {
2023-03-06 19:53:07 +00:00
GetAll() ([]api.Deal, error)
GetValue(dealId string) api.Deal
SetValue(deal api.Deal) error
2023-03-04 11:54:08 +00:00
DeleteValue(dealId string) error
Close() error
}
type DealRepository struct {
2023-11-05 16:03:52 +00:00
db *badger.DB
logger *slog.Logger
2023-03-04 11:54:08 +00:00
}
2023-11-05 16:03:52 +00:00
func InitDb(logger *slog.Logger) *DealRepository {
2023-03-04 11:54:08 +00:00
opts := badger.DefaultOptions("./db")
opts.Logger = nil
db, err := badger.Open(opts)
if err != nil {
2023-11-05 16:03:52 +00:00
logger.Error("error opening DB", slog.Any("error", err))
2023-03-04 11:54:08 +00:00
}
2023-11-05 16:03:52 +00:00
return &DealRepository{db, logger}
2023-03-04 11:54:08 +00:00
}
func (d *DealRepository) Close() error {
return d.db.Close()
}
func (d *DealRepository) RunGC() error {
return d.db.RunValueLogGC(0.7)
}
2023-03-06 19:53:07 +00:00
func (d *DealRepository) GetAll() ([]api.Deal, error) {
var deals []api.Deal
2023-03-04 11:54:08 +00:00
err := d.db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
err := item.Value(func(val []byte) error {
2023-03-06 19:53:07 +00:00
retrievedDeal := api.Deal{}
2023-03-04 11:54:08 +00:00
err := json.Unmarshal(val, &retrievedDeal)
deals = append(deals, retrievedDeal)
return err
})
if err != nil {
return err
}
}
return nil
})
return deals, err
}
2023-03-06 19:53:07 +00:00
func (d *DealRepository) GetValue(dealId string) (api.Deal, error) {
retrievedDeal := api.Deal{}
2023-03-04 11:54:08 +00:00
err := d.db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(dealId))
if err != nil {
return err
}
err = item.Value(func(val []byte) error {
err = json.Unmarshal(val, &retrievedDeal)
return err
})
return err
})
if err != nil {
2023-03-06 19:53:07 +00:00
return api.Deal{}, err
2023-03-04 11:54:08 +00:00
}
return retrievedDeal, nil
}
2023-03-06 19:53:07 +00:00
func (d *DealRepository) SetValue(deal api.Deal) error {
2023-03-04 11:54:08 +00:00
jsonBytes, err := json.Marshal(deal)
if err != nil {
return err
}
err = d.db.Update(func(txn *badger.Txn) error {
err := txn.Set([]byte(deal.Id), jsonBytes)
return err
})
if err != nil {
return err
}
return nil
}
func (d *DealRepository) DeleteValue(dealId string) error {
err := d.db.Update(func(txn *badger.Txn) error {
err := txn.Delete([]byte(dealId))
return err
})
return err
}
func (d *DealRepository) DeleteAll() error {
err := d.db.Update(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
err := txn.Delete(item.Key())
if err != nil {
return err
}
}
return nil
})
return err
}