add db function for user connections
This commit is contained in:
+66
-8
@@ -33,6 +33,15 @@ func DbInit(ctx context.Context) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = dbConn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS user_connections (
|
||||
requestor_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
recipient_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
is_accepted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
)
|
||||
`)
|
||||
|
||||
_, err = dbConn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS chat_groups (
|
||||
id SERIAL PRIMARY KEY,
|
||||
@@ -63,9 +72,9 @@ func DbInit(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// DbUserSaveWithoutGroups saves user in db without groups and sets its id
|
||||
// DbUserSaveWithoutGroupsConnections saves user in db without groups and sets its id
|
||||
// return: error if not successful
|
||||
func DbUserSaveWithoutGroups(ctx context.Context, user *User) error {
|
||||
func DbUserSaveWithoutGroupsConnections(ctx context.Context, user *User) error {
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
INSERT INTO users (name, pass_hash, pronouns, color_red, color_green, color_blue, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
@@ -84,7 +93,7 @@ func DbUserDelete(ctx context.Context, id uint32) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// DbUserSetByName sets all fields of given struct with database's data using name
|
||||
// DbUserSetByName sets all fields of given struct with database's data
|
||||
// return: error if not successful
|
||||
func DbUserSetByName(ctx context.Context, user *User) error {
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
@@ -93,12 +102,16 @@ func DbUserSetByName(ctx context.Context, user *User) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return DbUserSetGroups(ctx, user)
|
||||
err = DbUserSetGroups(ctx, user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return DbUserSetConnections(ctx, user)
|
||||
}
|
||||
|
||||
// DbUserSetByIdWithoutGroups sets all fields of given struct with database's data using id, excluding groups
|
||||
// DbUserSetByIdWithoutGroupsConnections sets all fields of given struct with database's data using id, excluding groups
|
||||
// return: error if not successful
|
||||
func DbUserSetByIdWithoutGroups(ctx context.Context, user *User) error {
|
||||
func DbUserSetByIdWithoutGroupsConnections(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)
|
||||
@@ -108,11 +121,15 @@ func DbUserSetByIdWithoutGroups(ctx context.Context, user *User) error {
|
||||
// DbUserSetById sets all fields of given struct with database's data using id, including groups
|
||||
// return: error if not successful
|
||||
func DbUserSetById(ctx context.Context, user *User) error {
|
||||
err := DbUserSetByIdWithoutGroups(ctx, user)
|
||||
err := DbUserSetByIdWithoutGroupsConnections(ctx, user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return DbUserSetGroups(ctx, user)
|
||||
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
|
||||
@@ -137,6 +154,47 @@ func DbUserSetGroups(ctx context.Context, user *User) error {
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
func DbUserSetConnections(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
|
||||
`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var (
|
||||
requestorId uint32
|
||||
recipientId uint32
|
||||
isAccepted bool
|
||||
createdAt time.Time
|
||||
)
|
||||
err = rows.Scan(&requestorId, &recipientId, &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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
// DbUserSetColor set user's color based on id
|
||||
// return: error if not successful
|
||||
func DbUserSetColor(ctx context.Context, user *User) error {
|
||||
|
||||
Reference in New Issue
Block a user