complete hub save endpoint

This commit is contained in:
2026-04-23 20:07:09 +02:00
parent c0d7e53884
commit 8aaa27a6dc
3 changed files with 151 additions and 90 deletions
+79 -65
View File
@@ -73,67 +73,81 @@ func Init(ctx context.Context) {
}
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS hubs ()
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
CREATE TABLE IF NOT EXISTS hubs (
id UUID PRIMARY KEY,
creator_id UUID NOT NULL REFERENCES users(id),
name TEXT NOT NULL,
allow_user_color BOOLEAN DEFAULT TRUE,
allow_user_color BOOLEAN NOT NULL DEFAULT TRUE,
rgba BIGINT NOT NULL DEFAULT 0 CHECK (rgba BETWEEN 0 AND 4294967295),
created_at TIMESTAMP NOT NULL DEFAULT NOW()
)
`)
if err != nil {
panic(err)
}
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS hub_channel_groups ()
hub_id UUID PRIMARY KEY NOT NULL REFERENCES hubs(id) ON DELETE CASCADE,
channel_group_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
rgba BIGINT NOT NULL DEFAULT 0 CHECK (rgba BETWEEN 0 AND 4294967295),
`)
if err != nil {
panic(err)
}
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS hub_channels ()
hub_id UUID PRIMARY KEY NOT NULL REFERENCES hubs(id) ON DELETE CASCADE,
channel_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
parent_group_id UUID REFERENCES hub_channel_groups(id) ON DELETE CASCADE,
`)
if err != nil {
panic(err)
}
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS hub_users ()
hub_id UUID PRIMARY KEY NOT NULL REFERENCES hubs(id) ON DELETE CASCADE,
user_id UUID PRIMARY KEY NOT NULL REFERENCES users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
rgba BIGINT NOT NULL DEFAULT 0 CHECK (rgba BETWEEN 0 AND 4294967295),
created_at TIMESTAMP NOT NULL DEFAULT NOW()
`)
if err != nil {
panic(err)
}
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS hub_user_roles ()
user_id UUID PRIMARY KEY NOT NULL REFERENCES users(id) ON DELETE CASCADE,
role_id SMALLINT NOT NULL REFERENCES hub_roles(role_id) ON DELETE CASCADE
`)
if err != nil {
panic(err)
}
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS hub_roles ()
hub_id UUID PRIMARY KEY NOT NULL REFERENCES hubs(id) ON DELETE CASCADE,
role_id SMALLINT NOT NULL DEFAULT 0,
CREATE TABLE IF NOT EXISTS hub_roles (
hub_id UUID NOT NULL REFERENCES hubs(id) ON DELETE CASCADE,
role_id SMALLINT NOT NULL,
name TEXT NOT NULL,
permissions INTEGER NOT NULL DEFAULT 0,
rgba BIGINT NOT NULL DEFAULT 0 CHECK (rgba BETWEEN 0 AND 4294967295),
PRIMARY KEY (hub_id, role_id)
)
`)
if err != nil {
panic(err)
}
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS hub_channel_groups (
channel_group_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
hub_id UUID NOT NULL REFERENCES hubs(id) ON DELETE CASCADE,
name TEXT NOT NULL,
rgba BIGINT NOT NULL DEFAULT 0 CHECK (rgba BETWEEN 0 AND 4294967295),
position SMALLINT NOT NULL DEFAULT 0
)
`)
if err != nil {
panic(err)
}
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS hub_channels (
channel_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
hub_id UUID NOT NULL REFERENCES hubs(id) ON DELETE CASCADE,
name TEXT NOT NULL,
parent_group_id UUID REFERENCES hub_channel_groups(channel_group_id) ON DELETE CASCADE,
position SMALLINT NOT NULL DEFAULT 0
)
`)
if err != nil {
panic(err)
}
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS hub_users (
hub_id UUID NOT NULL REFERENCES hubs(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
PRIMARY KEY (hub_id, user_id)
)
`)
if err != nil {
panic(err)
}
_, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS hub_user_roles (
hub_id UUID NOT NULL,
user_id UUID NOT NULL,
role_id SMALLINT NOT NULL,
PRIMARY KEY (hub_id, user_id, role_id),
FOREIGN KEY (hub_id, user_id) REFERENCES hub_users(hub_id, user_id) ON DELETE CASCADE,
FOREIGN KEY (hub_id, role_id) REFERENCES hub_roles(hub_id, role_id) ON DELETE CASCADE
)
`)
if err != nil {
panic(err)
@@ -322,10 +336,10 @@ func ConnectionGetMessagesBefore(ctx context.Context, before time.Time, connecti
}
func HubSave(ctx context.Context, hub *types.Hub) error {
return dbConn.QueryRow(ctx, `
INSERT INTO hubs (creator_id, name, allow_user_color, rgba, created_at) VALUES ($1, $2, $3, $4, $5)
RETURNING id
`, hub.Creator, hub.Name, hub.AllowUserColor, convertions.RgbaToUint32(hub.Color), hub.CreatedAt).Scan(&hub.Id)
_, err := dbConn.Exec(ctx, `
INSERT INTO hubs (id, creator_id, name, allow_user_color, rgba, created_at) VALUES ($1, $2, $3, $4, $5, $6)
`, hub.Id, hub.Creator, hub.Name, hub.AllowUserColor, convertions.RgbaToUint32(hub.Color), hub.CreatedAt)
return err
}
func HubDelete(ctx context.Context, hub *types.Hub) error {
@@ -373,7 +387,7 @@ func HubGetUsers(ctx context.Context, hub *types.Hub) error {
rows, err := dbConn.Query(ctx, `
SELECT hu.user_id, hu.name, hu.created_at, COALESCE(array_agg(hur.role_id) FILTER (WHERE hur.role_id IS NOT NULL), '{}')
FROM hub_users hu
LEFT JOIN hub_user_roles hur ON hu.user_id = hur.user_id
LEFT JOIN hub_user_roles hur ON hu.hub_id = hur.hub_id AND hu.user_id = hur.user_id
WHERE hu.hub_id = $1
GROUP BY hu.user_id, hu.name, hu.created_at
`, hub.Id)
@@ -442,17 +456,17 @@ func HubUserRemove(ctx context.Context, hubId uuid.UUID, userId uuid.UUID) error
return err
}
func HubUserRoleAdd(ctx context.Context, userId uuid.UUID, roleId uint8) error {
func HubUserRoleAdd(ctx context.Context, hubId uuid.UUID, userId uuid.UUID, roleId uint8) error {
_, err := dbConn.Exec(ctx, `
INSERT INTO hub_user_roles (user_id, role_id) VALUES ($1, $2)
`, userId, roleId)
INSERT INTO hub_user_roles (hub_id, user_id, role_id) VALUES ($1, $2, $3)
`, hubId, userId, roleId)
return err
}
func HubUserRoleRemove(ctx context.Context, userId uuid.UUID, roleId uint8) error {
func HubUserRoleRemove(ctx context.Context, hubId uuid.UUID, userId uuid.UUID, roleId uint8) error {
_, err := dbConn.Exec(ctx, `
DELETE FROM hub_user_roles WHERE user_id = $1 AND role_id = $2
`, userId, roleId)
DELETE FROM hub_user_roles WHERE hub_id = $1 AND user_id = $2 AND role_id = $3
`, hubId, userId, roleId)
return err
}
@@ -478,10 +492,10 @@ func HubRoleUpdate(ctx context.Context, hubId uuid.UUID, role *types.HubRole) er
}
func HubChannelGroupSave(ctx context.Context, hubId uuid.UUID, cg *types.HubChannelGroup) error {
return dbConn.QueryRow(ctx, `
INSERT INTO hub_channel_groups (hub_id, name, rgba) VALUES ($1, $2, $3)
RETURNING channel_group_id
`, hubId, cg.Name, convertions.RgbaToUint32(cg.Color)).Scan(&cg.Id)
_, err := dbConn.Exec(ctx, `
INSERT INTO hub_channel_groups (channel_group_id, hub_id, name, rgba, position) VALUES ($1, $2, $3, $4, $5)
`, cg.Id, hubId, cg.Name, convertions.RgbaToUint32(cg.Color), cg.Position)
return err
}
func HubChannelGroupDelete(ctx context.Context, cgId uuid.UUID) error {
@@ -491,9 +505,9 @@ func HubChannelGroupDelete(ctx context.Context, cgId uuid.UUID) error {
func HubChannelSave(ctx context.Context, hubId uuid.UUID, ch *types.HubChannel) error {
return dbConn.QueryRow(ctx, `
INSERT INTO hub_channels (hub_id, name, parent_group_id) VALUES ($1, $2, $3)
INSERT INTO hub_channels (hub_id, name, parent_group_id, position) VALUES ($1, $2, $3, $4)
RETURNING channel_id
`, hubId, ch.Name, ch.ParentGroupId).Scan(&ch.Id)
`, hubId, ch.Name, ch.ParentGroupId, ch.Position).Scan(&ch.Id)
}
func HubChannelDelete(ctx context.Context, chId uuid.UUID) error {