add client adding logic
This commit is contained in:
+22
@@ -2,7 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
@@ -121,3 +123,23 @@ func DbSetGroupById(ctx context.Context, group *Group) error {
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
func DbAddClientsToGroup(ctx context.Context, groupId uint32, clientIds []uint32) error {
|
||||
batch := &pgx.Batch{}
|
||||
now := time.Now()
|
||||
for _, cid := range clientIds {
|
||||
batch.Queue(`
|
||||
INSERT INTO chat_group_members (group_id, user_id, joined_at)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT DO NOTHING
|
||||
`, groupId, cid, now)
|
||||
}
|
||||
br := dbConn.SendBatch(ctx, batch)
|
||||
defer br.Close()
|
||||
for range clientIds {
|
||||
if _, err := br.Exec(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user