change client to user
This commit is contained in:
+46
-46
@@ -63,57 +63,57 @@ func DbInit(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// DbSaveClientWithoutGroups saves client in db without groups and sets its id
|
||||
// DbSaveUserWithoutGroups saves user in db without groups and sets its id
|
||||
// return: error if not successful
|
||||
func DbSaveClientWithoutGroups(ctx context.Context, client *Client) error {
|
||||
func DbSaveUserWithoutGroups(ctx context.Context, user *User) error {
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
INSERT INTO clients (name, pass_hash, pronouns, color_red, color_green, color_blue, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id
|
||||
`, client.Name, client.PasswordHash, client.Pronouns, client.Color[0], client.Color[1], client.Color[2], client.CreatedAt).
|
||||
Scan(&client.Id)
|
||||
`, user.Name, user.PasswordHash, user.Pronouns, user.Color[0], user.Color[1], user.Color[2], user.CreatedAt).
|
||||
Scan(&user.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
// DbSetClientByName sets all fields of given struct with database's data using name
|
||||
// DbSetUserByName 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 {
|
||||
func DbSetUserByName(ctx context.Context, user *User) error {
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
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)
|
||||
`, user.Name).Scan(&user.Id, &user.Name, &user.PasswordHash, &user.Pronouns, &user.Color[0], &user.Color[1], &user.Color[2], &user.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return DbSetClientGroups(ctx, client)
|
||||
return DbSetUserGroups(ctx, user)
|
||||
}
|
||||
|
||||
// DbSetClientByIdWithoutGroups sets all fields of given struct with database's data using id, excluding groups
|
||||
// DbSetUserByIdWithoutGroups 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 {
|
||||
func DbSetUserByIdWithoutGroups(ctx context.Context, user *User) 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)
|
||||
`, user.Id).Scan(&user.Name, &user.PasswordHash, &user.Pronouns, &user.Color[0], &user.Color[1], &user.Color[2], &user.CreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
// DbSetClientById sets all fields of given struct with database's data using id, including groups
|
||||
// DbSetUserById 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)
|
||||
func DbSetUserById(ctx context.Context, user *User) error {
|
||||
err := DbSetUserByIdWithoutGroups(ctx, user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return DbSetClientGroups(ctx, client)
|
||||
return DbSetUserGroups(ctx, user)
|
||||
}
|
||||
|
||||
// DbSaveGroupWithoutClients saves group in db and sets its id, also adds the owner as first member
|
||||
// DbSaveGroupWithoutUsers 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 {
|
||||
func DbSaveGroupWithoutUsers(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).
|
||||
`, group.Name, group.CreatorId, group.OwnerId, group.EnableUserColors, group.Color[0], group.Color[1], group.Color[2], group.CreatedAt).
|
||||
Scan(&group.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -134,12 +134,12 @@ func DbDeleteGroup(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
|
||||
// DbSetGroupByIdWithoutUsers sets all fields of given struct with database's data using id, populates Users map with member ids but not their data
|
||||
// return: error if not successful
|
||||
func DbSetGroupByIdWithoutClients(ctx context.Context, group *Group) error {
|
||||
func DbSetGroupByIdWithoutUsers(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)
|
||||
`, group.Id).Scan(&group.Name, &group.CreatorId, &group.OwnerId, &group.EnableUserColors, &group.Color[0], &group.Color[1], &group.Color[2], &group.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -152,30 +152,30 @@ func DbSetGroupByIdWithoutClients(ctx context.Context, group *Group) error {
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
group.Clients = make(map[uint32]struct{})
|
||||
group.Users = make(map[uint32]struct{})
|
||||
for rows.Next() {
|
||||
var userId uint32
|
||||
if err := rows.Scan(&userId); err != nil {
|
||||
return err
|
||||
}
|
||||
group.Clients[userId] = struct{}{}
|
||||
group.Users[userId] = struct{}{}
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
// DbSetGroupById sets all fields of given struct with database's data using id, including full member client data
|
||||
// DbSetGroupById sets all fields of given struct with database's data using id, including full member user data
|
||||
// return: error if not successful
|
||||
func DbSetGroupById(ctx context.Context, group *Group) error {
|
||||
err := DbSetGroupByIdWithoutClients(ctx, group)
|
||||
err := DbSetGroupByIdWithoutUsers(ctx, group)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return DbSetGroupMemberClients(ctx, group)
|
||||
return DbSetGroupMemberUsers(ctx, group)
|
||||
}
|
||||
|
||||
// DbSetGroupMemberClients populates group's Clients map with ids of all members from database
|
||||
// DbSetGroupMemberUsers populates group's Users map with ids of all members from database
|
||||
// return: error if not successful
|
||||
func DbSetGroupMemberClients(ctx context.Context, group *Group) error {
|
||||
func DbSetGroupMemberUsers(ctx context.Context, group *Group) error {
|
||||
rows, err := dbConn.Query(ctx, `
|
||||
SELECT user_id FROM chat_group_members WHERE group_id = $1
|
||||
`, group.Id)
|
||||
@@ -184,32 +184,32 @@ func DbSetGroupMemberClients(ctx context.Context, group *Group) error {
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
group.Clients = make(map[uint32]struct{})
|
||||
group.Users = make(map[uint32]struct{})
|
||||
for rows.Next() {
|
||||
var userId uint32
|
||||
if err := rows.Scan(&userId); err != nil {
|
||||
return err
|
||||
}
|
||||
group.Clients[userId] = struct{}{}
|
||||
group.Users[userId] = struct{}{}
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
// DbAddClientsToGroup adds given clients to group in db, silently ignores already existing members
|
||||
// DbAddUsersToGroup adds given users 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 {
|
||||
func DbAddUsersToGroup(ctx context.Context, groupId uint32, userIds *[MaxUsersInGroup]uint32) error {
|
||||
batch := &pgx.Batch{}
|
||||
now := time.Now()
|
||||
var count int
|
||||
for _, cid := range clientIds {
|
||||
if cid == 0 {
|
||||
for _, uid := range userIds {
|
||||
if uid == 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)
|
||||
`, groupId, uid, now)
|
||||
count++
|
||||
}
|
||||
br := dbConn.SendBatch(ctx, batch)
|
||||
@@ -222,18 +222,18 @@ func DbAddClientsToGroup(ctx context.Context, groupId uint32, clientIds *[MaxCli
|
||||
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) {
|
||||
// DbRemoveUsersFromGroup removes given users from group in db, silently ignores not existing members
|
||||
// return: deleted users count, error if not successful
|
||||
func DbRemoveUsersFromGroup(ctx context.Context, groupId uint32, userIds *[MaxUsersInGroup]uint32) (int, error) {
|
||||
batch := &pgx.Batch{}
|
||||
var count int
|
||||
for _, cid := range clientIds {
|
||||
if cid == 0 {
|
||||
for _, uid := range userIds {
|
||||
if uid == 0 {
|
||||
continue
|
||||
}
|
||||
batch.Queue(`
|
||||
DELETE FROM chat_group_members WHERE group_id = $1 AND user_id = $2
|
||||
`, groupId, cid)
|
||||
`, groupId, uid)
|
||||
count++
|
||||
}
|
||||
br := dbConn.SendBatch(ctx, batch)
|
||||
@@ -249,24 +249,24 @@ func DbRemoveClientsFromGroup(ctx context.Context, groupId uint32, clientIds *[M
|
||||
return deleted, nil
|
||||
}
|
||||
|
||||
// DbSetClientGroups populates client's Groups map with ids of all groups the client belongs to from database
|
||||
// DbSetUserGroups populates user's Groups map with ids of all groups the user belongs to from database
|
||||
// return: error if not successful
|
||||
func DbSetClientGroups(ctx context.Context, client *Client) error {
|
||||
func DbSetUserGroups(ctx context.Context, user *User) error {
|
||||
rows, err := dbConn.Query(ctx, `
|
||||
SELECT group_id FROM chat_group_members WHERE user_id = $1
|
||||
`, client.Id)
|
||||
`, user.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
client.Groups = make(map[uint32]struct{})
|
||||
user.Groups = make(map[uint32]struct{})
|
||||
for rows.Next() {
|
||||
var groupId uint32
|
||||
if err := rows.Scan(&groupId); err != nil {
|
||||
return err
|
||||
}
|
||||
client.Groups[groupId] = struct{}{}
|
||||
user.Groups[groupId] = struct{}{}
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user