making some progress

This commit is contained in:
2026-04-25 13:31:41 -06:00
parent 27088c2cd8
commit 09f4421f7f
11 changed files with 166 additions and 125 deletions

View File

@@ -8,44 +8,130 @@ package db
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
const getUserAuthValue = `-- name: GetUserAuthValue :one
SELECT u.ID as user_id, a.value as auth_value
FROM users u
JOIN auth_method a
ON a.user_id = u.id
WHERE
(u.name = $1 OR u.email = $1)
AND a.type = $2
const createAuthMethod = `-- name: CreateAuthMethod :exec
INSERT INTO auth_method (user_id, type, value)
VALUES ($1, $2, $3)
`
type GetUserAuthValueParams struct {
Name string `json:"name"`
Type AuthType `json:"type"`
type CreateAuthMethodParams struct {
UserID uuid.UUID `json:"user_id"`
Type AuthType `json:"type"`
Value string `json:"value"`
}
type GetUserAuthValueRow struct {
UserID pgtype.UUID `json:"user_id"`
AuthValue string `json:"auth_value"`
func (q *Queries) CreateAuthMethod(ctx context.Context, arg CreateAuthMethodParams) error {
_, err := q.db.Exec(ctx, createAuthMethod, arg.UserID, arg.Type, arg.Value)
return err
}
func (q *Queries) GetUserAuthValue(ctx context.Context, arg GetUserAuthValueParams) (GetUserAuthValueRow, error) {
row := q.db.QueryRow(ctx, getUserAuthValue, arg.Name, arg.Type)
var i GetUserAuthValueRow
err := row.Scan(&i.UserID, &i.AuthValue)
return i, err
}
const getUserFromId = `-- name: GetUserFromId :one
SELECT id, name, email, bio, profile_photo, updated_at, created_at
FROM users u
WHERE u.id = $1
const createUser = `-- name: CreateUser :one
INSERT INTO users (name, email, bio, profile_photo)
VALUES($1, $2, $3, $4)
RETURNING id, name, email, bio, profile_photo, updated_at, created_at
`
func (q *Queries) GetUserFromId(ctx context.Context, id pgtype.UUID) (User, error) {
row := q.db.QueryRow(ctx, getUserFromId, id)
type CreateUserParams struct {
Name string `json:"name"`
Email string `json:"email"`
Bio string `json:"bio"`
ProfilePhoto pgtype.Text `json:"profile_photo"`
}
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
row := q.db.QueryRow(ctx, createUser,
arg.Name,
arg.Email,
arg.Bio,
arg.ProfilePhoto,
)
var i User
err := row.Scan(
&i.ID,
&i.Name,
&i.Email,
&i.Bio,
&i.ProfilePhoto,
&i.UpdatedAt,
&i.CreatedAt,
)
return i, err
}
const getUserAuthValue = `-- name: GetUserAuthValue :one
SELECT u.ID as user_id, a.value as auth_value
FROM users u
JOIN auth_method a
ON a.user_id = u.id
WHERE
(u.name = $1::text OR u.email = $1)
AND a.type = $2::auth_type
`
type GetUserAuthValueParams struct {
AuthValue string `json:"auth_value"`
AuthType AuthType `json:"auth_type"`
}
type GetUserAuthValueRow struct {
UserID uuid.UUID `json:"user_id"`
AuthValue string `json:"auth_value"`
}
func (q *Queries) GetUserAuthValue(ctx context.Context, arg GetUserAuthValueParams) (GetUserAuthValueRow, error) {
row := q.db.QueryRow(ctx, getUserAuthValue, arg.AuthValue, arg.AuthType)
var i GetUserAuthValueRow
err := row.Scan(&i.UserID, &i.AuthValue)
return i, err
}
const getUserById = `-- name: GetUserById :one
SELECT id, name, email, bio, profile_photo, updated_at, created_at
FROM users u
WHERE u.id = $1::uuid
`
func (q *Queries) GetUserById(ctx context.Context, userID uuid.UUID) (User, error) {
row := q.db.QueryRow(ctx, getUserById, userID)
var i User
err := row.Scan(
&i.ID,
&i.Name,
&i.Email,
&i.Bio,
&i.ProfilePhoto,
&i.UpdatedAt,
&i.CreatedAt,
)
return i, err
}
const updateUser = `-- name: UpdateUser :one
UPDATE users
SET name = $1::text, email = $2::text, bio = $3::text, profile_photo = $4::text
WHERE id = $5::uuid
RETURNING id, name, email, bio, profile_photo, updated_at, created_at
`
type UpdateUserParams struct {
Name string `json:"name"`
Email string `json:"email"`
Bio string `json:"bio"`
Photo string `json:"photo"`
ID uuid.UUID `json:"id"`
}
func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error) {
row := q.db.QueryRow(ctx, updateUser,
arg.Name,
arg.Email,
arg.Bio,
arg.Photo,
arg.ID,
)
var i User
err := row.Scan(
&i.ID,