[server][core] List Bookmarks, remove incremental query

This commit is contained in:
Abhishek Shroff
2025-03-31 01:16:57 +05:30
parent 2508267a64
commit 2d4c60780f
6 changed files with 30 additions and 77 deletions

View File

@@ -1,7 +1,6 @@
package my
import (
"strconv"
"time"
"github.com/gin-gonic/gin"
@@ -23,18 +22,8 @@ func setupBookmarksRoutes(r *gin.RouterGroup) {
}
func handleBookmarksGetRoute(c *gin.Context) {
var since *time.Time
sinceStr := c.Query("since")
if sinceStr != "" {
sinceInt, err := strconv.ParseInt(sinceStr, 10, 64)
if err != nil {
panic(err)
}
t := time.UnixMilli(sinceInt - 1000).UTC()
since = &t
}
u := auth.GetUser(c)
bookmarks, err := user.CreateManager(c.Request.Context()).ListBookmarks(u, since)
bookmarks, err := user.CreateManager(c.Request.Context()).ListBookmarks(u)
if err != nil {
panic(err)
}

View File

@@ -32,12 +32,12 @@ func setupBookmarksListCommand() *cobra.Command {
Short: "List Bookmarks",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if bookmarks, err := user.CreateManager(context.Background()).ListBookmarks(common.User(cmd), nil); err != nil {
if bookmarks, err := user.CreateManager(context.Background()).ListBookmarks(common.User(cmd)); err != nil {
fmt.Println("unable to list bookmark: " + err.Error())
os.Exit(1)
} else {
for _, b := range bookmarks {
fmt.Printf("%s %s %t\n", b.ResourceID.String(), b.Name, b.Deleted)
fmt.Printf("%s %s\n", b.ResourceID.String(), b.Name)
}
}
},

View File

@@ -9,7 +9,6 @@ import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
const addBookmark = `-- name: AddBookmark :one
@@ -58,40 +57,3 @@ func (q *Queries) AddBookmark(ctx context.Context, arg AddBookmarkParams) (Bookm
)
return i, err
}
const listBookmarks = `-- name: ListBookmarks :many
SELECT username, resource_id, name, dir, created, modified, deleted FROM bookmarks b WHERE username = $1::TEXT AND modified > $2::TIMESTAMP
`
type ListBookmarksParams struct {
Username string
Since pgtype.Timestamp
}
func (q *Queries) ListBookmarks(ctx context.Context, arg ListBookmarksParams) ([]Bookmark, error) {
rows, err := q.db.Query(ctx, listBookmarks, arg.Username, arg.Since)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Bookmark
for rows.Next() {
var i Bookmark
if err := rows.Scan(
&i.Username,
&i.ResourceID,
&i.Name,
&i.Dir,
&i.Created,
&i.Modified,
&i.Deleted,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}

View File

@@ -1,10 +1,8 @@
package user
import (
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5"
"github.com/shroff/phylum/server/internal/core/db"
"github.com/shroff/phylum/server/internal/core/fs"
)
@@ -14,7 +12,19 @@ type Bookmark struct {
Name string `json:"name"`
Dir bool `json:"dir"`
Created int64 `json:"created"`
Deleted bool `json:"deleted"`
}
func scanBookmark(row pgx.CollectableRow) (Bookmark, error) {
var p Bookmark
if err := row.Scan(
&p.ResourceID,
&p.Name,
&p.Dir,
&p.Created,
); err != nil {
return Bookmark{}, err
}
return p, nil
}
func (m manager) AddBookmark(u User, resource fs.Resource, name string) (Bookmark, error) {
@@ -42,24 +52,20 @@ func (m manager) RemoveBookmark(u User, id uuid.UUID) error {
return err
}
func (m manager) ListBookmarks(u User, since *time.Time) ([]Bookmark, error) {
s := pgtype.Timestamp{
Valid: true,
}
if since != nil {
s.Time = *since
}
// TODO: #permissions This doesn't take permissions into account. is this okay?
res, err := m.db.Queries.ListBookmarks(m.ctx, db.ListBookmarksParams{Username: u.Username, Since: s})
if err != nil {
return nil, err
}
func (m manager) ListBookmarks(u User) ([]Bookmark, error) {
const q = `SELECT resource_id, name, dir, created
FROM bookmarks b
WHERE username = $1::TEXT
AND deleted IS NULL
ORDER BY modified DESC`
result := make([]Bookmark, len(res))
for i, b := range res {
result[i] = bookmarkFromDb(b)
if rows, err := m.db.Query(m.ctx, q, u.Username); err != nil {
return nil, err
} else if bookmarks, err := pgx.CollectRows(rows, scanBookmark); err != nil {
return nil, err
} else {
return bookmarks, nil
}
return result, err
}
func bookmarkFromDb(b db.Bookmark) Bookmark {
@@ -68,6 +74,5 @@ func bookmarkFromDb(b db.Bookmark) Bookmark {
Name: b.Name,
Dir: b.Dir,
Created: b.Created.Time.UnixMilli(),
Deleted: b.Deleted.Valid,
}
}

View File

@@ -45,7 +45,7 @@ type Manager interface {
// bookmarks.go
AddBookmark(u User, resource fs.Resource, name string) (Bookmark, error)
RemoveBookmark(u User, id uuid.UUID) error
ListBookmarks(u User, since *time.Time) ([]Bookmark, error)
ListBookmarks(u User) ([]Bookmark, error)
// shared.go
SharedResources(u User) (result []fs.Resource, err error)

View File

@@ -16,6 +16,3 @@ SET
deleted = NULL,
name = @name::TEXT
RETURNING *;
-- name: ListBookmarks :many
SELECT * FROM bookmarks b WHERE username = @username::TEXT AND modified > @since::TIMESTAMP;