mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package repositories
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/muety/wakapi/config"
|
|
"github.com/muety/wakapi/models"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type KeyValueRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewKeyValueRepository(db *gorm.DB) *KeyValueRepository {
|
|
return &KeyValueRepository{db: db}
|
|
}
|
|
|
|
func (r *KeyValueRepository) GetAll() ([]*models.KeyStringValue, error) {
|
|
var keyValues []*models.KeyStringValue
|
|
if err := r.db.Find(&keyValues).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return keyValues, nil
|
|
}
|
|
|
|
func (r *KeyValueRepository) GetString(key string) (*models.KeyStringValue, error) {
|
|
kv := &models.KeyStringValue{}
|
|
if err := r.db.
|
|
Where(&models.KeyStringValue{Key: key}).
|
|
First(&kv).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return kv, nil
|
|
}
|
|
|
|
func (r *KeyValueRepository) Search(like string) ([]*models.KeyStringValue, error) {
|
|
var keyValues []*models.KeyStringValue
|
|
condition := "key like ?"
|
|
if r.db.Config.Name() == config.SQLDialectMysql {
|
|
condition = "`key` like ?"
|
|
}
|
|
if err := r.db.Table("key_string_values").
|
|
Where(condition, like).
|
|
Find(&keyValues).
|
|
Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return keyValues, nil
|
|
}
|
|
|
|
func (r *KeyValueRepository) PutString(kv *models.KeyStringValue) error {
|
|
result := r.db.
|
|
Clauses(clause.OnConflict{
|
|
UpdateAll: true,
|
|
}).
|
|
Where(&models.KeyStringValue{Key: kv.Key}).
|
|
Assign(kv).
|
|
Create(kv)
|
|
|
|
if err := result.Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *KeyValueRepository) DeleteString(key string) error {
|
|
result := r.db.
|
|
Delete(&models.KeyStringValue{}, &models.KeyStringValue{Key: key})
|
|
|
|
if err := result.Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
if result.RowsAffected != 1 {
|
|
return errors.New("nothing deleted")
|
|
}
|
|
|
|
return nil
|
|
}
|