forked from mtusi2026/mtusi2026lab7
105 lines
2.3 KiB
Go
105 lines
2.3 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: sessions.sql
|
|
|
|
package sqlcgen
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const createSession = `-- name: CreateSession :one
|
|
INSERT INTO sessions (token_hash, user_id, expires_at)
|
|
VALUES ($1, $2, $3)
|
|
RETURNING token_hash, user_id, expires_at
|
|
`
|
|
|
|
type CreateSessionParams struct {
|
|
TokenHash []byte
|
|
UserID uuid.UUID
|
|
ExpiresAt time.Time
|
|
}
|
|
|
|
type CreateSessionRow struct {
|
|
TokenHash []byte
|
|
UserID uuid.UUID
|
|
ExpiresAt time.Time
|
|
}
|
|
|
|
func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (CreateSessionRow, error) {
|
|
row := q.db.QueryRow(ctx, createSession, arg.TokenHash, arg.UserID, arg.ExpiresAt)
|
|
var i CreateSessionRow
|
|
err := row.Scan(&i.TokenHash, &i.UserID, &i.ExpiresAt)
|
|
return i, err
|
|
}
|
|
|
|
const deleteSession = `-- name: DeleteSession :exec
|
|
DELETE FROM sessions
|
|
WHERE token_hash = $1
|
|
AND user_id = $2
|
|
`
|
|
|
|
type DeleteSessionParams struct {
|
|
TokenHash []byte
|
|
UserID uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) DeleteSession(ctx context.Context, arg DeleteSessionParams) error {
|
|
_, err := q.db.Exec(ctx, deleteSession, arg.TokenHash, arg.UserID)
|
|
return err
|
|
}
|
|
|
|
const getSessionUser = `-- name: GetSessionUser :one
|
|
SELECT u.id, u.username, u.display_name, u.created_at, s.expires_at
|
|
FROM sessions s
|
|
JOIN users u ON u.id = s.user_id
|
|
WHERE s.token_hash = $1
|
|
AND s.expires_at > now()
|
|
`
|
|
|
|
type GetSessionUserRow struct {
|
|
ID uuid.UUID
|
|
Username string
|
|
DisplayName string
|
|
CreatedAt time.Time
|
|
ExpiresAt time.Time
|
|
}
|
|
|
|
func (q *Queries) GetSessionUser(ctx context.Context, tokenHash []byte) (GetSessionUserRow, error) {
|
|
row := q.db.QueryRow(ctx, getSessionUser, tokenHash)
|
|
var i GetSessionUserRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Username,
|
|
&i.DisplayName,
|
|
&i.CreatedAt,
|
|
&i.ExpiresAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const updatePasswordHash = `-- name: UpdatePasswordHash :execrows
|
|
UPDATE users
|
|
SET password_hash = $1
|
|
WHERE id = $2
|
|
AND password_hash = $3
|
|
`
|
|
|
|
type UpdatePasswordHashParams struct {
|
|
NewHash string
|
|
UserID uuid.UUID
|
|
OldHash string
|
|
}
|
|
|
|
func (q *Queries) UpdatePasswordHash(ctx context.Context, arg UpdatePasswordHashParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, updatePasswordHash, arg.NewHash, arg.UserID, arg.OldHash)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|