diff --git a/internal/infra/notify/queries/notifications.sql b/internal/infra/notify/queries/notifications.sql index 9bd5c10..e4959ee 100644 --- a/internal/infra/notify/queries/notifications.sql +++ b/internal/infra/notify/queries/notifications.sql @@ -1,2 +1,22 @@ --- name: Ping :one -SELECT 1::int AS ok; +-- 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); + +-- 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; diff --git a/internal/infra/notify/sqlc/notifications.sql.go b/internal/infra/notify/sqlc/notifications.sql.go index 12a816f..35cf851 100644 --- a/internal/infra/notify/sqlc/notifications.sql.go +++ b/internal/infra/notify/sqlc/notifications.sql.go @@ -7,15 +7,120 @@ package notifysqlc import ( "context" + + "github.com/jackc/pgx/v5/pgtype" ) -const ping = `-- name: Ping :one -SELECT 1::int AS ok +const countUnreadByUser = `-- name: CountUnreadByUser :one +SELECT COUNT(*)::int FROM notifications WHERE user_id = $1 AND read_at IS NULL ` -func (q *Queries) Ping(ctx context.Context) (int32, error) { - row := q.db.QueryRow(ctx, ping) - var ok int32 - err := row.Scan(&ok) - return ok, err +func (q *Queries) CountUnreadByUser(ctx context.Context, userID pgtype.UUID) (int32, error) { + row := q.db.QueryRow(ctx, countUnreadByUser, userID) + var column_1 int32 + err := row.Scan(&column_1) + 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 }