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

View file

@ -49,6 +49,6 @@ func (r *shareRepositoryWrapper) Save(entity interface{}) (string, error) {
return id, err return id, err
} }
func (r *shareRepositoryWrapper) Update(entity interface{}, _ ...string) error { func (r *shareRepositoryWrapper) Update(id string, entity interface{}, _ ...string) error {
return r.Persistable.Update(entity, "description") return r.Persistable.Update(id, entity, "description")
} }

View file

@ -41,7 +41,7 @@ var _ = Describe("Share", func() {
Describe("Update", func() { Describe("Update", func() {
It("filters out read-only fields", func() { It("filters out read-only fields", func() {
entity := "entity" entity := "entity"
err := repo.Update(entity) err := repo.Update("id", entity)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(mockedRepo.(*tests.MockShareRepo).Entity).To(Equal("entity")) Expect(mockedRepo.(*tests.MockShareRepo).Entity).To(Equal("entity"))
Expect(mockedRepo.(*tests.MockShareRepo).Cols).To(ConsistOf("description")) Expect(mockedRepo.(*tests.MockShareRepo).Cols).To(ConsistOf("description"))

2
go.mod
View file

@ -10,7 +10,7 @@ require (
github.com/astaxie/beego v1.12.3 github.com/astaxie/beego v1.12.3
github.com/bradleyjkemp/cupaloy v2.3.0+incompatible github.com/bradleyjkemp/cupaloy v2.3.0+incompatible
github.com/cespare/reflex v0.3.1 github.com/cespare/reflex v0.3.1
github.com/deluan/rest v0.0.0-20210503015435-e7091d44f0ba github.com/deluan/rest v0.0.0-20211101235434-380523c4bb47
github.com/denisenkom/go-mssqldb v0.10.0 // indirect github.com/denisenkom/go-mssqldb v0.10.0 // indirect
github.com/dhowden/tag v0.0.0-20200412032933-5d76b8eaae27 github.com/dhowden/tag v0.0.0-20200412032933-5d76b8eaae27
github.com/disintegration/imaging v1.6.2 github.com/disintegration/imaging v1.6.2

4
go.sum
View file

@ -179,8 +179,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= github.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 h1:1iy2qD6JEhHKKhUOA9IWs7mjco7lnw2qx8FsRI2wirE=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.0-20210816181553-5444fa50b93d/go.mod h1:tmAIfUFEirG/Y8jhZ9M+h36obRZAk/1fcSpXwAVlfqE= 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-20211101235434-380523c4bb47 h1:IhGAYGDi212gspq0XkYAI+DN5e9lfAIm8Qgu1wj9yN4=
github.com/deluan/rest v0.0.0-20210503015435-e7091d44f0ba/go.mod h1:tSgDythFsl0QgS/PFWfIZqcJKnkADWneY80jaVRlqK8= 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 h1:jEeSF4sdv8/3cT/WY8AgDHUoItNSoEZ7qg9dX7pc218=
github.com/denis-tingajkin/go-header v0.4.2/go.mod h1:eLRHAVXzE5atsKAnNRDB90WHCFFnBUn4RN0nRcs1LJA= 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= github.com/denisenkom/go-mssqldb v0.10.0 h1:QykgLZBorFE95+gO3u9esLd0BmbvpWp0/waNNZfHBM8=

View file

@ -376,9 +376,10 @@ func (r albumRepository) Save(entity interface{}) (string, error) {
return id, err 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) album := entity.(*model.Album)
_, err := r.put(album.ID, album) album.ID = id
_, err := r.put(id, album, cols...)
return err return err
} }

View file

@ -285,8 +285,9 @@ func (r artistRepository) Save(entity interface{}) (string, error) {
return artist.ID, err 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 := entity.(*model.Artist)
artist.ID = id
return r.Put(artist) return r.Put(artist)
} }

View file

@ -209,8 +209,9 @@ func (r *mediaFileRepository) Save(entity interface{}) (string, error) {
return mf.ID, err 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 := entity.(*model.MediaFile)
mf.ID = id
return r.Put(mf) return r.Put(mf)
} }

View file

@ -108,12 +108,13 @@ func (r *playerRepository) Save(entity interface{}) (string, error) {
return id, err 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 := entity.(*model.Player)
t.ID = id
if !r.isPermitted(t) { if !r.isPermitted(t) {
return rest.ErrPermissionDenied return rest.ErrPermissionDenied
} }
_, err := r.put(t.ID, t) _, err := r.put(id, t, cols...)
if err == model.ErrNotFound { if err == model.ErrNotFound {
return rest.ErrNotFound return rest.ErrNotFound
} }

View file

@ -376,13 +376,19 @@ func (r *playlistRepository) Save(entity interface{}) (string, error) {
return pls.ID, err return pls.ID, err
} }
func (r *playlistRepository) Update(entity interface{}, cols ...string) error { func (r *playlistRepository) Update(id string, entity interface{}, cols ...string) error {
pls := entity.(*model.Playlist) current, err := r.Get(id)
if err != nil {
return err
}
usr := loggedUser(r.ctx) usr := loggedUser(r.ctx)
if !usr.IsAdmin && pls.OwnerID != usr.ID { if !usr.IsAdmin && current.OwnerID != usr.ID {
return rest.ErrPermissionDenied 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 { if err == model.ErrNotFound {
return rest.ErrNotFound return rest.ErrNotFound
} }

View file

@ -46,9 +46,10 @@ func (r *shareRepository) Put(s *model.Share) error {
return err 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) s := entity.(*model.Share)
_, err := r.put(s.ID, s, cols...) s.ID = id
_, err := r.put(id, s, cols...)
if err == model.ErrNotFound { if err == model.ErrNotFound {
return rest.ErrNotFound return rest.ErrNotFound
} }

View file

@ -6,8 +6,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/navidrome/navidrome/utils"
. "github.com/Masterminds/squirrel" . "github.com/Masterminds/squirrel"
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
"github.com/google/uuid" "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 there's an ID, try to update first
if id != "" { if id != "" {
updateValues := map[string]interface{}{} 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 { for k, v := range values {
if len(colsToUpdate) == 0 || utils.StringInSlice(k, colsToUpdate) { if _, found := c2upd[k]; len(c2upd) == 0 || found {
updateValues[k] = v updateValues[k] = v
} }
} }
delete(updateValues, "created_at") delete(updateValues, "created_at")
update := Update(r.tableName).Where(Eq{"id": id}).SetMap(updateValues) update := Update(r.tableName).Where(Eq{"id": id}).SetMap(updateValues)
count, err := r.executeSQL(update) count, err := r.executeSQL(update)
@ -206,7 +211,7 @@ func (r sqlRepository) put(id string, m interface{}, colsToUpdate ...string) (ne
return id, nil 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 == "" { if id == "" {
id = uuid.NewString() id = uuid.NewString()
values["id"] = id values["id"] = id

View file

@ -77,9 +77,10 @@ func (r *transcodingRepository) Save(entity interface{}) (string, error) {
return id, err 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) t := entity.(*model.Transcoding)
_, err := r.put(t.ID, t) t.ID = id
_, err := r.put(id, t)
if err == model.ErrNotFound { if err == model.ErrNotFound {
return rest.ErrNotFound return rest.ErrNotFound
} }

View file

@ -169,8 +169,9 @@ func (r *userRepository) Save(entity interface{}) (string, error) {
return u.ID, err 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 := entity.(*model.User)
u.ID = id
usr := loggedUser(r.ctx) usr := loggedUser(r.ctx)
if !usr.IsAdmin && usr.ID != u.ID { if !usr.IsAdmin && usr.ID != u.ID {
return rest.ErrPermissionDenied return rest.ErrPermissionDenied

View file

@ -11,6 +11,7 @@ type MockShareRepo struct {
rest.Persistable rest.Persistable
Entity interface{} Entity interface{}
ID string
Cols []string Cols []string
Error error Error error
} }
@ -23,10 +24,11 @@ func (m *MockShareRepo) Save(entity interface{}) (string, error) {
return "id", nil 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 { if m.Error != nil {
return m.Error return m.Error
} }
m.ID = id
m.Entity = entity m.Entity = entity
m.Cols = cols m.Cols = cols
return nil return nil