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
+43 -1
View File
@@ -7,6 +7,8 @@ import (
"go-socket/packages/cache"
"go-socket/packages/postgresql"
"go-socket/packages/types"
"github.com/google/uuid"
)
func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
@@ -33,9 +35,49 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
hub.Color = types.Rgba{}.GetRandom()
hub.CreatedAt = time.Now()
rootGrp := &types.HubChannelGroup{
Id: uuid.New(),
Name: "root",
Color: types.Rgba{6, 2, 1, 255},
Position: uint8(0),
}
hub.ChannelGroups[rootGrp.Id] = rootGrp
rootRole := &types.HubRole{
Id: 0,
Name: "root",
Color: types.Rgba{255, 0, 0, 255},
RolePermission: ^types.RolePermission(0),
}
hub.Roles[rootRole.Id] = rootRole
rootUser := &types.HubUser{
Id: user.Id,
Username: user.Name,
Roles: []uint8{rootRole.Id},
CreatedAt: hub.CreatedAt,
}
hub.Users[rootUser.Id] = rootUser
err = postgresql.HubSave(ctx, hub)
if err != nil {
http.Error(response, "failed to save hub", http.StatusInternalServerError)
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
if err = postgresql.HubRoleSave(ctx, hub.Id, rootRole); err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
if err = postgresql.HubChannelGroupSave(ctx, hub.Id, rootGrp); err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
if err = postgresql.HubUserAdd(ctx, hub.Id, rootUser); err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
if err = postgresql.HubUserRoleAdd(ctx, hub.Id, user.Id, rootRole.Id); err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
cache.SaveHub(hub)