add hubs create in http

This commit is contained in:
2026-04-23 18:14:05 +02:00
parent 8a66a905cb
commit c0d7e53884
6 changed files with 299 additions and 55 deletions
+178 -5
View File
@@ -263,7 +263,7 @@ func ConnectionsGetBelongingToUser(ctx context.Context, user *types.User) error
}
for rows.Next() {
conn := &types.Connection{}
conn := types.NewConnection()
if err = rows.Scan(&conn.Id, &conn.RequestorId, &conn.RecipientId, &conn.State, &conn.CreatedAt); err != nil {
return fmt.Errorf("scanning connection row: %w", err)
}
@@ -321,9 +321,182 @@ func ConnectionGetMessagesBefore(ctx context.Context, before time.Time, connecti
return messages, rows.Err()
}
func HubCreate(ctx context.Context, hub *types.Hub, creator *types.User) error {
_, err := dbConn.Exec(ctx, `
INSERT INTO hubs (id, creator_id, name, allow_user_color, rgba, created_at) VALUES ($1, $2, $3, $4, $5)
`, hub.Id, creator.Id, hub.Name, hub.AllowUserColor, convertions.RgbaToUint32(hub.Color), hub.CreatedAt)
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)
}
func HubDelete(ctx context.Context, hub *types.Hub) error {
_, err := dbConn.Exec(ctx, `DELETE FROM hubs WHERE id = $1`, hub.Id)
return err
}
func HubGet(ctx context.Context, hub *types.Hub) error {
var rgba uint32
err := dbConn.QueryRow(ctx, `
SELECT creator_id, name, allow_user_color, rgba, created_at FROM hubs WHERE id = $1
`, hub.Id).Scan(&hub.Creator, &hub.Name, &hub.AllowUserColor, &rgba, &hub.CreatedAt)
if err == nil {
hub.Color = convertions.Uint32ToRgba(rgba)
}
return err
}
func HubsGetBelongingToUser(ctx context.Context, userId uuid.UUID) ([]*types.Hub, error) {
rows, err := dbConn.Query(ctx, `
SELECT h.id, h.creator_id, h.name, h.allow_user_color, h.rgba, h.created_at
FROM hubs h
JOIN hub_users hu ON h.id = hu.hub_id
WHERE hu.user_id = $1
`, userId)
if err != nil {
return nil, err
}
defer rows.Close()
var hubs []*types.Hub
for rows.Next() {
hub := types.NewHub()
var rgba uint32
if err = rows.Scan(&hub.Id, &hub.Creator, &hub.Name, &hub.AllowUserColor, &rgba, &hub.CreatedAt); err != nil {
return nil, fmt.Errorf("scanning hub row: %w", err)
}
hub.Color = convertions.Uint32ToRgba(rgba)
hubs = append(hubs, hub)
}
return hubs, rows.Err()
}
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
WHERE hu.hub_id = $1
GROUP BY hu.user_id, hu.name, hu.created_at
`, hub.Id)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
hu := &types.HubUser{}
var roles []int16
if err = rows.Scan(&hu.Id, &hu.Username, &hu.CreatedAt, &roles); err != nil {
return fmt.Errorf("scanning hub user row: %w", err)
}
hu.Roles = make([]uint8, len(roles))
for i, r := range roles {
hu.Roles[i] = uint8(r)
}
hub.Users[hu.Id] = hu
}
return rows.Err()
}
func HubGetRoles(ctx context.Context, hub *types.Hub) error {
rows, err := dbConn.Query(ctx, `
SELECT role_id, name, permissions, rgba FROM hub_roles WHERE hub_id = $1
`, hub.Id)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
hr := &types.HubRole{}
var rgba uint32
if err = rows.Scan(&hr.Id, &hr.Name, &hr.RolePermission, &rgba); err != nil {
return fmt.Errorf("scanning hub role row: %w", err)
}
hr.Color = convertions.Uint32ToRgba(rgba)
hub.Roles[hr.Id] = hr
}
return rows.Err()
}
func GetWholeHub(ctx context.Context, hub *types.Hub) error {
if err := HubGet(ctx, hub); err != nil {
return err
}
if err := HubGetUsers(ctx, hub); err != nil {
return err
}
return HubGetRoles(ctx, hub)
}
func HubUserAdd(ctx context.Context, hubId uuid.UUID, hu *types.HubUser) error {
_, err := dbConn.Exec(ctx, `
INSERT INTO hub_users (hub_id, user_id, name, created_at) VALUES ($1, $2, $3, $4)
`, hubId, hu.Id, hu.Username, hu.CreatedAt)
return err
}
func HubUserRemove(ctx context.Context, hubId uuid.UUID, userId uuid.UUID) error {
_, err := dbConn.Exec(ctx, `
DELETE FROM hub_users WHERE hub_id = $1 AND user_id = $2
`, hubId, userId)
return err
}
func HubUserRoleAdd(ctx context.Context, userId uuid.UUID, roleId uint8) error {
_, err := dbConn.Exec(ctx, `
INSERT INTO hub_user_roles (user_id, role_id) VALUES ($1, $2)
`, userId, roleId)
return err
}
func HubUserRoleRemove(ctx context.Context, 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)
return err
}
func HubRoleSave(ctx context.Context, hubId uuid.UUID, role *types.HubRole) error {
_, err := dbConn.Exec(ctx, `
INSERT INTO hub_roles (hub_id, role_id, name, permissions, rgba) VALUES ($1, $2, $3, $4, $5)
`, hubId, role.Id, role.Name, role.RolePermission, convertions.RgbaToUint32(role.Color))
return err
}
func HubRoleDelete(ctx context.Context, hubId uuid.UUID, roleId uint8) error {
_, err := dbConn.Exec(ctx, `
DELETE FROM hub_roles WHERE hub_id = $1 AND role_id = $2
`, hubId, roleId)
return err
}
func HubRoleUpdate(ctx context.Context, hubId uuid.UUID, role *types.HubRole) error {
_, err := dbConn.Exec(ctx, `
UPDATE hub_roles SET name = $1, permissions = $2, rgba = $3 WHERE hub_id = $4 AND role_id = $5
`, role.Name, role.RolePermission, convertions.RgbaToUint32(role.Color), hubId, role.Id)
return err
}
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)
}
func HubChannelGroupDelete(ctx context.Context, cgId uuid.UUID) error {
_, err := dbConn.Exec(ctx, `DELETE FROM hub_channel_groups WHERE channel_group_id = $1`, cgId)
return err
}
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)
RETURNING channel_id
`, hubId, ch.Name, ch.ParentGroupId).Scan(&ch.Id)
}
func HubChannelDelete(ctx context.Context, chId uuid.UUID) error {
_, err := dbConn.Exec(ctx, `DELETE FROM hub_channels WHERE channel_id = $1`, chId)
return err
}