navidrome/persistence/share_repository.go

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

113 lines
2.7 KiB
Go
Raw Normal View History

package persistence
import (
"context"
2022-10-01 00:54:25 +02:00
"errors"
2023-01-20 04:52:55 +01:00
"time"
. "github.com/Masterminds/squirrel"
2022-07-30 18:43:48 +02:00
"github.com/beego/beego/v2/client/orm"
"github.com/deluan/rest"
"github.com/navidrome/navidrome/model"
)
type shareRepository struct {
sqlRepository
sqlRestful
}
2022-07-30 18:43:48 +02:00
func NewShareRepository(ctx context.Context, o orm.QueryExecutor) model.ShareRepository {
r := &shareRepository{}
r.ctx = ctx
r.ormer = o
r.tableName = "share"
return r
}
func (r *shareRepository) Delete(id string) error {
err := r.delete(Eq{"id": id})
2022-10-01 00:54:25 +02:00
if errors.Is(err, model.ErrNotFound) {
return rest.ErrNotFound
}
return err
}
func (r *shareRepository) selectShare(options ...model.QueryOptions) SelectBuilder {
2023-01-20 04:52:55 +01:00
return r.newSelect(options...).Join("user u on u.id = share.user_id").
Columns("share.*", "user_name as username")
}
2023-01-20 04:52:55 +01:00
func (r *shareRepository) Exists(id string) (bool, error) {
return r.exists(Select().Where(Eq{"id": id}))
}
func (r *shareRepository) GetAll(options ...model.QueryOptions) (model.Shares, error) {
sq := r.selectShare(options...)
res := model.Shares{}
err := r.queryAll(sq, &res)
return res, err
}
func (r *shareRepository) Update(id string, entity interface{}, cols ...string) error {
s := entity.(*model.Share)
2023-01-20 04:52:55 +01:00
// TODO Validate record
s.ID = id
2023-01-21 01:53:53 +01:00
s.UpdatedAt = time.Now()
2023-01-20 04:52:55 +01:00
cols = append(cols, "updated_at")
_, err := r.put(id, s, cols...)
2022-10-01 00:54:25 +02:00
if errors.Is(err, model.ErrNotFound) {
return rest.ErrNotFound
}
return err
}
func (r *shareRepository) Save(entity interface{}) (string, error) {
s := entity.(*model.Share)
2023-01-20 04:52:55 +01:00
// TODO Validate record
u := loggedUser(r.ctx)
if s.UserID == "" {
s.UserID = u.ID
}
2023-01-21 01:53:53 +01:00
s.CreatedAt = time.Now()
s.UpdatedAt = time.Now()
id, err := r.put(s.ID, s)
2022-10-01 00:54:25 +02:00
if errors.Is(err, model.ErrNotFound) {
return "", rest.ErrNotFound
}
return id, err
}
func (r *shareRepository) CountAll(options ...model.QueryOptions) (int64, error) {
return r.count(r.selectShare(), options...)
}
func (r *shareRepository) Count(options ...rest.QueryOptions) (int64, error) {
return r.CountAll(r.parseRestOptions(options...))
}
func (r *shareRepository) EntityName() string {
return "share"
}
func (r *shareRepository) NewInstance() interface{} {
return &model.Share{}
}
func (r *shareRepository) Get(id string) (*model.Share, error) {
sel := r.selectShare().Where(Eq{"share.id": id})
var res model.Share
err := r.queryOne(sel, &res)
return &res, err
}
func (r *shareRepository) Read(id string) (interface{}, error) {
return r.Get(id)
}
func (r *shareRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
return r.GetAll(r.parseRestOptions(options...))
}
var _ model.ShareRepository = (*shareRepository)(nil)
var _ rest.Repository = (*shareRepository)(nil)
var _ rest.Persistable = (*shareRepository)(nil)