add hub get logic and part of client
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package httpRequest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"maps"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -80,6 +83,7 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
hub.Id = uuid.New()
|
||||
hub.Creator = user.Id
|
||||
hub.CreatedAt = time.Now()
|
||||
user.Hubs[user.Id] = hub
|
||||
|
||||
creator := types.NewHubUser()
|
||||
creator.OriginalId = user.Id
|
||||
@@ -211,3 +215,30 @@ func HandleHubMessage(response http.ResponseWriter, request *http.Request) {
|
||||
wsServer.WsSendMessageCloseIfTimeout(target, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func HandleGetHubs(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
user, err := getUserByToken(ctx, request.Header.Get("token"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
user.Mu.RLock()
|
||||
hubs := slices.Collect(maps.Values(user.Hubs))
|
||||
user.Mu.RUnlock()
|
||||
if len(hubs) == 0 {
|
||||
response.WriteHeader(http.StatusNoContent)
|
||||
response.Write([]byte("no hubs found"))
|
||||
}
|
||||
converted, err := json.Marshal(hubs)
|
||||
if err != nil {
|
||||
http.Error(response, "json error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
response.WriteHeader(http.StatusOK)
|
||||
response.Write(converted)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user