delete groups
This commit is contained in:
+1
-181
@@ -6,7 +6,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
@@ -66,44 +65,6 @@ func DbInit(ctx context.Context) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = dbConn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS chat_groups (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name TEXT NOT NULL,
|
||||
creator_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
owner_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
enable_client_colors BOOLEAN NOT NULL DEFAULT true,
|
||||
color_red SMALLINT DEFAULT NULL,
|
||||
color_green SMALLINT DEFAULT NULL,
|
||||
color_blue SMALLINT DEFAULT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
)
|
||||
`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = dbConn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS chat_messages (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
sender_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
group_id UUID NOT NULL REFERENCES chat_groups(id) ON DELETE CASCADE,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
content TEXT NOT NULL
|
||||
)
|
||||
`)
|
||||
|
||||
_, err = dbConn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS chat_group_members (
|
||||
group_id UUID NOT NULL REFERENCES chat_groups(id) ON DELETE CASCADE,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
joined_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
PRIMARY KEY (group_id, user_id)
|
||||
)
|
||||
`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func DbUserSave(ctx context.Context, user *User) error {
|
||||
@@ -137,26 +98,6 @@ func DbUserGetById(ctx context.Context, user *User) error {
|
||||
return err
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
user.Groups = make(map[uuid.UUID]struct{})
|
||||
for rows.Next() {
|
||||
var groupId uuid.UUID
|
||||
if err := rows.Scan(&groupId); err != nil {
|
||||
return err
|
||||
}
|
||||
user.Groups[groupId] = struct{}{}
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
func DbUserSetColor(ctx context.Context, user *User) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
UPDATE users SET color_red = $1, color_green = $2, color_blue = $3 WHERE id = $4
|
||||
@@ -175,9 +116,6 @@ func DbGetWholeUser(ctx context.Context, user *User) error {
|
||||
if err := DbUserGetById(ctx, user); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := DbUserGetGroups(ctx, user); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := DbConnectionsGetBelongingToUser(ctx, user); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -186,14 +124,6 @@ func DbGetWholeUser(ctx context.Context, user *User) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func DbGroupSetColor(ctx context.Context, group *Group) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
UPDATE chat_groups SET color_red = $1, color_green = $2, color_blue = $3 WHERE id = $4
|
||||
`, group.Color[0], group.Color[1], group.Color[2], group.Id)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func DbConnectionSave(ctx context.Context, conn *Connection) error {
|
||||
return dbConn.QueryRow(ctx, `
|
||||
INSERT INTO user_connections (requestor_id, recipient_id, state, created_at) VALUES ($1, $2, $3, $4)
|
||||
@@ -240,7 +170,7 @@ func DbConnectionUpdateState(ctx context.Context, conn *Connection) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func DbMessageSave(ctx context.Context, message *Message) error {
|
||||
func DbConnectionMessageSave(ctx context.Context, message *Message) error {
|
||||
if message.Id != (uuid.UUID{}) {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
INSERT INTO direct_messages (id, sender_id, receiver_id, created_at, content) VALUES ($1, $2, $3, $4, $5)
|
||||
@@ -282,113 +212,3 @@ func DbConnectionGetMessagesBefore(ctx context.Context, before time.Time, connec
|
||||
return messages, rows.Err()
|
||||
}
|
||||
|
||||
func DbGroupSave(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.EnableUserColors, group.Color[0], group.Color[1], group.Color[2], group.CreatedAt).
|
||||
Scan(&group.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = dbConn.Exec(ctx, `
|
||||
INSERT INTO chat_group_members (group_id, user_id, joined_at)
|
||||
VALUES ($1, $2, $3)
|
||||
`, group.Id, group.OwnerId, group.CreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbGroupDelete(ctx context.Context, group *Group) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
DELETE FROM chat_groups WHERE id = $1
|
||||
`, group.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
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 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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
group.Users = make(map[uuid.UUID]struct{})
|
||||
for rows.Next() {
|
||||
var userId uuid.UUID
|
||||
if err := rows.Scan(&userId); err != nil {
|
||||
return err
|
||||
}
|
||||
group.Users[userId] = struct{}{}
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
func DbGroupAddUsers(ctx context.Context, groupId uuid.UUID, userIds *[MaxUsersInGroup]uuid.UUID) error {
|
||||
batch := &pgx.Batch{}
|
||||
now := time.Now()
|
||||
var count int
|
||||
for _, uid := range userIds {
|
||||
if uid == (uuid.UUID{}) {
|
||||
continue
|
||||
}
|
||||
batch.Queue(`
|
||||
INSERT INTO chat_group_members (group_id, user_id, joined_at)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT DO NOTHING
|
||||
`, groupId, uid, now)
|
||||
count++
|
||||
}
|
||||
br := dbConn.SendBatch(ctx, batch)
|
||||
defer br.Close()
|
||||
for range count {
|
||||
if _, err := br.Exec(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DbGroupRemoveUsers(ctx context.Context, groupId uuid.UUID, userIds *[MaxUsersInGroup]uuid.UUID) (int, error) {
|
||||
batch := &pgx.Batch{}
|
||||
var count int
|
||||
for _, uid := range userIds {
|
||||
if uid == (uuid.UUID{}) {
|
||||
continue
|
||||
}
|
||||
batch.Queue(`
|
||||
DELETE FROM chat_group_members WHERE group_id = $1 AND user_id = $2
|
||||
`, groupId, uid)
|
||||
count++
|
||||
}
|
||||
br := dbConn.SendBatch(ctx, batch)
|
||||
defer br.Close()
|
||||
var deleted int
|
||||
for range count {
|
||||
tag, err := br.Exec()
|
||||
if err != nil {
|
||||
return deleted, err
|
||||
}
|
||||
deleted += int(tag.RowsAffected())
|
||||
}
|
||||
return deleted, nil
|
||||
}
|
||||
|
||||
func DbGroupSetOwnerId(ctx context.Context, group *Group) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
UPDATE chat_groups SET owner_id = $1 WHERE id = $2
|
||||
`, group.OwnerId, group.Id)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
DbGroup
|
||||
Reference in New Issue
Block a user