setup project

This commit is contained in:
2026-04-04 13:35:33 -06:00
parent 3f75aead20
commit 27088c2cd8
48 changed files with 7831 additions and 2 deletions

32
db/db.go Normal file
View File

@@ -0,0 +1,32 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
package db
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
db: tx,
}
}

11
db/helpers.go Normal file
View File

@@ -0,0 +1,11 @@
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

@@ -0,0 +1,77 @@
-- +goose Up
-- create initial schema
CREATE TABLE users (
id UUID PRIMARY KEY ,
name TEXT NOT NULL,
email TEXT NOT NULL,
bio TEXT NOT NULL default '',
profile_photo TEXT NULL,
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TYPE auth_type AS ENUM ('discord', 'google', 'password');
CREATE TABLE IF NOT EXISTS auth_method (
id UUID PRIMARY KEY default uuidv7(),
user_id UUID REFERENCES users(id),
type auth_type NOT NULL,
value TEXT NOT NULL,
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- BRACKET MANAGEMENT
CREATE TABLE IF NOT EXISTS event(
id UUID PRIMARY KEY default uuidv7(),
name TEXT NOT NULL,
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TYPE entrant_type AS ENUM ('single', 'team');
CREATE TABLE IF NOT EXISTS entrant(
id UUID PRIMARY KEY default uuidv7(),
name TEXT NOT NULL,
event_id UUID REFERENCES event(id),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS team(
id UUID PRIMARY KEY default uuidv7(),
name TEXT NOT NULL,
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS team_entrant(
id UUID PRIMARY KEY default uuidv7(),
team_id UUID REFERENCES team(id),
entrant_id UUID REFERENCES entrant(id),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS bracket(
id UUID PRIMARY KEY default uuidv7(),
name TEXT NOT NULL,
event_id UUID REFERENCES event(id),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS bracket_entrant(
id UUID PRIMARY KEY default uuidv7(),
seed_order INTEGER NOT NULL,
entrant_id UUID REFERENCES entrant(id),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS match(
id UUID PRIMARY KEY default uuidv7(),
player1 UUID REFERENCES entrant(id),
player2 UUID REFERENCES entrant(id),
player1_from UUID REFERENCES match(id) NULL,
player2_from UUID references match(id) NULL,
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- +goose Down
-- Nothing this is the first migration

172
db/models.go Normal file
View File

@@ -0,0 +1,172 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
package db
import (
"database/sql/driver"
"fmt"
"github.com/jackc/pgx/v5/pgtype"
)
type AuthType string
const (
AuthTypeDiscord AuthType = "discord"
AuthTypeGoogle AuthType = "google"
AuthTypePassword AuthType = "password"
)
func (e *AuthType) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = AuthType(s)
case string:
*e = AuthType(s)
default:
return fmt.Errorf("unsupported scan type for AuthType: %T", src)
}
return nil
}
type NullAuthType struct {
AuthType AuthType `json:"auth_type"`
Valid bool `json:"valid"` // Valid is true if AuthType is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullAuthType) Scan(value interface{}) error {
if value == nil {
ns.AuthType, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.AuthType.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullAuthType) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return string(ns.AuthType), nil
}
type EntrantType string
const (
EntrantTypeSingle EntrantType = "single"
EntrantTypeTeam EntrantType = "team"
)
func (e *EntrantType) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = EntrantType(s)
case string:
*e = EntrantType(s)
default:
return fmt.Errorf("unsupported scan type for EntrantType: %T", src)
}
return nil
}
type NullEntrantType struct {
EntrantType EntrantType `json:"entrant_type"`
Valid bool `json:"valid"` // Valid is true if EntrantType is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullEntrantType) Scan(value interface{}) error {
if value == nil {
ns.EntrantType, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.EntrantType.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullEntrantType) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return string(ns.EntrantType), nil
}
type AuthMethod struct {
ID pgtype.UUID `json:"id"`
UserID pgtype.UUID `json:"user_id"`
Type AuthType `json:"type"`
Value string `json:"value"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type Bracket struct {
ID pgtype.UUID `json:"id"`
Name string `json:"name"`
EventID pgtype.UUID `json:"event_id"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type BracketEntrant struct {
ID pgtype.UUID `json:"id"`
SeedOrder int32 `json:"seed_order"`
EntrantID pgtype.UUID `json:"entrant_id"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type Entrant struct {
ID pgtype.UUID `json:"id"`
Name string `json:"name"`
EventID pgtype.UUID `json:"event_id"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type Event struct {
ID pgtype.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"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type Team struct {
ID pgtype.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"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type User struct {
ID pgtype.UUID `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Bio string `json:"bio"`
ProfilePhoto pgtype.Text `json:"profile_photo"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}

0
db/queries/brackets.sql Normal file
View File

0
db/queries/players.sql Normal file
View File

13
db/queries/users.sql Normal file
View File

@@ -0,0 +1,13 @@
-- name: GetUserFromId :one
SELECT *
FROM users u
WHERE u.id = $1;
-- 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;

60
db/users.sql.go Normal file
View File

@@ -0,0 +1,60 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: users.sql
package db
import (
"context"
"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
`
type GetUserAuthValueParams struct {
Name string `json:"name"`
Type AuthType `json:"type"`
}
type GetUserAuthValueRow struct {
UserID pgtype.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.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
`
func (q *Queries) GetUserFromId(ctx context.Context, id pgtype.UUID) (User, error) {
row := q.db.QueryRow(ctx, getUserFromId, id)
var i User
err := row.Scan(
&i.ID,
&i.Name,
&i.Email,
&i.Bio,
&i.ProfilePhoto,
&i.UpdatedAt,
&i.CreatedAt,
)
return i, err
}