change db structure, add goroutine connection handling

This commit is contained in:
GitProtogen
2026-03-12 14:24:31 +01:00
parent afdc3f96a0
commit b7b5788f98
5 changed files with 79 additions and 101 deletions
+29 -10
View File
@@ -19,11 +19,26 @@ func InitDatabase(ctx context.Context) {
_, err = conn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS users (
Id SERIAL PRIMARY KEY,
Name VARCHAR(20) UNIQUE NOT NULL,
PassHash VARCHAR(60) NOT NULL,
Color VARCHAR(3) NOT NULL
)
id SERIAL PRIMARY KEY,
name VARCHAR(20) UNIQUE NOT NULL,
pass_hash VARCHAR(60) NOT NULL,
color VARCHAR(3) NOT NULL
);
CREATE TABLE IF NOT EXISTS chat_groups (
id SERIAL PRIMARY KEY,
name VARCHAR(48) NOT NULL,
createor INTEGER NOT NULL REFERANCE users(id) ON DELETE SET NULL,
owner INTEGER NOT NULL REFERANCE user(id) ON DELETE SET NULL
enable_user_colors BOOLEAN NOT NULL DEFAULT true,
group_color VARCHAR(3),
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)
@@ -54,16 +69,16 @@ func AddNewUser(ctx context.Context, user User) (uint32, error) {
return 0, errors.New("color invalid")
}
err = dbConnection.QueryRow(ctx, `
INSERT INTO users (Name, PassHash, Color)
INSERT INTO users (name, pass_hash, color)
VALUES ($1, $2, $3)
RETURNING Id
RETURNING id
`, user.Name, user.Password, user.Color).Scan(&id)
return id, err
}
func isPassValid(ctx context.Context, id uint32, plainPassword string) bool {
var controlHash string
err := dbConnection.QueryRow(ctx, "SELECT PassHash FROM users WHERE Id = $1", id).Scan(&controlHash)
err := dbConnection.QueryRow(ctx, "SELECT pass_hash FROM users WHERE id = $1", id).Scan(&controlHash)
if err != nil {
return false
}
@@ -73,7 +88,7 @@ func isPassValid(ctx context.Context, id uint32, plainPassword string) bool {
func GetUserDataById(ctx context.Context, id uint32) (*User, error) {
var user User
err := dbConnection.QueryRow(ctx, "SELECT Id, Name, PassHash, Color FROM users WHERE Id = $1", id).
err := dbConnection.QueryRow(ctx, "SELECT id, name, pass_hash, color FROM users WHERE id = $1", id).
Scan(&user.Id, &user.Name, &user.Password, &user.Color)
if err != nil {
return &User{}, err
@@ -83,7 +98,7 @@ func GetUserDataById(ctx context.Context, id uint32) (*User, error) {
}
func GetUserDataByName(ctx context.Context, name string) (*User, error) {
var user User
err := dbConnection.QueryRow(ctx, "SELECT Id, Name, PassHash, Color FROM users WHERE Name = $1", name).
err := dbConnection.QueryRow(ctx, "SELECT id, name, pass_hash, color FROM users WHERE name = $1", name).
Scan(&user.Id, &user.Name, &user.Password, &user.Color)
if err != nil {
return &User{}, err
@@ -91,3 +106,7 @@ func GetUserDataByName(ctx context.Context, name string) (*User, error) {
user.IsPasswordHashed = true
return &user, nil
}
func CreateGroup() {
}