feat: implement feed agg and browse
This commit is contained in:
@@ -7,31 +7,34 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createFeed = `-- name: CreateFeed :one
|
||||
INSERT INTO feeds (id, created_at, updated_at, name, url, user_id)
|
||||
INSERT INTO feeds (id, created_at, updated_at, name, url, user_id, last_fetched_at)
|
||||
VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6
|
||||
$6,
|
||||
$7
|
||||
)
|
||||
RETURNING id, created_at, updated_at, name, url, user_id
|
||||
RETURNING id, created_at, updated_at, name, url, user_id, last_fetched_at
|
||||
`
|
||||
|
||||
type CreateFeedParams struct {
|
||||
ID uuid.UUID
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
Name string
|
||||
Url string
|
||||
UserID uuid.UUID
|
||||
ID uuid.UUID
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
Name string
|
||||
Url string
|
||||
UserID uuid.UUID
|
||||
LastFetchedAt sql.NullTime
|
||||
}
|
||||
|
||||
func (q *Queries) CreateFeed(ctx context.Context, arg CreateFeedParams) (Feed, error) {
|
||||
@@ -42,6 +45,7 @@ func (q *Queries) CreateFeed(ctx context.Context, arg CreateFeedParams) (Feed, e
|
||||
arg.Name,
|
||||
arg.Url,
|
||||
arg.UserID,
|
||||
arg.LastFetchedAt,
|
||||
)
|
||||
var i Feed
|
||||
err := row.Scan(
|
||||
@@ -51,12 +55,13 @@ func (q *Queries) CreateFeed(ctx context.Context, arg CreateFeedParams) (Feed, e
|
||||
&i.Name,
|
||||
&i.Url,
|
||||
&i.UserID,
|
||||
&i.LastFetchedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getAllFeeds = `-- name: GetAllFeeds :many
|
||||
SELECT id, created_at, updated_at, name, url, user_id FROM feeds
|
||||
SELECT id, created_at, updated_at, name, url, user_id, last_fetched_at FROM feeds
|
||||
`
|
||||
|
||||
func (q *Queries) GetAllFeeds(ctx context.Context) ([]Feed, error) {
|
||||
@@ -75,6 +80,7 @@ func (q *Queries) GetAllFeeds(ctx context.Context) ([]Feed, error) {
|
||||
&i.Name,
|
||||
&i.Url,
|
||||
&i.UserID,
|
||||
&i.LastFetchedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -90,7 +96,7 @@ func (q *Queries) GetAllFeeds(ctx context.Context) ([]Feed, error) {
|
||||
}
|
||||
|
||||
const getFeedByUrl = `-- name: GetFeedByUrl :one
|
||||
SELECT id, created_at, updated_at, name, url, user_id FROM feeds
|
||||
SELECT id, created_at, updated_at, name, url, user_id, last_fetched_at FROM feeds
|
||||
WHERE url = $1
|
||||
`
|
||||
|
||||
@@ -104,6 +110,43 @@ func (q *Queries) GetFeedByUrl(ctx context.Context, url string) (Feed, error) {
|
||||
&i.Name,
|
||||
&i.Url,
|
||||
&i.UserID,
|
||||
&i.LastFetchedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getNextFeedToFetch = `-- name: GetNextFeedToFetch :one
|
||||
SELECT id, created_at, updated_at, name, url, user_id, last_fetched_at FROM feeds
|
||||
ORDER BY last_fetched_at ASC NULLS FIRST
|
||||
`
|
||||
|
||||
func (q *Queries) GetNextFeedToFetch(ctx context.Context) (Feed, error) {
|
||||
row := q.db.QueryRowContext(ctx, getNextFeedToFetch)
|
||||
var i Feed
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Name,
|
||||
&i.Url,
|
||||
&i.UserID,
|
||||
&i.LastFetchedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const markFeedAsFetched = `-- name: MarkFeedAsFetched :exec
|
||||
UPDATE feeds
|
||||
SET last_fetched_at = $1
|
||||
WHERE feeds.id = $2
|
||||
`
|
||||
|
||||
type MarkFeedAsFetchedParams struct {
|
||||
LastFetchedAt sql.NullTime
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) MarkFeedAsFetched(ctx context.Context, arg MarkFeedAsFetchedParams) error {
|
||||
_, err := q.db.ExecContext(ctx, markFeedAsFetched, arg.LastFetchedAt, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user