refactor db functions
This commit is contained in:
+9
-90
@@ -72,9 +72,7 @@ func DbInit(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbUserSaveWithoutGroupsConnections saves user in db without groups and sets its id
|
func DbUserSave(ctx context.Context, user *User) error {
|
||||||
// return: error if not successful
|
|
||||||
func DbUserSaveWithoutGroupsConnections(ctx context.Context, user *User) error {
|
|
||||||
err := dbConn.QueryRow(ctx, `
|
err := dbConn.QueryRow(ctx, `
|
||||||
INSERT INTO users (name, pass_hash, pronouns, color_red, color_green, color_blue, created_at)
|
INSERT INTO users (name, pass_hash, pronouns, color_red, color_green, color_blue, created_at)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||||
@@ -84,8 +82,6 @@ func DbUserSaveWithoutGroupsConnections(ctx context.Context, user *User) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbUserDelete deletes given user by id
|
|
||||||
// return: error if not successful
|
|
||||||
func DbUserDelete(ctx context.Context, id uint32) error {
|
func DbUserDelete(ctx context.Context, id uint32) error {
|
||||||
_, err := dbConn.Exec(ctx, `
|
_, err := dbConn.Exec(ctx, `
|
||||||
DELETE FROM users WHERE id = $1
|
DELETE FROM users WHERE id = $1
|
||||||
@@ -93,48 +89,21 @@ func DbUserDelete(ctx context.Context, id uint32) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbUserSetByName sets all fields of given struct with database's data
|
func DbGetUserByName(ctx context.Context, user *User) error {
|
||||||
// return: error if not successful
|
|
||||||
func DbUserSetByName(ctx context.Context, user *User) error {
|
|
||||||
err := dbConn.QueryRow(ctx, `
|
err := dbConn.QueryRow(ctx, `
|
||||||
SELECT id, name, pass_hash, pronouns, color_red, color_green, color_blue, created_at FROM users WHERE name = $1
|
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)
|
`, 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 err
|
||||||
}
|
|
||||||
err = DbUserSetGroups(ctx, user)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return DbUserSetConnections(ctx, user)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbUserSetByIdWithoutGroupsConnections sets all fields of given struct with database's data using id, excluding groups
|
func DbGetUserById(ctx context.Context, user *User) error {
|
||||||
// return: error if not successful
|
|
||||||
func DbUserSetByIdWithoutGroupsConnections(ctx context.Context, user *User) error {
|
|
||||||
err := dbConn.QueryRow(ctx, `
|
err := dbConn.QueryRow(ctx, `
|
||||||
SELECT name, pass_hash, pronouns, color_red, color_green, color_blue, created_at FROM users WHERE id = $1
|
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)
|
`, user.Id).Scan(&user.Name, &user.PasswordHash, &user.Pronouns, &user.Color[0], &user.Color[1], &user.Color[2], &user.CreatedAt)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbUserSetById sets all fields of given struct with database's data using id, including groups
|
func DbGetUserGroups(ctx context.Context, user *User) error {
|
||||||
// return: error if not successful
|
|
||||||
func DbUserSetById(ctx context.Context, user *User) error {
|
|
||||||
err := DbUserSetByIdWithoutGroupsConnections(ctx, user)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = DbUserSetGroups(ctx, user)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return DbUserSetConnections(ctx, user)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DbUserSetGroups populates user's Groups map with ids of all groups the user belongs to from database
|
|
||||||
// return: error if not successful
|
|
||||||
func DbUserSetGroups(ctx context.Context, user *User) error {
|
|
||||||
rows, err := dbConn.Query(ctx, `
|
rows, err := dbConn.Query(ctx, `
|
||||||
SELECT group_id FROM chat_group_members WHERE user_id = $1
|
SELECT group_id FROM chat_group_members WHERE user_id = $1
|
||||||
`, user.Id)
|
`, user.Id)
|
||||||
@@ -154,7 +123,7 @@ func DbUserSetGroups(ctx context.Context, user *User) error {
|
|||||||
return rows.Err()
|
return rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func DbUserSetConnections(ctx context.Context, user *User) error {
|
func DbGetUserConnections(ctx context.Context, user *User) error {
|
||||||
rows, err := dbConn.Query(ctx, `
|
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 requestor_id, recipient_id, is_accepted, created_at FROM user_connections WHERE requestor_id = $1 or recipient_id = $1
|
||||||
`)
|
`)
|
||||||
@@ -195,8 +164,6 @@ func DbUserSetConnections(ctx context.Context, user *User) error {
|
|||||||
return rows.Err()
|
return rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbUserSetColor set user's color based on id
|
|
||||||
// return: error if not successful
|
|
||||||
func DbUserSetColor(ctx context.Context, user *User) error {
|
func DbUserSetColor(ctx context.Context, user *User) error {
|
||||||
_, err := dbConn.Exec(ctx, `
|
_, err := dbConn.Exec(ctx, `
|
||||||
UPDATE users SET color_red = $1, color_green = $2, color_blue = $3 WHERE id = $4
|
UPDATE users SET color_red = $1, color_green = $2, color_blue = $3 WHERE id = $4
|
||||||
@@ -204,8 +171,6 @@ func DbUserSetColor(ctx context.Context, user *User) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbUserSetPronouns set user's pronouns based on id
|
|
||||||
// return: error if not successful
|
|
||||||
func DbUserSetPronouns(ctx context.Context, user *User) error {
|
func DbUserSetPronouns(ctx context.Context, user *User) error {
|
||||||
_, err := dbConn.Exec(ctx, `
|
_, err := dbConn.Exec(ctx, `
|
||||||
UPDATE users SET pronouns = $1 WHERE id = $2
|
UPDATE users SET pronouns = $1 WHERE id = $2
|
||||||
@@ -213,8 +178,6 @@ func DbUserSetPronouns(ctx context.Context, user *User) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbGroupSetColor set group's color based on id
|
|
||||||
// return: error if not successful
|
|
||||||
func DbGroupSetColor(ctx context.Context, group *Group) error {
|
func DbGroupSetColor(ctx context.Context, group *Group) error {
|
||||||
_, err := dbConn.Exec(ctx, `
|
_, err := dbConn.Exec(ctx, `
|
||||||
UPDATE chat_groups SET color_red = $1, color_green = $2, color_blue = $3 WHERE id = $4
|
UPDATE chat_groups SET color_red = $1, color_green = $2, color_blue = $3 WHERE id = $4
|
||||||
@@ -223,9 +186,7 @@ func DbGroupSetColor(ctx context.Context, group *Group) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbGroupSaveWithoutUsers saves group in db and sets its id, also adds the owner as first member
|
func DbGroupSave(ctx context.Context, group *Group) error {
|
||||||
// return: error if not successful
|
|
||||||
func DbGroupSaveWithoutUsers(ctx context.Context, group *Group) error {
|
|
||||||
err := dbConn.QueryRow(ctx, `
|
err := dbConn.QueryRow(ctx, `
|
||||||
INSERT INTO chat_groups (name, creator_id, owner_id, enable_client_colors, color_red, color_green, color_blue, created_at)
|
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)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||||
@@ -242,8 +203,6 @@ func DbGroupSaveWithoutUsers(ctx context.Context, group *Group) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbGroupDelete deletes given group by id
|
|
||||||
// return: error if not successful
|
|
||||||
func DbGroupDelete(ctx context.Context, group *Group) error {
|
func DbGroupDelete(ctx context.Context, group *Group) error {
|
||||||
_, err := dbConn.Exec(ctx, `
|
_, err := dbConn.Exec(ctx, `
|
||||||
DELETE FROM chat_groups WHERE id = $1
|
DELETE FROM chat_groups WHERE id = $1
|
||||||
@@ -251,16 +210,14 @@ func DbGroupDelete(ctx context.Context, group *Group) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbGroupSetWithoutUsersById sets all fields of given struct with database's data using id, populates Users map with member ids but not their data
|
func DbGetGroupById(ctx context.Context, group *Group) error {
|
||||||
// return: error if not successful
|
|
||||||
func DbGroupSetWithoutUsersById(ctx context.Context, group *Group) error {
|
|
||||||
err := dbConn.QueryRow(ctx, `
|
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
|
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)
|
`, 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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DbGetGroupMembers(ctx context.Context, group *Group) error {
|
||||||
rows, err := dbConn.Query(ctx, `
|
rows, err := dbConn.Query(ctx, `
|
||||||
SELECT user_id FROM chat_group_members WHERE group_id = $1
|
SELECT user_id FROM chat_group_members WHERE group_id = $1
|
||||||
`, group.Id)
|
`, group.Id)
|
||||||
@@ -280,40 +237,6 @@ func DbGroupSetWithoutUsersById(ctx context.Context, group *Group) error {
|
|||||||
return rows.Err()
|
return rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbGroupSetById sets all fields of given struct with database's data using id, including full member user data
|
|
||||||
// return: error if not successful
|
|
||||||
func DbGroupSetById(ctx context.Context, group *Group) error {
|
|
||||||
err := DbGroupSetWithoutUsersById(ctx, group)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return DbGroupSetMembers(ctx, group)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DbGroupSetMembers populates group's Users map with ids of all members from database
|
|
||||||
// return: error if not successful
|
|
||||||
func DbGroupSetMembers(ctx context.Context, group *Group) error {
|
|
||||||
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.Users = make(map[uint32]struct{})
|
|
||||||
for rows.Next() {
|
|
||||||
var userId uint32
|
|
||||||
if err := rows.Scan(&userId); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
group.Users[userId] = struct{}{}
|
|
||||||
}
|
|
||||||
return rows.Err()
|
|
||||||
}
|
|
||||||
|
|
||||||
// DbGroupAddUsers adds given users to group in db, silently ignores already existing members
|
|
||||||
// return: error if not successful
|
|
||||||
func DbGroupAddUsers(ctx context.Context, groupId uint32, userIds *[MaxUsersInGroup]uint32) error {
|
func DbGroupAddUsers(ctx context.Context, groupId uint32, userIds *[MaxUsersInGroup]uint32) error {
|
||||||
batch := &pgx.Batch{}
|
batch := &pgx.Batch{}
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
@@ -339,8 +262,6 @@ func DbGroupAddUsers(ctx context.Context, groupId uint32, userIds *[MaxUsersInGr
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbGroupRemoveUsers removes given users from group in db, silently ignores not existing members
|
|
||||||
// return: deleted users count, error if not successful
|
|
||||||
func DbGroupRemoveUsers(ctx context.Context, groupId uint32, userIds *[MaxUsersInGroup]uint32) (int, error) {
|
func DbGroupRemoveUsers(ctx context.Context, groupId uint32, userIds *[MaxUsersInGroup]uint32) (int, error) {
|
||||||
batch := &pgx.Batch{}
|
batch := &pgx.Batch{}
|
||||||
var count int
|
var count int
|
||||||
@@ -366,8 +287,6 @@ func DbGroupRemoveUsers(ctx context.Context, groupId uint32, userIds *[MaxUsersI
|
|||||||
return deleted, nil
|
return deleted, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DbGroupSetOwnerId set group's owner based on id
|
|
||||||
// return: error if not successful
|
|
||||||
func DbGroupSetOwnerId(ctx context.Context, group *Group) error {
|
func DbGroupSetOwnerId(ctx context.Context, group *Group) error {
|
||||||
_, err := dbConn.Exec(ctx, `
|
_, err := dbConn.Exec(ctx, `
|
||||||
UPDATE chat_groups SET owner_id = $1 WHERE id = $2
|
UPDATE chat_groups SET owner_id = $1 WHERE id = $2
|
||||||
|
|||||||
@@ -31,8 +31,13 @@ func getUser(ctx context.Context, token string) (*User, error) {
|
|||||||
user, err := CacheGetUserById(userId)
|
user, err := CacheGetUserById(userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
user = &User{Id: userId}
|
user = &User{Id: userId}
|
||||||
err = DbUserSetById(ctx, user)
|
if err = DbGetUserById(ctx, user); err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
|
}
|
||||||
|
if err = DbGetUserGroups(ctx, user); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err = DbGetUserConnections(ctx, user); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
CacheSaveUser(user)
|
CacheSaveUser(user)
|
||||||
@@ -45,8 +50,10 @@ func getGroup(ctx context.Context, groupId uint32) (*Group, error) {
|
|||||||
group, err := CacheGetGroup(groupId)
|
group, err := CacheGetGroup(groupId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
group = &Group{Id: groupId}
|
group = &Group{Id: groupId}
|
||||||
err = DbGroupSetById(ctx, group)
|
if err = DbGetGroupById(ctx, group); err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
|
}
|
||||||
|
if err = DbGetGroupMembers(ctx, group); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
CacheSaveGroup(group)
|
CacheSaveGroup(group)
|
||||||
@@ -125,7 +132,7 @@ func HttpHandleUserNew(response http.ResponseWriter, request *http.Request) {
|
|||||||
|
|
||||||
ctx := request.Context()
|
ctx := request.Context()
|
||||||
|
|
||||||
err = DbUserSaveWithoutGroupsConnections(ctx, newUser)
|
err = DbUserSave(ctx, newUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(response, "name taken", http.StatusUnauthorized)
|
http.Error(response, "name taken", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
@@ -236,9 +243,15 @@ func HttpHandleNewToken(response http.ResponseWriter, request *http.Request) {
|
|||||||
user, err = CacheGetUserByName(username)
|
user, err = CacheGetUserByName(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
user = &User{Name: username}
|
user = &User{Name: username}
|
||||||
|
if err = DbGetUserByName(ctx, user); err != nil {
|
||||||
err := DbUserSetByName(ctx, user)
|
http.Error(response, "bad login1", http.StatusUnauthorized)
|
||||||
if err != nil {
|
return
|
||||||
|
}
|
||||||
|
if err = DbGetUserGroups(ctx, user); err != nil {
|
||||||
|
http.Error(response, "bad login1", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = DbGetUserConnections(ctx, user); err != nil {
|
||||||
http.Error(response, "bad login1", http.StatusUnauthorized)
|
http.Error(response, "bad login1", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -473,7 +486,7 @@ func HttpHandleGroupChangeOwner(response http.ResponseWriter, request *http.Requ
|
|||||||
newOwner, err := CacheGetUserByName(newOwnerName)
|
newOwner, err := CacheGetUserByName(newOwnerName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
newOwner = &User{Name: newOwnerName}
|
newOwner = &User{Name: newOwnerName}
|
||||||
err = DbUserSetByName(ctx, newOwner)
|
err = DbGetUserByName(ctx, newOwner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(response, "user not in group", http.StatusBadRequest)
|
http.Error(response, "user not in group", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user