Skip to content

🎧 Your Personal Streaming Service

Create share table and repository. (#930)

Yash Jipkate327c259a3d80
diff --git a/db/migration/20210530121921_create_shares_table.go b/db/migration/20210530121921_create_shares_table.go
new file mode 100644
index 000000000..6fe169148
--- /dev/null
+++ b/db/migration/20210530121921_create_shares_table.go
@@ -0,0 +1,34 @@
+package migrations
+
+import (
+ "database/sql"
+
+ "github.com/pressly/goose"
+)
+
+func init() {
+ goose.AddMigration(upCreateSharesTable, downCreateSharesTable)
+}
+
+func upCreateSharesTable(tx *sql.Tx) error {
+ _, err := tx.Exec(`
+create table share
+(
+ id varchar(255) not null primary key,
+ name varchar(255) not null unique,
+ description varchar(255),
+ expires datetime,
+ created datetime,
+ last_visited datetime,
+ resource_ids varchar not null,
+ resource_type varchar(255) not null,
+ visit_count integer default 0
+);
+`)
+
+ return err
+}
+
+func downCreateSharesTable(tx *sql.Tx) error {
+ return nil
+}
diff --git a/model/datastore.go b/model/datastore.go
index 136083b53..d4c8f959c 100644
--- a/model/datastore.go
+++ b/model/datastore.go
@@ -28,6 +28,7 @@ type DataStore interface {
Playlist(ctx context.Context) PlaylistRepository
PlayQueue(ctx context.Context) PlayQueueRepository
Property(ctx context.Context) PropertyRepository
+ Share(ctx context.Context) ShareRepository
User(ctx context.Context) UserRepository
Transcoding(ctx context.Context) TranscodingRepository
Player(ctx context.Context) PlayerRepository
diff --git a/model/share.go b/model/share.go
new file mode 100644
index 000000000..ecc7f9537
--- /dev/null
+++ b/model/share.go
@@ -0,0 +1,24 @@
+package model
+
+import (
+ "time"
+)
+
+type Share struct {
+ ID string `json:"id" orm:"column(id)"`
+ Name string `json:"name"`
+ Description string `json:"description"`
+ ExpiresAt time.Time `json:"expiresAt"`
+ CreatedAt time.Time `json:"createdAt"`
+ LastVisitedAt time.Time `json:"lastVisitedAt"`
+ ResourceIDs string `json:"resourceIds" orm:"column(resource_ids)"`
+ ResourceType string `json:"resourceType"`
+ VisitCount string `json:"visitCount"`
+}
+
+type Shares []Share
+
+type ShareRepository interface {
+ Put(s *Share) error
+ GetAll(options ...QueryOptions) (Shares, error)
+}
diff --git a/persistence/persistence.go b/persistence/persistence.go
index e66da1370..964ad51d5 100644
--- a/persistence/persistence.go
+++ b/persistence/persistence.go
@@ -50,6 +50,10 @@ func (s *SQLStore) Property(ctx context.Context) model.PropertyRepository {
return NewPropertyRepository(ctx, s.getOrmer())
}
+func (s *SQLStore) Share(ctx context.Context) model.ShareRepository {
+ return NewShareRepository(ctx, s.getOrmer())
+}
+
func (s *SQLStore) User(ctx context.Context) model.UserRepository {
return NewUserRepository(ctx, s.getOrmer())
}
diff --git a/persistence/share_repository.go b/persistence/share_repository.go
new file mode 100644
index 000000000..795257b1f
--- /dev/null
+++ b/persistence/share_repository.go
@@ -0,0 +1,100 @@
+package persistence
+
+import (
+ "context"
+
+ . "github.com/Masterminds/squirrel"
+ "github.com/astaxie/beego/orm"
+ "github.com/deluan/rest"
+ "github.com/navidrome/navidrome/model"
+)
+
+type shareRepository struct {
+ sqlRepository
+ sqlRestful
+}
+
+func NewShareRepository(ctx context.Context, o orm.Ormer) model.ShareRepository {
+ r := &shareRepository{}
+ r.ctx = ctx
+ r.ormer = o
+ r.tableName = "share"
+ return r
+}
+
+func (r *shareRepository) Delete(id string) error {
+ err := r.delete(Eq{"id": id})
+ if err == model.ErrNotFound {
+ return rest.ErrNotFound
+ }
+ return err
+}
+
+func (r *shareRepository) selectShare(options ...model.QueryOptions) SelectBuilder {
+ return r.newSelect(options...).Columns("*")
+}
+
+func (r *shareRepository) GetAll(options ...model.QueryOptions) (model.Shares, error) {
+ sq := r.selectShare(options...)
+ res := model.Shares{}
+ err := r.queryAll(sq, &res)
+ return res, err
+}
+
+func (r *shareRepository) Put(s *model.Share) error {
+ _, err := r.put(s.ID, s)
+ return err
+}
+
+func (r *shareRepository) Update(entity interface{}, cols ...string) error {
+ s := entity.(*model.Share)
+ _, err := r.put(s.ID, s)
+ if err == model.ErrNotFound {
+ return rest.ErrNotFound
+ }
+ return err
+}
+
+func (r *shareRepository) Save(entity interface{}) (string, error) {
+ s := entity.(*model.Share)
+ id, err := r.put(s.ID, s)
+ if err == model.ErrNotFound {
+ return "", rest.ErrNotFound
+ }
+ return id, err
+}
+
+func (r *shareRepository) CountAll(options ...model.QueryOptions) (int64, error) {
+ return r.count(r.selectShare(), options...)
+}
+
+func (r *shareRepository) Count(options ...rest.QueryOptions) (int64, error) {
+ return r.CountAll(r.parseRestOptions(options...))
+}
+
+func (r *shareRepository) EntityName() string {
+ return "share"
+}
+
+func (r *shareRepository) NewInstance() interface{} {
+ return &model.Share{}
+}
+
+func (r *shareRepository) Get(id string) (*model.Share, error) {
+ sel := r.newSelect().Columns("*").Where(Eq{"id": id})
+ var res model.Share
+ err := r.queryOne(sel, &res)
+ return &res, err
+}
+
+func (r *shareRepository) Read(id string) (interface{}, error) {
+ return r.Get(id)
+}
+
+func (r *shareRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
+ return r.GetAll(r.parseRestOptions(options...))
+}
+
+var _ model.ShareRepository = (*shareRepository)(nil)
+var _ rest.Repository = (*shareRepository)(nil)
+var _ rest.Persistable = (*shareRepository)(nil)
diff --git a/tests/mock_persistence.go b/tests/mock_persistence.go
index d877ef4c5..c7ca758a6 100644
--- a/tests/mock_persistence.go
+++ b/tests/mock_persistence.go
@@ -14,6 +14,7 @@ type MockDataStore struct {
MockedUser model.UserRepository
MockedProperty model.PropertyRepository
MockedPlayer model.PlayerRepository
+ MockedShare model.ShareRepository
MockedTranscoding model.TranscodingRepository
}
@@ -64,6 +65,10 @@ func (db *MockDataStore) Property(context.Context) model.PropertyRepository {
return db.MockedProperty
}
+func (db *MockDataStore) Share(context.Context) model.ShareRepository {
+ return struct{ model.ShareRepository }{}
+}
+
func (db *MockDataStore) User(context.Context) model.UserRepository {
if db.MockedUser == nil {
db.MockedUser = CreateMockUserRepo()