forked from mtusi2026/mtusi2026lab2
refactor(db): drop Querier and select only user id for session lookup
This commit is contained in:
@@ -9,11 +9,10 @@ INSERT INTO sessions (
|
||||
sqlc.arg(expires_at)
|
||||
);
|
||||
|
||||
-- name: GetSessionUser :one
|
||||
SELECT u.id, u.username, u.display_name, u.created_at FROM sessions s
|
||||
JOIN users u ON u.id = s.user_id
|
||||
WHERE s.token_hash = sqlc.arg(token_hash)
|
||||
AND s.expires_at > now();
|
||||
-- name: GetSessionUserID :one
|
||||
SELECT user_id FROM sessions
|
||||
WHERE token_hash = sqlc.arg(token_hash)
|
||||
AND expires_at > now();
|
||||
|
||||
-- name: DeleteSession :exec
|
||||
DELETE FROM sessions
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Querier interface {
|
||||
AddChatMembers(ctx context.Context, arg AddChatMembersParams) error
|
||||
CreateChat(ctx context.Context, arg CreateChatParams) (Chat, error)
|
||||
CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error)
|
||||
CreateSession(ctx context.Context, arg CreateSessionParams) error
|
||||
CreateUser(ctx context.Context, arg CreateUserParams) (CreateUserRow, error)
|
||||
DeleteExpiredSessions(ctx context.Context) (int64, error)
|
||||
DeleteSession(ctx context.Context, tokenHash string) error
|
||||
GetChat(ctx context.Context, arg GetChatParams) (GetChatRow, error)
|
||||
GetSessionUser(ctx context.Context, tokenHash string) (GetSessionUserRow, error)
|
||||
GetUser(ctx context.Context, id uuid.UUID) (GetUserRow, error)
|
||||
GetUserByUsername(ctx context.Context, username string) (User, error)
|
||||
IsChatMember(ctx context.Context, arg IsChatMemberParams) (bool, error)
|
||||
ListChats(ctx context.Context, arg ListChatsParams) ([]ListChatsRow, error)
|
||||
ListMessages(ctx context.Context, arg ListMessagesParams) ([]Message, error)
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
@@ -58,28 +58,15 @@ func (q *Queries) DeleteSession(ctx context.Context, tokenHash string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
const getSessionUser = `-- name: GetSessionUser :one
|
||||
SELECT u.id, u.username, u.display_name, u.created_at FROM sessions s
|
||||
JOIN users u ON u.id = s.user_id
|
||||
WHERE s.token_hash = $1
|
||||
AND s.expires_at > now()
|
||||
const getSessionUserID = `-- name: GetSessionUserID :one
|
||||
SELECT user_id FROM sessions
|
||||
WHERE token_hash = $1
|
||||
AND expires_at > now()
|
||||
`
|
||||
|
||||
type GetSessionUserRow struct {
|
||||
ID uuid.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) GetSessionUser(ctx context.Context, tokenHash string) (GetSessionUserRow, error) {
|
||||
row := q.db.QueryRow(ctx, getSessionUser, tokenHash)
|
||||
var i GetSessionUserRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Username,
|
||||
&i.DisplayName,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
func (q *Queries) GetSessionUserID(ctx context.Context, tokenHash string) (uuid.UUID, error) {
|
||||
row := q.db.QueryRow(ctx, getSessionUserID, tokenHash)
|
||||
var user_id uuid.UUID
|
||||
err := row.Scan(&user_id)
|
||||
return user_id, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user