From 39a926cd521806aedc298ddd671d1a118794fcec Mon Sep 17 00:00:00 2001 From: Leonardo Bishop Date: Wed, 17 Sep 2025 18:13:30 +0100 Subject: Add endpoints for web extension --- pkg/database/sqlc/entries.sql.go | 99 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) (limited to 'pkg/database/sqlc') diff --git a/pkg/database/sqlc/entries.sql.go b/pkg/database/sqlc/entries.sql.go index c7b5e1c..846076d 100644 --- a/pkg/database/sqlc/entries.sql.go +++ b/pkg/database/sqlc/entries.sql.go @@ -43,6 +43,19 @@ func (q *Queries) CreateEntryWithKindName(ctx context.Context, arg CreateEntryWi return i, err } +const deleteEntry = `-- name: DeleteEntry :execrows +DELETE FROM entries +WHERE id = ? +` + +func (q *Queries) DeleteEntry(ctx context.Context, id int64) (int64, error) { + result, err := q.db.ExecContext(ctx, deleteEntry, id) + if err != nil { + return 0, err + } + return result.RowsAffected() +} + const getEntries = `-- name: GetEntries :many SELECT title, url, description, timestamp, kinds.name as kind_name, kinds.emoji as kind_emoji FROM entries JOIN kinds ON entries.kind == kinds.id @@ -87,3 +100,89 @@ func (q *Queries) GetEntries(ctx context.Context) ([]GetEntriesRow, error) { } return items, nil } + +const getEntryByUrl = `-- name: GetEntryByUrl :one +SELECT entries.id, title, url, description, timestamp, kinds.name as kind_name, kinds.emoji as kind_emoji FROM entries +JOIN kinds ON entries.kind == kinds.id +WHERE url = ? +LIMIT 1 +` + +type GetEntryByUrlRow struct { + ID int64 `json:"id"` + Title string `json:"title"` + Url string `json:"url"` + Description string `json:"description"` + Timestamp string `json:"timestamp"` + KindName string `json:"kind_name"` + KindEmoji string `json:"kind_emoji"` +} + +func (q *Queries) GetEntryByUrl(ctx context.Context, url string) (GetEntryByUrlRow, error) { + row := q.db.QueryRowContext(ctx, getEntryByUrl, url) + var i GetEntryByUrlRow + err := row.Scan( + &i.ID, + &i.Title, + &i.Url, + &i.Description, + &i.Timestamp, + &i.KindName, + &i.KindEmoji, + ) + return i, err +} + +const getEntryURLs = `-- name: GetEntryURLs :many +SELECT url FROM entries +ORDER BY timestamp DESC +` + +func (q *Queries) GetEntryURLs(ctx context.Context) ([]string, error) { + rows, err := q.db.QueryContext(ctx, getEntryURLs) + if err != nil { + return nil, err + } + defer rows.Close() + var items []string + for rows.Next() { + var url string + if err := rows.Scan(&url); err != nil { + return nil, err + } + items = append(items, url) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const updateEntryKind = `-- name: UpdateEntryKind :one +UPDATE entries +SET kind = (SELECT id FROM kinds WHERE kinds.name = ?) +WHERE entries.id = ? +RETURNING id, title, kind, url, description, timestamp +` + +type UpdateEntryKindParams struct { + Name string `json:"name"` + ID int64 `json:"id"` +} + +func (q *Queries) UpdateEntryKind(ctx context.Context, arg UpdateEntryKindParams) (Entry, error) { + row := q.db.QueryRowContext(ctx, updateEntryKind, arg.Name, arg.ID) + var i Entry + err := row.Scan( + &i.ID, + &i.Title, + &i.Kind, + &i.Url, + &i.Description, + &i.Timestamp, + ) + return i, err +} -- cgit v1.2.3-70-g09d2