add brief for functions in db, implemet full group manipulation (except owner,color), some fixes

This commit is contained in:
2026-04-01 20:13:50 +02:00
parent 81d0d19064
commit b00eff5c47
9 changed files with 150 additions and 26 deletions
+54 -2
View File
@@ -63,6 +63,8 @@ func DbInit(ctx context.Context) {
}
}
// DbSaveClientWithoutGroups saves client in db without groups and sets its id
// return: error if not successful
func DbSaveClientWithoutGroups(ctx context.Context, client *Client) error {
err := dbConn.QueryRow(ctx, `
INSERT INTO clients (name, pass_hash, pronouns, color_red, color_green, color_blue, created_at)
@@ -73,6 +75,8 @@ func DbSaveClientWithoutGroups(ctx context.Context, client *Client) error {
return err
}
// DbSetClientByName sets all fields of given struct with database's data using name
// return: error if not successful
func DbSetClientByName(ctx context.Context, client *Client) error {
err := dbConn.QueryRow(ctx, `
SELECT id, name, pass_hash, pronouns, color_red, color_green, color_blue, created_at FROM clients WHERE name = $1
@@ -83,6 +87,8 @@ func DbSetClientByName(ctx context.Context, client *Client) error {
return DbSetClientGroups(ctx, client)
}
// DbSetClientByIdWithoutGroups sets all fields of given struct with database's data using id, excluding groups
// return: error if not successful
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
@@ -90,6 +96,8 @@ func DbSetClientByIdWithoutGroups(ctx context.Context, client *Client) error {
return err
}
// DbSetClientById sets all fields of given struct with database's data using id, including groups
// return: error if not successful
func DbSetClientById(ctx context.Context, client *Client) error {
err := DbSetClientByIdWithoutGroups(ctx, client)
if err != nil {
@@ -98,6 +106,8 @@ func DbSetClientById(ctx context.Context, client *Client) error {
return DbSetClientGroups(ctx, client)
}
// DbSaveGroupWithoutClients saves group in db and sets its id, also adds the owner as first member
// return: error if not successful
func DbSaveGroupWithoutClients(ctx context.Context, group *Group) error {
err := dbConn.QueryRow(ctx, `
INSERT INTO chat_groups (name, creator_id, owner_id, enable_client_colors, color_red, color_green, color_blue, created_at)
@@ -115,6 +125,8 @@ func DbSaveGroupWithoutClients(ctx context.Context, group *Group) error {
return err
}
// DbSetGroupByIdWithoutClients sets all fields of given struct with database's data using id, populates Clients map with member ids but not their data
// return: error if not successful
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
@@ -142,6 +154,8 @@ func DbSetGroupByIdWithoutClients(ctx context.Context, group *Group) error {
return rows.Err()
}
// DbSetGroupById sets all fields of given struct with database's data using id, including full member client data
// return: error if not successful
func DbSetGroupById(ctx context.Context, group *Group) error {
err := DbSetGroupByIdWithoutClients(ctx, group)
if err != nil {
@@ -150,6 +164,8 @@ func DbSetGroupById(ctx context.Context, group *Group) error {
return DbSetGroupMemberClients(ctx, group)
}
// DbSetGroupMemberClients populates group's Clients map with ids of all members from database
// return: error if not successful
func DbSetGroupMemberClients(ctx context.Context, group *Group) error {
rows, err := dbConn.Query(ctx, `
SELECT user_id FROM chat_group_members WHERE group_id = $1
@@ -170,19 +186,26 @@ func DbSetGroupMemberClients(ctx context.Context, group *Group) error {
return rows.Err()
}
func DbAddClientsToGroup(ctx context.Context, groupId uint32, clientIds []uint32) error {
// DbAddClientsToGroup adds given clients to group in db, silently ignores already existing members
// return: error if not successful
func DbAddClientsToGroup(ctx context.Context, groupId uint32, clientIds *[MaxClientsInGroup]uint32) error {
batch := &pgx.Batch{}
now := time.Now()
var count int
for _, cid := range clientIds {
if cid == 0 {
continue
}
batch.Queue(`
INSERT INTO chat_group_members (group_id, user_id, joined_at)
VALUES ($1, $2, $3)
ON CONFLICT DO NOTHING
`, groupId, cid, now)
count++
}
br := dbConn.SendBatch(ctx, batch)
defer br.Close()
for range clientIds {
for range count {
if _, err := br.Exec(); err != nil {
return err
}
@@ -190,6 +213,35 @@ func DbAddClientsToGroup(ctx context.Context, groupId uint32, clientIds []uint32
return nil
}
// DbRemoveClientsFromGroup removes given clients from group in db, silently ignores not existing members
// return: deleted clients count, error if not successful
func DbRemoveClientsFromGroup(ctx context.Context, groupId uint32, clientIds *[MaxClientsInGroup]uint32) (int, error) {
batch := &pgx.Batch{}
var count int
for _, cid := range clientIds {
if cid == 0 {
continue
}
batch.Queue(`
DELETE FROM chat_group_members WHERE group_id = $1 AND user_id = $2
`, groupId, cid)
count++
}
br := dbConn.SendBatch(ctx, batch)
defer br.Close()
var deleted int
for range count {
tag, err := br.Exec()
if err != nil {
return deleted, err
}
deleted += int(tag.RowsAffected())
}
return deleted, nil
}
// DbSetClientGroups populates client's Groups map with ids of all groups the client belongs to from database
// return: error if not successful
func DbSetClientGroups(ctx context.Context, client *Client) error {
rows, err := dbConn.Query(ctx, `
SELECT group_id FROM chat_group_members WHERE user_id = $1