fix db function naming, connections query, and nil map init
This commit is contained in:
+25
-27
@@ -89,21 +89,21 @@ func DbUserDelete(ctx context.Context, id uint32) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func DbGetUserByName(ctx context.Context, user *User) error {
|
||||
func DbUserGetByName(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 users WHERE name = $1
|
||||
`, user.Name).Scan(&user.Id, &user.Name, &user.PasswordHash, &user.Pronouns, &user.Color[0], &user.Color[1], &user.Color[2], &user.CreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbGetUserById(ctx context.Context, user *User) error {
|
||||
func DbUserGetById(ctx context.Context, user *User) error {
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
SELECT name, pass_hash, pronouns, color_red, color_green, color_blue, created_at FROM users WHERE id = $1
|
||||
`, user.Id).Scan(&user.Name, &user.PasswordHash, &user.Pronouns, &user.Color[0], &user.Color[1], &user.Color[2], &user.CreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbGetUserGroups(ctx context.Context, user *User) error {
|
||||
func DbUserGetGroups(ctx context.Context, user *User) error {
|
||||
rows, err := dbConn.Query(ctx, `
|
||||
SELECT group_id FROM chat_group_members WHERE user_id = $1
|
||||
`, user.Id)
|
||||
@@ -123,41 +123,39 @@ func DbGetUserGroups(ctx context.Context, user *User) error {
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
func DbGetUserConnections(ctx context.Context, user *User) error {
|
||||
func DbUserGetConnections(ctx context.Context, user *User) error {
|
||||
rows, err := dbConn.Query(ctx, `
|
||||
SELECT requestor_id, recipient_id, is_accepted, created_at FROM user_connections WHERE requestor_id = $1 or recipient_id = $1
|
||||
`)
|
||||
SELECT
|
||||
CASE WHEN requestor_id = $1 THEN recipient_id ELSE requestor_id END AS other_id,
|
||||
requestor_id = $1 AS is_from_user,
|
||||
is_accepted,
|
||||
created_at
|
||||
FROM user_connections
|
||||
WHERE requestor_id = $1 OR recipient_id = $1
|
||||
`, user.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user.Connections = make(map[uint32]*Connection)
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var (
|
||||
requestorId uint32
|
||||
recipientId uint32
|
||||
isAccepted bool
|
||||
createdAt time.Time
|
||||
otherId uint32
|
||||
isFromUser bool
|
||||
isAccepted bool
|
||||
createdAt time.Time
|
||||
)
|
||||
err = rows.Scan(&requestorId, &recipientId, &isAccepted, &createdAt)
|
||||
err = rows.Scan(&otherId, &isFromUser, &isAccepted, &createdAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if requestorId == user.Id {
|
||||
user.Connections[recipientId] = &Connection{
|
||||
CreatedAt: createdAt,
|
||||
With: recipientId,
|
||||
IsFromUser: true,
|
||||
IsAccepted: isAccepted,
|
||||
}
|
||||
} else {
|
||||
user.Connections[requestorId] = &Connection{
|
||||
CreatedAt: createdAt,
|
||||
With: requestorId,
|
||||
IsFromUser: false,
|
||||
IsAccepted: isAccepted,
|
||||
}
|
||||
user.Connections[otherId] = &Connection{
|
||||
CreatedAt: createdAt,
|
||||
With: otherId,
|
||||
IsFromUser: isFromUser,
|
||||
IsAccepted: isAccepted,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,14 +208,14 @@ func DbGroupDelete(ctx context.Context, group *Group) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func DbGetGroupById(ctx context.Context, group *Group) error {
|
||||
func DbGroupGetById(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.EnableUserColors, &group.Color[0], &group.Color[1], &group.Color[2], &group.CreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbGetGroupMembers(ctx context.Context, group *Group) error {
|
||||
func DbGroupGetMembers(ctx context.Context, group *Group) error {
|
||||
rows, err := dbConn.Query(ctx, `
|
||||
SELECT user_id FROM chat_group_members WHERE group_id = $1
|
||||
`, group.Id)
|
||||
|
||||
Reference in New Issue
Block a user