navidrome/persistence/property_repository.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

59 lines
1.3 KiB
Go
Raw Normal View History

package persistence
2020-01-13 03:46:40 +01:00
import (
"context"
"github.com/Masterminds/squirrel"
2020-01-13 03:46:40 +01:00
"github.com/astaxie/beego/orm"
2020-01-24 01:44:08 +01:00
"github.com/deluan/navidrome/model"
2020-01-13 03:46:40 +01:00
)
type propertyRepository struct {
sqlRepository
}
func NewPropertyRepository(ctx context.Context, o orm.Ormer) model.PropertyRepository {
2020-01-13 03:46:40 +01:00
r := &propertyRepository{}
r.ctx = ctx
r.ormer = o
2020-01-13 06:04:11 +01:00
r.tableName = "property"
2020-01-13 03:46:40 +01:00
return r
}
func (r propertyRepository) Put(id string, value string) error {
update := squirrel.Update(r.tableName).Set("value", value).Where(squirrel.Eq{"id": id})
count, err := r.executeSQL(update)
2020-01-13 03:46:40 +01:00
if err != nil {
return nil
}
if count > 0 {
return nil
2020-01-13 03:46:40 +01:00
}
insert := squirrel.Insert(r.tableName).Columns("id", "value").Values(id, value)
_, err = r.executeSQL(insert)
2020-01-13 03:46:40 +01:00
return err
}
func (r propertyRepository) Get(id string) (string, error) {
sel := squirrel.Select("value").From(r.tableName).Where(squirrel.Eq{"id": id})
resp := struct {
Value string
}{}
err := r.queryOne(sel, &resp)
if err != nil {
return "", err
2020-01-13 03:46:40 +01:00
}
return resp.Value, nil
2020-01-13 03:46:40 +01:00
}
func (r propertyRepository) DefaultGet(id string, defaultValue string) (string, error) {
2020-01-13 03:46:40 +01:00
value, err := r.Get(id)
2020-01-15 04:22:34 +01:00
if err == model.ErrNotFound {
2020-01-13 03:46:40 +01:00
return defaultValue, nil
}
if err != nil {
return defaultValue, err
}
return value, nil
}