Add owner_id to playlist

This commit is contained in:
Deluan
2021-10-29 22:55:28 -04:00
committed by Deluan Quintão
parent 84bbcdbfc2
commit 133fed344f
38 changed files with 173 additions and 100 deletions
+11 -3
View File
@@ -85,7 +85,14 @@ var _ = Describe("Initialize test DB", func() {
BeforeSuite(func() {
o := orm.NewOrm()
ctx := log.NewContext(context.TODO())
ctx = request.WithUser(ctx, model.User{ID: "userid", UserName: "userid"})
user := model.User{ID: "userid", UserName: "userid"}
ctx = request.WithUser(ctx, user)
ur := NewUserRepository(ctx, o)
err := ur.Put(&user)
if err != nil {
panic(err)
}
gr := NewGenreRepository(ctx, o)
for i := range testGenres {
@@ -126,12 +133,13 @@ var _ = Describe("Initialize test DB", func() {
plsBest = model.Playlist{
Name: "Best",
Comment: "No Comments",
Owner: "userid",
OwnerID: "userid",
OwnerName: "userid",
Public: true,
SongCount: 2,
}
plsBest.AddTracks([]string{"1001", "1003"})
plsCool = model.Playlist{Name: "Cool", Owner: "userid"}
plsCool = model.Playlist{Name: "Cool", OwnerID: "userid", OwnerName: "userid"}
plsCool.AddTracks([]string{"1004"})
testPlaylists = []*model.Playlist{&plsBest, &plsCool}
+16 -10
View File
@@ -50,7 +50,7 @@ func (r *playlistRepository) userFilter() Sqlizer {
}
return Or{
Eq{"public": true},
Eq{"owner": user.UserName},
Eq{"owner_id": user.ID},
}
}
@@ -70,7 +70,7 @@ func (r *playlistRepository) Delete(id string) error {
if err != nil {
return err
}
if pls.Owner != usr.UserName {
if pls.OwnerID != usr.ID {
return rest.ErrPermissionDenied
}
}
@@ -117,11 +117,11 @@ func (r *playlistRepository) Put(p *model.Playlist) error {
}
func (r *playlistRepository) Get(id string) (*model.Playlist, error) {
return r.findBy(And{Eq{"id": id}, r.userFilter()})
return r.findBy(And{Eq{"playlist.id": id}, r.userFilter()})
}
func (r *playlistRepository) GetWithTracks(id string) (*model.Playlist, error) {
pls, err := r.findBy(And{Eq{"id": id}, r.userFilter()})
pls, err := r.Get(id)
if err != nil {
return nil, err
}
@@ -140,7 +140,7 @@ func (r *playlistRepository) FindByPath(path string) (*model.Playlist, error) {
}
func (r *playlistRepository) findBy(sql Sqlizer) (*model.Playlist, error) {
sel := r.newSelect().Columns("*").Where(sql)
sel := r.selectPlaylist().Where(sql)
var pls []dbPlaylist
err := r.queryAll(sel, &pls)
if err != nil {
@@ -169,7 +169,7 @@ func (r *playlistRepository) toModel(pls dbPlaylist) (*model.Playlist, error) {
}
func (r *playlistRepository) GetAll(options ...model.QueryOptions) (model.Playlists, error) {
sel := r.newSelect(options...).Columns("*").Where(r.userFilter())
sel := r.selectPlaylist(options...).Where(r.userFilter())
var res []dbPlaylist
err := r.queryAll(sel, &res)
if err != nil {
@@ -186,6 +186,11 @@ func (r *playlistRepository) GetAll(options ...model.QueryOptions) (model.Playli
return playlists, err
}
func (r *playlistRepository) selectPlaylist(options ...model.QueryOptions) SelectBuilder {
return r.newSelect(options...).Join("user on user.id = owner_id").
Columns(r.tableName+".*", "user.user_name as owner_name")
}
func (r *playlistRepository) refreshSmartPlaylist(pls *model.Playlist) bool {
// Only refresh if it is a smart playlist and was not refreshed in the last 5 seconds
if !pls.IsSmartPlaylist() || time.Since(pls.EvaluatedAt) < 5*time.Second {
@@ -194,7 +199,7 @@ func (r *playlistRepository) refreshSmartPlaylist(pls *model.Playlist) bool {
// Never refresh other users' playlists
usr := loggedUser(r.ctx)
if pls.Owner != usr.UserName {
if pls.OwnerID != usr.ID {
return false
}
@@ -362,7 +367,8 @@ func (r *playlistRepository) NewInstance() interface{} {
func (r *playlistRepository) Save(entity interface{}) (string, error) {
pls := entity.(*model.Playlist)
pls.Owner = loggedUser(r.ctx).UserName
pls.OwnerID = loggedUser(r.ctx).ID
pls.ID = "" // Make sure we don't override an existing playlist
err := r.Put(pls)
if err != nil {
return "", err
@@ -373,7 +379,7 @@ func (r *playlistRepository) Save(entity interface{}) (string, error) {
func (r *playlistRepository) Update(entity interface{}, cols ...string) error {
pls := entity.(*model.Playlist)
usr := loggedUser(r.ctx)
if !usr.IsAdmin && pls.Owner != usr.UserName {
if !usr.IsAdmin && pls.OwnerID != usr.ID {
return rest.ErrPermissionDenied
}
err := r.Put(pls)
@@ -432,7 +438,7 @@ func (r *playlistRepository) isWritable(playlistId string) bool {
return true
}
pls, err := r.Get(playlistId)
return err == nil && pls.Owner == usr.UserName
return err == nil && pls.OwnerID == usr.ID
}
var _ model.PlaylistRepository = (*playlistRepository)(nil)
+1 -1
View File
@@ -76,7 +76,7 @@ var _ = Describe("PlaylistRepository", func() {
It("Put/Exists/Delete", func() {
By("saves the playlist to the DB")
newPls := model.Playlist{Name: "Great!", Owner: "userid"}
newPls := model.Playlist{Name: "Great!", OwnerID: "userid"}
newPls.AddTracks([]string{"1004", "1003"})
By("saves the playlist to the DB")