1
0
mirror of https://github.com/mxpv/podsync.git synced 2024-05-11 05:55:04 +00:00

Add last access time for feeds

This commit is contained in:
Maksym Pavlenko
2017-07-31 21:51:21 -07:00
parent 36557d2de5
commit a1cc468f10
4 changed files with 64 additions and 55 deletions

View File

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

View File

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

View File

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

View File

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