Create share table and repository. (#930)

* Add share table and repository

* Add datastore mock

* Try fixing indent

* Try fixing indent - 2

* Try fixing indent - 3

* Implement rest.Repository and rest.Persistance

* Renew date

* Better error handling

* Improve field name

* Fix json name conventionally
This commit is contained in:
Yash Jipkate 2021-05-30 21:20:35 +05:30 committed by GitHub
parent 675cbe11b3
commit 327c259a3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 168 additions and 0 deletions

View File

@ -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
}

View File

@ -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

24
model/share.go Normal file
View File

@ -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)
}

View File

@ -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())
}

View File

@ -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)

View File

@ -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()