ad group logic except somes
This commit is contained in:
+45
-1
@@ -37,7 +37,7 @@ func DbInit(ctx context.Context) {
|
||||
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,
|
||||
enable_client_colors BOOLEAN NOT NULL DEFAULT true,
|
||||
color_red SMALLINT DEFAULT NULL,
|
||||
color_green SMALLINT DEFAULT NULL,
|
||||
color_blue SMALLINT DEFAULT NULL,
|
||||
@@ -77,3 +77,47 @@ func DbSetClientByName(ctx context.Context, client *Client) error {
|
||||
`, client.Name).Scan(&client.Name, &client.PasswordHash, client.Pronouns, client.Color[0], client.Color[1], client.Color[2], client.CreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbSetClientById(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)
|
||||
return err
|
||||
}
|
||||
|
||||
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)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING id
|
||||
`, group.Name, group.CreatorId, group.OwnerId, group.EnableClientColors, group.Color[0], group.Color[1], group.Color[2], group.CreatedAt).
|
||||
Scan(&group.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbSetGroupById(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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user