refactor: new persistence, more SQL, less ORM

This commit is contained in:
Deluan
2020-01-28 08:22:17 -05:00
committed by Deluan Quintão
parent b26a5ef2d0
commit 71c1844bca
38 changed files with 1294 additions and 1346 deletions
+43 -28
View File
@@ -1,57 +1,73 @@
package persistence
import (
"context"
"strings"
. "github.com/Masterminds/squirrel"
"github.com/astaxie/beego/orm"
"github.com/deluan/navidrome/model"
"github.com/google/uuid"
)
type playlist struct {
ID string `orm:"pk;column(id)"`
Name string `orm:"index"`
ID string `orm:"column(id)"`
Name string
Comment string
Duration int
Owner string
Public bool
Tracks string `orm:"type(text)"`
Tracks string
}
type playlistRepository struct {
sqlRepository
}
func NewPlaylistRepository(o orm.Ormer) model.PlaylistRepository {
func NewPlaylistRepository(ctx context.Context, o orm.Ormer) model.PlaylistRepository {
r := &playlistRepository{}
r.ctx = ctx
r.ormer = o
r.tableName = "playlist"
return r
}
func (r *playlistRepository) CountAll() (int64, error) {
return r.count(Select())
}
func (r *playlistRepository) Exists(id string) (bool, error) {
return r.exists(Select().Where(Eq{"id": id}))
}
func (r *playlistRepository) Delete(id string) error {
return r.delete(Eq{"id": id})
}
func (r *playlistRepository) Put(p *model.Playlist) error {
if p.ID == "" {
id, _ := uuid.NewRandom()
p.ID = id.String()
}
tp := r.fromModel(p)
err := r.put(p.ID, &tp)
values, _ := toSqlArgs(r.fromModel(p))
update := Update(r.tableName).Where(Eq{"id": p.ID}).SetMap(values)
count, err := r.executeSQL(update)
if err != nil {
return err
}
if count > 0 {
return nil
}
insert := Insert(r.tableName).SetMap(values)
_, err = r.executeSQL(insert)
return err
}
func (r *playlistRepository) Get(id string) (*model.Playlist, error) {
tp := &playlist{ID: id}
err := r.ormer.Read(tp)
if err == orm.ErrNoRows {
return nil, model.ErrNotFound
}
if err != nil {
return nil, err
}
pls := r.toModel(tp)
sel := r.newSelect().Columns("*").Where(Eq{"id": id})
var res playlist
err := r.queryOne(sel, &res)
pls := r.toModel(&res)
return &pls, err
}
@@ -60,35 +76,34 @@ func (r *playlistRepository) GetWithTracks(id string) (*model.Playlist, error) {
if err != nil {
return nil, err
}
qs := r.ormer.QueryTable(&mediaFile{})
mfRepo := NewMediaFileRepository(r.ctx, r.ormer)
pls.Duration = 0
var newTracks model.MediaFiles
for _, t := range pls.Tracks {
mf := &mediaFile{}
if err := qs.Filter("id", t.ID).One(mf); err == nil {
pls.Duration += mf.Duration
newTracks = append(newTracks, model.MediaFile(*mf))
mf, err := mfRepo.Get(t.ID)
if err != nil {
continue
}
pls.Duration += mf.Duration
newTracks = append(newTracks, model.MediaFile(*mf))
}
pls.Tracks = newTracks
return pls, err
}
func (r *playlistRepository) GetAll(options ...model.QueryOptions) (model.Playlists, error) {
var all []playlist
_, err := r.newQuery(options...).All(&all)
if err != nil {
return nil, err
}
return r.toModels(all)
sel := r.newSelect(options...).Columns("*")
var res []playlist
err := r.queryAll(sel, &res)
return r.toModels(res), err
}
func (r *playlistRepository) toModels(all []playlist) (model.Playlists, error) {
func (r *playlistRepository) toModels(all []playlist) model.Playlists {
result := make(model.Playlists, len(all))
for i, p := range all {
result[i] = r.toModel(&p)
}
return result, nil
return result
}
func (r *playlistRepository) toModel(p *playlist) model.Playlist {