add client and groups member logic

This commit is contained in:
2026-03-27 18:49:45 +01:00
parent a95bc43be6
commit c5fc74d142
9 changed files with 256 additions and 70 deletions
+67 -17
View File
@@ -18,7 +18,7 @@ func DbInit(ctx context.Context) {
}
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS client (
CREATE TABLE IF NOT EXISTS clients (
id SERIAL PRIMARY KEY,
name VARCHAR(20) UNIQUE NOT NULL,
pass_hash VARCHAR(60) NOT NULL,
@@ -37,8 +37,8 @@ func DbInit(ctx context.Context) {
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,
creator_id INTEGER NOT NULL REFERENCES clients(id) ON DELETE CASCADE,
owner_id INTEGER NOT NULL REFERENCES clients(id) ON DELETE CASCADE,
enable_client_colors BOOLEAN NOT NULL DEFAULT true,
color_red SMALLINT DEFAULT NULL,
color_green SMALLINT DEFAULT NULL,
@@ -53,7 +53,7 @@ func DbInit(ctx context.Context) {
_, 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,
user_id INTEGER NOT NULL REFERENCES clients(id) ON DELETE CASCADE,
joined_at TIMESTAMP NOT NULL DEFAULT NOW(),
PRIMARY KEY (group_id, user_id)
)
@@ -73,22 +73,17 @@ func DbSaveClientWithoutGroups(ctx context.Context, client *Client) error {
return err
}
func DbGetIdByClientName(ctx context.Context, name string) (uint32, error) {
var id uint32
err := dbConn.QueryRow(ctx, `
SELECT id FROM clients WHERE name = $1
`, name).Scan(&id)
return id, err
}
func DbSetClientByName(ctx context.Context, client *Client) error {
err := dbConn.QueryRow(ctx, `
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
SELECT id, name, pass_hash, pronouns, color_red, color_green, color_blue, created_at FROM clients WHERE name = $1
`, client.Name).Scan(&client.Id, &client.Name, &client.PasswordHash, &client.Pronouns, &client.Color[0], &client.Color[1], &client.Color[2], &client.CreatedAt)
if err != nil {
return err
}
return DbSetClientGroups(ctx, client)
}
func DbSetClientById(ctx context.Context, client *Client) error {
func DbSetClientByIdWithoutGroups(ctx context.Context, client *Client) error {
err := dbConn.QueryRow(ctx, `
SELECT name, pass_hash, pronouns, color_red, color_green, color_blue, created_at FROM clients WHERE id = $1
`, client.Id).Scan(&client.Name, &client.PasswordHash, &client.Pronouns, &client.Color[0], &client.Color[1], &client.Color[2], &client.CreatedAt)
@@ -102,10 +97,17 @@ func DbSaveGroupWithoutClients(ctx context.Context, group *Group) error {
RETURNING id
`, group.Name, group.CreatorId, group.OwnerId, group.EnableClientColors, group.Color[0], group.Color[1], group.Color[2], group.CreatedAt).
Scan(&group.Id)
if err != nil {
return err
}
_, err = dbConn.Exec(ctx, `
INSERT INTO chat_group_members (group_id, user_id, joined_at)
VALUES ($1, $2, $3)
`, group.Id, group.OwnerId, group.CreatedAt)
return err
}
func DbSetGroupById(ctx context.Context, group *Group) error {
func DbSetGroupByIdWithoutClients(ctx context.Context, group *Group) error {
err := dbConn.QueryRow(ctx, `
SELECT name, creator_id, owner_id, enable_client_colors, color_red, color_green, color_blue, created_at FROM chat_groups WHERE id = $1
`, group.Id).Scan(&group.Name, &group.CreatorId, &group.OwnerId, &group.EnableClientColors, &group.Color[0], &group.Color[1], &group.Color[2], &group.CreatedAt)
@@ -132,6 +134,34 @@ func DbSetGroupById(ctx context.Context, group *Group) error {
return rows.Err()
}
func DbSetGroupById(ctx context.Context, group *Group) error {
err := DbSetGroupByIdWithoutClients(ctx, group)
if err != nil {
return err
}
return DbSetGroupMemberClients(ctx, group)
}
func DbSetGroupMemberClients(ctx context.Context, group *Group) error {
rows, err := dbConn.Query(ctx, `
SELECT user_id FROM chat_group_members WHERE group_id = $1
`, group.Id)
if err != nil {
return err
}
defer rows.Close()
group.Clients = make(map[uint32]struct{})
for rows.Next() {
var userId uint32
if err := rows.Scan(&userId); err != nil {
return err
}
group.Clients[userId] = struct{}{}
}
return rows.Err()
}
func DbAddClientsToGroup(ctx context.Context, groupId uint32, clientIds []uint32) error {
batch := &pgx.Batch{}
now := time.Now()
@@ -151,3 +181,23 @@ func DbAddClientsToGroup(ctx context.Context, groupId uint32, clientIds []uint32
}
return nil
}
func DbSetClientGroups(ctx context.Context, client *Client) error {
rows, err := dbConn.Query(ctx, `
SELECT group_id FROM chat_group_members WHERE user_id = $1
`, client.Id)
if err != nil {
return err
}
defer rows.Close()
client.Groups = make(map[uint32]struct{})
for rows.Next() {
var groupId uint32
if err := rows.Scan(&groupId); err != nil {
return err
}
client.Groups[groupId] = struct{}{}
}
return rows.Err()
}