154 lines
4.4 KiB
Go
154 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
var dbConnection *pgx.Conn
|
|
|
|
func InitDatabase(ctx context.Context) {
|
|
conn, err := pgx.Connect(ctx, "postgres://master:secret@localhost:5432")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
_, err = conn.Exec(ctx, `
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id SERIAL PRIMARY KEY,
|
|
name VARCHAR(20) UNIQUE NOT NULL,
|
|
pass_hash VARCHAR(60) NOT 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 SET NULL,
|
|
owner_id INTEGER NOT NULL REFERENCES users(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)
|
|
}
|
|
dbConnection = conn
|
|
}
|
|
|
|
func AddNewUser(ctx context.Context, user *User) (uint32, error) {
|
|
var id uint32
|
|
var err error
|
|
|
|
if len(user.Name) == 0 || len(user.Name) > 20 {
|
|
return 0, errors.New("username bad length")
|
|
}
|
|
if user.IsPasswordHashed {
|
|
if len(user.Password) != 60 {
|
|
return 0, errors.New("password bad length")
|
|
}
|
|
} else {
|
|
var hashed []byte
|
|
hashed, err = bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
user.Password = string(hashed)
|
|
}
|
|
if len(user.Color) != 1 && len(user.Color) != 3 {
|
|
return 0, errors.New("color invalid")
|
|
}
|
|
err = dbConnection.QueryRow(ctx, `
|
|
INSERT INTO users (name, pass_hash, color)
|
|
VALUES ($1, $2, $3)
|
|
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 pass_hash FROM users WHERE id = $1", id).Scan(&controlHash)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
return bcrypt.CompareHashAndPassword([]byte(controlHash), []byte(plainPassword)) == nil
|
|
}
|
|
|
|
func GetUserDataById(ctx context.Context, id uint32) (User, error) {
|
|
var user User
|
|
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
|
|
}
|
|
user.IsPasswordHashed = true
|
|
return user, nil
|
|
}
|
|
func GetUserDataByName(ctx context.Context, name string) (User, error) {
|
|
var user User
|
|
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
|
|
}
|
|
user.IsPasswordHashed = true
|
|
return user, nil
|
|
}
|
|
|
|
func CreateChatGroupWithoutMembers(ctx context.Context, group *ChatGroup) (uint32, error) {
|
|
if len(group.Name) < 1 {
|
|
return 0, errors.New("group name too short")
|
|
}
|
|
if len(group.Name) > 48 {
|
|
return 0, errors.New("group name too long")
|
|
}
|
|
|
|
var id uint32
|
|
err := dbConnection.QueryRow(ctx, `INSERT INTO chat_groups (name, creator_id, owner_id, created_at )
|
|
VALUES ($1, $2, $3, $4)
|
|
RETURNING id
|
|
`, group.Name, group.CreatorId, group.OwnerId, group.CreatedAt).Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
func GetChatGroupWithoutMembers(ctx context.Context, id uint32) (ChatGroup, error) {
|
|
var group ChatGroup
|
|
err := dbConnection.QueryRow(ctx, `SELECT name, creator_id, owner_id, enable_user_colors, group_color, created_at FROM chat_groups WHERE id = $1`,
|
|
id).Scan(&group.Name, &group.CreatorId, &group.OwnerId, &group.EnableUserColors, &group.Color, &group.CreatedAt)
|
|
return group, err
|
|
}
|
|
|
|
func GetChatGroupMembers(ctx context.Context, groupId uint32) ([]User, error) {
|
|
rows, err := dbConnection.Query(ctx, `
|
|
SELECT usr.id, usr.name, usr.color FROM users usr
|
|
JOIN chat_group_members members ON usr.id = members.user_id
|
|
WHERE members.group_id = $1
|
|
`, groupId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var members []User
|
|
for rows.Next() {
|
|
var user User
|
|
if err := rows.Scan(&user.Id, &user.Name, &user.Color); err != nil {
|
|
return nil, err
|
|
}
|
|
members = append(members, user)
|
|
}
|
|
return members, rows.Err()
|
|
}
|