Add getShares and createShare Subsonic endpoints

This commit is contained in:
Deluan
2023-01-22 14:38:55 -05:00
parent 94cc2b2ac5
commit d0dceae094
21 changed files with 257 additions and 56 deletions
+20 -15
View File
@@ -55,16 +55,7 @@ func (s *shareService) Load(ctx context.Context, id string) (*model.Share, error
if err != nil {
return nil, err
}
share.Tracks = slice.Map(mfs, func(mf model.MediaFile) model.ShareTrack {
return model.ShareTrack{
ID: mf.ID,
Title: mf.Title,
Artist: mf.Artist,
Album: mf.Album,
Duration: mf.Duration,
UpdatedAt: mf.UpdatedAt,
}
})
share.Tracks = mfs
return entity.(*model.Share), nil
}
@@ -129,12 +120,26 @@ func (r *shareRepositoryWrapper) Save(entity interface{}) (string, error) {
if s.ExpiresAt.IsZero() {
s.ExpiresAt = time.Now().Add(365 * 24 * time.Hour)
}
switch s.ResourceType {
case "album":
s.Contents = r.shareContentsFromAlbums(s.ID, s.ResourceIDs)
case "playlist":
s.Contents = r.shareContentsFromPlaylist(s.ID, s.ResourceIDs)
// TODO Validate all ids
firstId := strings.SplitN(s.ResourceIDs, ",", 1)[0]
v, err := model.GetEntityByID(r.ctx, r.ds, firstId)
if err != nil {
return "", err
}
switch v.(type) {
case *model.Album:
s.ResourceType = "album"
s.Contents = r.shareContentsFromAlbums(s.ID, s.ResourceIDs)
case *model.Playlist:
s.ResourceType = "playlist"
s.Contents = r.shareContentsFromPlaylist(s.ID, s.ResourceIDs)
case *model.Artist:
s.ResourceType = "artist"
case *model.MediaFile:
s.ResourceType = "song"
}
id, err = r.Persistable.Save(s)
return id, err
}
+5 -3
View File
@@ -14,10 +14,11 @@ var _ = Describe("Share", func() {
var ds model.DataStore
var share Share
var mockedRepo rest.Persistable
ctx := context.Background()
BeforeEach(func() {
ds = &tests.MockDataStore{}
mockedRepo = ds.Share(context.Background()).(rest.Persistable)
mockedRepo = ds.Share(ctx).(rest.Persistable)
share = NewShare(ds)
})
@@ -25,12 +26,13 @@ var _ = Describe("Share", func() {
var repo rest.Persistable
BeforeEach(func() {
repo = share.NewRepository(context.Background()).(rest.Persistable)
repo = share.NewRepository(ctx).(rest.Persistable)
_ = ds.Album(ctx).Put(&model.Album{ID: "123", Name: "Album"})
})
Describe("Save", func() {
It("it sets a random ID", func() {
entity := &model.Share{Description: "test"}
entity := &model.Share{Description: "test", ResourceIDs: "123"}
id, err := repo.Save(entity)
Expect(err).ToNot(HaveOccurred())
Expect(id).ToNot(BeEmpty())