new start
This commit is contained in:
+48
-37
@@ -3,66 +3,77 @@ package main
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
var dbConn *pgx.Conn
|
||||
var dbConn *pgxpool.Pool
|
||||
|
||||
func InitDatabase(ctx context.Context) {
|
||||
conn, err := pgx.Connect(ctx, "postgres://master:secret@localhost:5432")
|
||||
func DbInit(ctx context.Context) {
|
||||
var err error
|
||||
dbConn, err = pgxpool.New(ctx, "postgres://master:secret@localhost:5432") // TODO change to env in production
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = conn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
_, err = dbConn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS client (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(20) UNIQUE NOT NULL,
|
||||
pass_hash VARCHAR(60) NOT NULL,
|
||||
pronouns VARCHAR(15) DEFAULT NULL,
|
||||
color VARCHAR(3) DEFAULT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS chat_groups (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(48) NOT NULL,
|
||||
creator_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
owner_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
enable_user_colors BOOLEAN NOT NULL DEFAULT true,
|
||||
group_color VARCHAR(3),
|
||||
color_red SMALLINT DEFAULT NULL,
|
||||
color_green SMALLINT DEFAULT NULL,
|
||||
color_blue SMALLINT DEFAULT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS chat_group_members (
|
||||
group_id INTEGER NOT NULL REFERENCES chat_groups(id) ON DELETE CASCADE,
|
||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
joined_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
PRIMARY KEY (group_id, user_id)
|
||||
);
|
||||
)
|
||||
`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
dbConn = conn
|
||||
_, err = dbConn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS chat_groups (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(48) NOT NULL,
|
||||
creator_id INTEGER NOT NULL REFERENCES client(id) ON DELETE CASCADE,
|
||||
owner_id INTEGER NOT NULL REFERENCES client(id) ON DELETE CASCADE,
|
||||
enable_user_colors BOOLEAN NOT NULL DEFAULT true,
|
||||
color_red SMALLINT DEFAULT NULL,
|
||||
color_green SMALLINT DEFAULT NULL,
|
||||
color_blue SMALLINT DEFAULT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
)
|
||||
`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = dbConn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS chat_group_members (
|
||||
group_id INTEGER NOT NULL REFERENCES chat_groups(id) ON DELETE CASCADE,
|
||||
user_id INTEGER NOT NULL REFERENCES client(id) ON DELETE CASCADE,
|
||||
joined_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
PRIMARY KEY (group_id, user_id)
|
||||
)
|
||||
`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func DbSaveClientWithoutGroups(ctx context.Context, client *Client) error {
|
||||
var id uint64
|
||||
var err error
|
||||
|
||||
err = dbConn.QueryRow(ctx, `
|
||||
INSERT INTO users (name, pass_hash, pronouns)
|
||||
VALUES ($1, $2, $3)
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
INSERT INTO clients (name, pass_hash, pronouns, color_red, color_green, color_blue, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id
|
||||
`, client.Name, client.PasswordHash, client.Pronouns).Scan(&id)
|
||||
`, client.Name, client.PasswordHash, client.Pronouns, client.Color[0], client.Color[1], client.Color[2], client.CreatedAt).
|
||||
Scan(&client.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbGetClient(ctx context.Context, id uint32, client *Client) error {
|
||||
func DbGetClientByName(ctx context.Context, client *Client) error {
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
SELECT name, pass_hash, pronouns, color WHERE id = $1
|
||||
`, id).Scan(client.Name, client.PasswordHash, client.Pronouns, client.Color)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
SELECT name, pass_hash, color_red, color_green, color_blue, created_at FROM clients WHERE name = $1
|
||||
`, client.Name).Scan(&client.Name, &client.PasswordHash, client.Pronouns, client.Color[0], client.Color[1], client.Color[2], client.CreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user