46 lines
1006 B
Go
46 lines
1006 B
Go
package httpRequest
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"go-socket/packages/cache"
|
|
"go-socket/packages/postgresql"
|
|
"go-socket/packages/types"
|
|
)
|
|
|
|
func HandleHubCreate(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.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
hubName := request.Header.Get("hubname")
|
|
if hubName == "" {
|
|
http.Error(response, "hub name is required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
hub := types.NewHub()
|
|
hub.Name = hubName
|
|
hub.Creator = user.Id
|
|
hub.Color = types.Rgba{}.GetRandom()
|
|
hub.CreatedAt = time.Now()
|
|
|
|
err = postgresql.HubSave(ctx, hub)
|
|
if err != nil {
|
|
http.Error(response, "failed to save hub", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
cache.SaveHub(hub)
|
|
|
|
response.WriteHeader(http.StatusCreated)
|
|
response.Write([]byte(hub.Id.String()))
|
|
}
|