diff --git a/web/pkg/database/models.go b/web/pkg/database/models.go index f0892fb..2294d2c 100644 --- a/web/pkg/database/models.go +++ b/web/pkg/database/models.go @@ -1,5 +1,7 @@ package database +import "time" + type Quality string type Format string @@ -11,33 +13,12 @@ const ( ) type Feed struct { - Id int64 - HashId string - UserId string - URL string - PageSize int - Quality Quality - Format Format -} - -// Query helpers - -type WhereFunc func() (string, interface{}) - -func WithId(id int) WhereFunc { - return func() (string, interface{}) { - return "id", id - } -} - -func WithHashId(hashId string) WhereFunc { - return func() (string, interface{}) { - return "hash_id", hashId - } -} - -func WithUserId(userId string) WhereFunc { - return func() (string, interface{}) { - return "user_id", userId - } + Id int64 + HashId string + UserId string + URL string + PageSize int + Quality Quality + Format Format + LastAccess time.Time } diff --git a/web/pkg/database/pg.go b/web/pkg/database/pg.go index 7e90e71..16b8d28 100644 --- a/web/pkg/database/pg.go +++ b/web/pkg/database/pg.go @@ -1,13 +1,14 @@ package database import ( - "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/proxy" - "github.com/go-pg/pg" - "github.com/go-pg/pg/orm" - "github.com/pkg/errors" "log" "net" "strings" + "time" + + "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/proxy" + "github.com/go-pg/pg" + "github.com/pkg/errors" ) type PgConfig struct { @@ -19,6 +20,7 @@ type PgStorage struct { } func (p *PgStorage) CreateFeed(feed *Feed) error { + feed.LastAccess = time.Now().UTC() _, err := p.db.Model(feed).Insert() if err != nil { return errors.Wrap(err, "failed to create feed") @@ -27,21 +29,17 @@ func (p *PgStorage) CreateFeed(feed *Feed) error { return nil } -func (p *PgStorage) GetFeed(q ...WhereFunc) (out []Feed, err error) { - out = []Feed{} - err = p.db.Model(&out).Apply(whereFunc(q...)).Select() - return -} +func (p *PgStorage) GetFeed(hashId string) (*Feed, error) { + lastAccess := time.Now().UTC() -func whereFunc(where ...WhereFunc) func(*orm.Query) (*orm.Query, error) { - return func(q *orm.Query) (*orm.Query, error) { - for _, fn := range where { - field, value := fn() - q = q.Where(field+" = ?", value) - } + feed := &Feed{} + _, err := p.db.Model(feed). + Set("last_access = ?", lastAccess). + Where("hash_id = ?", hashId). + Returning("*"). + Update() - return q, nil - } + return feed, err } func NewPgStorage(config *PgConfig) (*PgStorage, error) { diff --git a/web/pkg/database/pg_sql.go b/web/pkg/database/pg_sql.go index 543781e..7891206 100644 --- a/web/pkg/database/pg_sql.go +++ b/web/pkg/database/pg_sql.go @@ -16,12 +16,13 @@ $$; CREATE TABLE IF NOT EXISTS feeds ( id BIGSERIAL PRIMARY KEY, - hash_id VARCHAR(12) NOT NULL CHECK (hash_id <> ''), + hash_id VARCHAR(12) NOT NULL CHECK (hash_id <> '') UNIQUE, user_id VARCHAR(32) NULL, url VARCHAR(64) NOT NULL CHECK (url <> ''), page_size INT NOT NULL DEFAULT 50, quality quality NOT NULL DEFAULT 'high', - format format NOT NULL DEFAULT 'video' + format format NOT NULL DEFAULT 'video', + last_access timestamp WITHOUT TIME ZONE NOT NULL ); COMMIT; diff --git a/web/pkg/database/pg_test.go b/web/pkg/database/pg_test.go index 7c6ef23..aab017b 100644 --- a/web/pkg/database/pg_test.go +++ b/web/pkg/database/pg_test.go @@ -1,8 +1,9 @@ package database import ( - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func TestCreate(t *testing.T) { @@ -27,15 +28,43 @@ func TestGetFeed(t *testing.T) { client := createClient(t) client.CreateFeed(feed) - out, err := client.GetFeed(WithUserId("123")) + out, err := client.GetFeed("xyz") require.NoError(t, err) - require.Equal(t, 1, len(out)) - require.Equal(t, feed.Id, out[0].Id) + require.Equal(t, feed.Id, out.Id) +} - out, err = client.GetFeed(WithHashId("xyz")) +func TestUpdateLastAccess(t *testing.T) { + feed := &Feed{ + HashId: "xyz", + UserId: "123", + URL: "http://youtube.com", + } + + client := createClient(t) + err := client.CreateFeed(feed) require.NoError(t, err) - require.Equal(t, 1, len(out)) - require.Equal(t, feed.Id, out[0].Id) + + lastAccess := feed.LastAccess + require.True(t, lastAccess.Unix() > 0) + + last, err := client.GetFeed("xyz") + require.NoError(t, err) + + require.NotEmpty(t, last.HashId) + require.NotEmpty(t, last.UserId) + require.NotEmpty(t, last.URL) + + require.True(t, last.LastAccess.Unix() > lastAccess.Unix()) +} + +func TestUniqueHashId(t *testing.T) { + client := createClient(t) + + err := client.CreateFeed(&Feed{HashId: "xyz", URL: "url"}) + require.NoError(t, err) + + err = client.CreateFeed(&Feed{HashId: "xyz", URL: "url"}) + require.Error(t, err) } const TestDatabaseConnectionUrl = "postgres://postgres:@localhost/podsync?sslmode=disable"