You've already forked agentic-coding-workflow
feat(notify): sqlc queries for notifications crud
This commit is contained in:
@@ -1,2 +1,22 @@
|
|||||||
-- name: Ping :one
|
-- name: InsertNotification :exec
|
||||||
SELECT 1::int AS ok;
|
INSERT INTO notifications (id, user_id, topic, severity, title, body, link, metadata, created_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);
|
||||||
|
|
||||||
|
-- name: ListNotificationsByUser :many
|
||||||
|
SELECT id, user_id, topic, severity, title, body, link, metadata, read_at, created_at
|
||||||
|
FROM notifications
|
||||||
|
WHERE user_id = $1
|
||||||
|
AND ($2::bool = false OR read_at IS NULL)
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT $3;
|
||||||
|
|
||||||
|
-- name: CountUnreadByUser :one
|
||||||
|
SELECT COUNT(*)::int FROM notifications WHERE user_id = $1 AND read_at IS NULL;
|
||||||
|
|
||||||
|
-- name: MarkRead :exec
|
||||||
|
UPDATE notifications SET read_at = now()
|
||||||
|
WHERE id = $1 AND user_id = $2 AND read_at IS NULL;
|
||||||
|
|
||||||
|
-- name: MarkAllReadByUser :exec
|
||||||
|
UPDATE notifications SET read_at = now()
|
||||||
|
WHERE user_id = $1 AND read_at IS NULL;
|
||||||
|
|||||||
@@ -7,15 +7,120 @@ package notifysqlc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
)
|
)
|
||||||
|
|
||||||
const ping = `-- name: Ping :one
|
const countUnreadByUser = `-- name: CountUnreadByUser :one
|
||||||
SELECT 1::int AS ok
|
SELECT COUNT(*)::int FROM notifications WHERE user_id = $1 AND read_at IS NULL
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) Ping(ctx context.Context) (int32, error) {
|
func (q *Queries) CountUnreadByUser(ctx context.Context, userID pgtype.UUID) (int32, error) {
|
||||||
row := q.db.QueryRow(ctx, ping)
|
row := q.db.QueryRow(ctx, countUnreadByUser, userID)
|
||||||
var ok int32
|
var column_1 int32
|
||||||
err := row.Scan(&ok)
|
err := row.Scan(&column_1)
|
||||||
return ok, err
|
return column_1, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const insertNotification = `-- name: InsertNotification :exec
|
||||||
|
INSERT INTO notifications (id, user_id, topic, severity, title, body, link, metadata, created_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||||
|
`
|
||||||
|
|
||||||
|
type InsertNotificationParams struct {
|
||||||
|
ID pgtype.UUID `json:"id"`
|
||||||
|
UserID pgtype.UUID `json:"user_id"`
|
||||||
|
Topic string `json:"topic"`
|
||||||
|
Severity string `json:"severity"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
Link *string `json:"link"`
|
||||||
|
Metadata []byte `json:"metadata"`
|
||||||
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) InsertNotification(ctx context.Context, arg InsertNotificationParams) error {
|
||||||
|
_, err := q.db.Exec(ctx, insertNotification,
|
||||||
|
arg.ID,
|
||||||
|
arg.UserID,
|
||||||
|
arg.Topic,
|
||||||
|
arg.Severity,
|
||||||
|
arg.Title,
|
||||||
|
arg.Body,
|
||||||
|
arg.Link,
|
||||||
|
arg.Metadata,
|
||||||
|
arg.CreatedAt,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const listNotificationsByUser = `-- name: ListNotificationsByUser :many
|
||||||
|
SELECT id, user_id, topic, severity, title, body, link, metadata, read_at, created_at
|
||||||
|
FROM notifications
|
||||||
|
WHERE user_id = $1
|
||||||
|
AND ($2::bool = false OR read_at IS NULL)
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT $3
|
||||||
|
`
|
||||||
|
|
||||||
|
type ListNotificationsByUserParams struct {
|
||||||
|
UserID pgtype.UUID `json:"user_id"`
|
||||||
|
Column2 bool `json:"column_2"`
|
||||||
|
Limit int32 `json:"limit"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) ListNotificationsByUser(ctx context.Context, arg ListNotificationsByUserParams) ([]Notification, error) {
|
||||||
|
rows, err := q.db.Query(ctx, listNotificationsByUser, arg.UserID, arg.Column2, arg.Limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []Notification
|
||||||
|
for rows.Next() {
|
||||||
|
var i Notification
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.Topic,
|
||||||
|
&i.Severity,
|
||||||
|
&i.Title,
|
||||||
|
&i.Body,
|
||||||
|
&i.Link,
|
||||||
|
&i.Metadata,
|
||||||
|
&i.ReadAt,
|
||||||
|
&i.CreatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const markAllReadByUser = `-- name: MarkAllReadByUser :exec
|
||||||
|
UPDATE notifications SET read_at = now()
|
||||||
|
WHERE user_id = $1 AND read_at IS NULL
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) MarkAllReadByUser(ctx context.Context, userID pgtype.UUID) error {
|
||||||
|
_, err := q.db.Exec(ctx, markAllReadByUser, userID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const markRead = `-- name: MarkRead :exec
|
||||||
|
UPDATE notifications SET read_at = now()
|
||||||
|
WHERE id = $1 AND user_id = $2 AND read_at IS NULL
|
||||||
|
`
|
||||||
|
|
||||||
|
type MarkReadParams struct {
|
||||||
|
ID pgtype.UUID `json:"id"`
|
||||||
|
UserID pgtype.UUID `json:"user_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) MarkRead(ctx context.Context, arg MarkReadParams) error {
|
||||||
|
_, err := q.db.Exec(ctx, markRead, arg.ID, arg.UserID)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user