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

@@ -1,11 +0,0 @@
package db
var authMap = map[string]AuthType{
string(AuthTypePassword): AuthTypePassword,
string(AuthTypeDiscord): AuthTypeDiscord,
string(AuthTypeGoogle): AuthTypeGoogle,
}
func AuthTypeFromString(input string) AuthType {
return authMap[input]
}

View File

@@ -1,7 +1,7 @@
-- +goose Up
-- create initial schema
CREATE TABLE users (
id UUID PRIMARY KEY ,
id UUID PRIMARY KEY DEFAULT uuidv7(),
name TEXT NOT NULL,
email TEXT NOT NULL,
bio TEXT NOT NULL default '',

View File

@@ -8,6 +8,7 @@ import (
"database/sql/driver"
"fmt"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
@@ -97,8 +98,8 @@ func (ns NullEntrantType) Value() (driver.Value, error) {
}
type AuthMethod struct {
ID pgtype.UUID `json:"id"`
UserID pgtype.UUID `json:"user_id"`
ID uuid.UUID `json:"id"`
UserID uuid.UUID `json:"user_id"`
Type AuthType `json:"type"`
Value string `json:"value"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
@@ -106,63 +107,63 @@ type AuthMethod struct {
}
type Bracket struct {
ID pgtype.UUID `json:"id"`
ID uuid.UUID `json:"id"`
Name string `json:"name"`
EventID pgtype.UUID `json:"event_id"`
EventID uuid.UUID `json:"event_id"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type BracketEntrant struct {
ID pgtype.UUID `json:"id"`
ID uuid.UUID `json:"id"`
SeedOrder int32 `json:"seed_order"`
EntrantID pgtype.UUID `json:"entrant_id"`
EntrantID uuid.UUID `json:"entrant_id"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type Entrant struct {
ID pgtype.UUID `json:"id"`
ID uuid.UUID `json:"id"`
Name string `json:"name"`
EventID pgtype.UUID `json:"event_id"`
EventID uuid.UUID `json:"event_id"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type Event struct {
ID pgtype.UUID `json:"id"`
ID uuid.UUID `json:"id"`
Name string `json:"name"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type Match struct {
ID pgtype.UUID `json:"id"`
Player1 pgtype.UUID `json:"player1"`
Player2 pgtype.UUID `json:"player2"`
Player1From pgtype.UUID `json:"player1_from"`
Player2From pgtype.UUID `json:"player2_from"`
ID uuid.UUID `json:"id"`
Player1 uuid.UUID `json:"player1"`
Player2 uuid.UUID `json:"player2"`
Player1From uuid.UUID `json:"player1_from"`
Player2From uuid.UUID `json:"player2_from"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type Team struct {
ID pgtype.UUID `json:"id"`
ID uuid.UUID `json:"id"`
Name string `json:"name"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type TeamEntrant struct {
ID pgtype.UUID `json:"id"`
TeamID pgtype.UUID `json:"team_id"`
EntrantID pgtype.UUID `json:"entrant_id"`
ID uuid.UUID `json:"id"`
TeamID uuid.UUID `json:"team_id"`
EntrantID uuid.UUID `json:"entrant_id"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type User struct {
ID pgtype.UUID `json:"id"`
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Bio string `json:"bio"`

View File

@@ -1,7 +1,7 @@
-- name: GetUserFromId :one
-- name: GetUserById :one
SELECT *
FROM users u
WHERE u.id = $1;
WHERE u.id = @user_id::uuid;
-- name: GetUserAuthValue :one
SELECT u.ID as user_id, a.value as auth_value
@@ -9,5 +9,20 @@ 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;
(u.name = @auth_value::text OR u.email = @auth_value)
AND a.type = @auth_type::auth_type;
-- name: CreateUser :one
INSERT INTO users (name, email, bio, profile_photo)
VALUES($1, $2, $3, $4)
RETURNING *;
-- name: UpdateUser :one
UPDATE users
SET name = @name::text, email = @email::text, bio = @bio::text, profile_photo = @photo::text
WHERE id = @id::uuid
RETURNING *;
-- name: CreateAuthMethod :exec
INSERT INTO auth_method (user_id, type, value)
VALUES ($1, $2, $3);

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,