Use new rest lib (Update receives all columns that need to be updated)

This commit is contained in:
Deluan
2021-11-01 13:55:47 -04:00
parent b2acec0a09
commit 778f474d26
14 changed files with 46 additions and 26 deletions
+3 -2
View File
@@ -376,9 +376,10 @@ func (r albumRepository) Save(entity interface{}) (string, error) {
return id, err
}
func (r albumRepository) Update(entity interface{}, cols ...string) error {
func (r albumRepository) Update(id string, entity interface{}, cols ...string) error {
album := entity.(*model.Album)
_, err := r.put(album.ID, album)
album.ID = id
_, err := r.put(id, album, cols...)
return err
}
+2 -1
View File
@@ -285,8 +285,9 @@ func (r artistRepository) Save(entity interface{}) (string, error) {
return artist.ID, err
}
func (r artistRepository) Update(entity interface{}, cols ...string) error {
func (r artistRepository) Update(id string, entity interface{}, cols ...string) error {
artist := entity.(*model.Artist)
artist.ID = id
return r.Put(artist)
}
+2 -1
View File
@@ -209,8 +209,9 @@ func (r *mediaFileRepository) Save(entity interface{}) (string, error) {
return mf.ID, err
}
func (r *mediaFileRepository) Update(entity interface{}, cols ...string) error {
func (r *mediaFileRepository) Update(id string, entity interface{}, cols ...string) error {
mf := entity.(*model.MediaFile)
mf.ID = id
return r.Put(mf)
}
+3 -2
View File
@@ -108,12 +108,13 @@ func (r *playerRepository) Save(entity interface{}) (string, error) {
return id, err
}
func (r *playerRepository) Update(entity interface{}, cols ...string) error {
func (r *playerRepository) Update(id string, entity interface{}, cols ...string) error {
t := entity.(*model.Player)
t.ID = id
if !r.isPermitted(t) {
return rest.ErrPermissionDenied
}
_, err := r.put(t.ID, t)
_, err := r.put(id, t, cols...)
if err == model.ErrNotFound {
return rest.ErrNotFound
}
+10 -4
View File
@@ -376,13 +376,19 @@ func (r *playlistRepository) Save(entity interface{}) (string, error) {
return pls.ID, err
}
func (r *playlistRepository) Update(entity interface{}, cols ...string) error {
pls := entity.(*model.Playlist)
func (r *playlistRepository) Update(id string, entity interface{}, cols ...string) error {
current, err := r.Get(id)
if err != nil {
return err
}
usr := loggedUser(r.ctx)
if !usr.IsAdmin && pls.OwnerID != usr.ID {
if !usr.IsAdmin && current.OwnerID != usr.ID {
return rest.ErrPermissionDenied
}
err := r.Put(pls)
pls := entity.(*model.Playlist)
pls.ID = id
pls.UpdatedAt = time.Now()
_, err = r.put(id, pls, append(cols, "updatedAt")...)
if err == model.ErrNotFound {
return rest.ErrNotFound
}
+3 -2
View File
@@ -46,9 +46,10 @@ func (r *shareRepository) Put(s *model.Share) error {
return err
}
func (r *shareRepository) Update(entity interface{}, cols ...string) error {
func (r *shareRepository) Update(id string, entity interface{}, cols ...string) error {
s := entity.(*model.Share)
_, err := r.put(s.ID, s, cols...)
s.ID = id
_, err := r.put(id, s, cols...)
if err == model.ErrNotFound {
return rest.ErrNotFound
}
+9 -4
View File
@@ -6,8 +6,6 @@ import (
"strings"
"time"
"github.com/navidrome/navidrome/utils"
. "github.com/Masterminds/squirrel"
"github.com/astaxie/beego/orm"
"github.com/google/uuid"
@@ -191,11 +189,18 @@ func (r sqlRepository) put(id string, m interface{}, colsToUpdate ...string) (ne
// If there's an ID, try to update first
if id != "" {
updateValues := map[string]interface{}{}
// This is a map of the columns that need to be updated, if specified
c2upd := map[string]struct{}{}
for _, c := range colsToUpdate {
c2upd[toSnakeCase(c)] = struct{}{}
}
for k, v := range values {
if len(colsToUpdate) == 0 || utils.StringInSlice(k, colsToUpdate) {
if _, found := c2upd[k]; len(c2upd) == 0 || found {
updateValues[k] = v
}
}
delete(updateValues, "created_at")
update := Update(r.tableName).Where(Eq{"id": id}).SetMap(updateValues)
count, err := r.executeSQL(update)
@@ -206,7 +211,7 @@ func (r sqlRepository) put(id string, m interface{}, colsToUpdate ...string) (ne
return id, nil
}
}
// If does not have an ID OR the ID was not found (when it is a new record with predefined id)
// If it does not have an ID OR the ID was not found (when it is a new record with predefined id)
if id == "" {
id = uuid.NewString()
values["id"] = id
+3 -2
View File
@@ -77,9 +77,10 @@ func (r *transcodingRepository) Save(entity interface{}) (string, error) {
return id, err
}
func (r *transcodingRepository) Update(entity interface{}, cols ...string) error {
func (r *transcodingRepository) Update(id string, entity interface{}, cols ...string) error {
t := entity.(*model.Transcoding)
_, err := r.put(t.ID, t)
t.ID = id
_, err := r.put(id, t)
if err == model.ErrNotFound {
return rest.ErrNotFound
}
+2 -1
View File
@@ -169,8 +169,9 @@ func (r *userRepository) Save(entity interface{}) (string, error) {
return u.ID, err
}
func (r *userRepository) Update(entity interface{}, cols ...string) error {
func (r *userRepository) Update(id string, entity interface{}, cols ...string) error {
u := entity.(*model.User)
u.ID = id
usr := loggedUser(r.ctx)
if !usr.IsAdmin && usr.ID != u.ID {
return rest.ErrPermissionDenied