🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Use new `rest` lib (Update receives all columns that need to be updated)
Deluan778f474d2630
diff --git a/core/share.go b/core/share.goindex 68ee47db5..2bba5fb7c 100644--- a/core/share.go+++ b/core/share.go@@ -49,6 +49,6 @@ func (r *shareRepositoryWrapper) Save(entity interface{}) (string, error) {return id, err}-func (r *shareRepositoryWrapper) Update(entity interface{}, _ ...string) error {- return r.Persistable.Update(entity, "description")+func (r *shareRepositoryWrapper) Update(id string, entity interface{}, _ ...string) error {+ return r.Persistable.Update(id, entity, "description")}diff --git a/core/share_test.go b/core/share_test.goindex 4c829fb02..98f51fee6 100644--- a/core/share_test.go+++ b/core/share_test.go@@ -41,7 +41,7 @@ var _ = Describe("Share", func() {Describe("Update", func() {It("filters out read-only fields", func() {entity := "entity"- err := repo.Update(entity)+ err := repo.Update("id", entity)Expect(err).ToNot(HaveOccurred())Expect(mockedRepo.(*tests.MockShareRepo).Entity).To(Equal("entity"))Expect(mockedRepo.(*tests.MockShareRepo).Cols).To(ConsistOf("description"))diff --git a/go.mod b/go.modindex 122903338..aac9bca33 100644--- a/go.mod+++ b/go.mod@@ -10,7 +10,7 @@ require (github.com/astaxie/beego v1.12.3github.com/bradleyjkemp/cupaloy v2.3.0+incompatiblegithub.com/cespare/reflex v0.3.1- github.com/deluan/rest v0.0.0-20210503015435-e7091d44f0ba+ github.com/deluan/rest v0.0.0-20211101235434-380523c4bb47github.com/denisenkom/go-mssqldb v0.10.0 // indirectgithub.com/dhowden/tag v0.0.0-20200412032933-5d76b8eaae27github.com/disintegration/imaging v1.6.2diff --git a/go.sum b/go.sumindex aa141d4c7..3e9646997 100644--- a/go.sum+++ b/go.sum@@ -179,8 +179,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsgithub.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.0-20210816181553-5444fa50b93d h1:1iy2qD6JEhHKKhUOA9IWs7mjco7lnw2qx8FsRI2wirE=github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.0-20210816181553-5444fa50b93d/go.mod h1:tmAIfUFEirG/Y8jhZ9M+h36obRZAk/1fcSpXwAVlfqE=-github.com/deluan/rest v0.0.0-20210503015435-e7091d44f0ba h1:soWusRLgeSlkpw7rWUVrNd9COIVmazDBWfyK0HAcVPU=-github.com/deluan/rest v0.0.0-20210503015435-e7091d44f0ba/go.mod h1:tSgDythFsl0QgS/PFWfIZqcJKnkADWneY80jaVRlqK8=+github.com/deluan/rest v0.0.0-20211101235434-380523c4bb47 h1:IhGAYGDi212gspq0XkYAI+DN5e9lfAIm8Qgu1wj9yN4=+github.com/deluan/rest v0.0.0-20211101235434-380523c4bb47/go.mod h1:tSgDythFsl0QgS/PFWfIZqcJKnkADWneY80jaVRlqK8=github.com/denis-tingajkin/go-header v0.4.2 h1:jEeSF4sdv8/3cT/WY8AgDHUoItNSoEZ7qg9dX7pc218=github.com/denis-tingajkin/go-header v0.4.2/go.mod h1:eLRHAVXzE5atsKAnNRDB90WHCFFnBUn4RN0nRcs1LJA=github.com/denisenkom/go-mssqldb v0.10.0 h1:QykgLZBorFE95+gO3u9esLd0BmbvpWp0/waNNZfHBM8=diff --git a/persistence/album_repository.go b/persistence/album_repository.goindex 5e63972ec..2b3b28945 100644--- a/persistence/album_repository.go+++ b/persistence/album_repository.go@@ -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}diff --git a/persistence/artist_repository.go b/persistence/artist_repository.goindex 4e433d851..38711dd7d 100644--- a/persistence/artist_repository.go+++ b/persistence/artist_repository.go@@ -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 = idreturn r.Put(artist)}diff --git a/persistence/mediafile_repository.go b/persistence/mediafile_repository.goindex a044a3d58..3fdf4ae8f 100644--- a/persistence/mediafile_repository.go+++ b/persistence/mediafile_repository.go@@ -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 = idreturn r.Put(mf)}diff --git a/persistence/player_repository.go b/persistence/player_repository.goindex 8637f3419..d0695eb1f 100644--- a/persistence/player_repository.go+++ b/persistence/player_repository.go@@ -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 = idif !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}diff --git a/persistence/playlist_repository.go b/persistence/playlist_repository.goindex aa832f5d2..1a92b3a5c 100644--- a/persistence/playlist_repository.go+++ b/persistence/playlist_repository.go@@ -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}diff --git a/persistence/share_repository.go b/persistence/share_repository.goindex a47c2dafe..9d694ae4c 100644--- a/persistence/share_repository.go+++ b/persistence/share_repository.go@@ -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}diff --git a/persistence/sql_base_repository.go b/persistence/sql_base_repository.goindex 8ac2d43f5..b3b8cc43b 100644--- a/persistence/sql_base_repository.go+++ b/persistence/sql_base_repository.go@@ -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 firstif 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) (nereturn 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"] = iddiff --git a/persistence/transcoding_repository.go b/persistence/transcoding_repository.goindex 03dc78412..5bcb5128e 100644--- a/persistence/transcoding_repository.go+++ b/persistence/transcoding_repository.go@@ -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}diff --git a/persistence/user_repository.go b/persistence/user_repository.goindex ca6f9823a..acac955d0 100644--- a/persistence/user_repository.go+++ b/persistence/user_repository.go@@ -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 = idusr := loggedUser(r.ctx)if !usr.IsAdmin && usr.ID != u.ID {return rest.ErrPermissionDenieddiff --git a/tests/mock_share_repo.go b/tests/mock_share_repo.goindex e78150f76..6e92329cd 100644--- a/tests/mock_share_repo.go+++ b/tests/mock_share_repo.go@@ -11,6 +11,7 @@ type MockShareRepo struct {rest.PersistableEntity interface{}+ ID stringCols []stringError error}@@ -23,10 +24,11 @@ func (m *MockShareRepo) Save(entity interface{}) (string, error) {return "id", nil}-func (m *MockShareRepo) Update(entity interface{}, cols ...string) error {+func (m *MockShareRepo) Update(id string, entity interface{}, cols ...string) error {if m.Error != nil {return m.Error}+ m.ID = idm.Entity = entitym.Cols = colsreturn nil