fix: read serverID from URL param instead of body in channel create

The channel Create handler was expecting server_id in the JSON body,
but the frontend sends it in the URL path (/servers/{serverID}/channels).
Now reads serverID from chi.URLParam and only requires name in the body.
This commit is contained in:
2026-06-29 13:35:50 -04:00
parent 118155d737
commit de465c6527
+10 -5
View File
@@ -30,7 +30,6 @@ func (h *Handler) RegisterRoutes(r chi.Router) {
}
type createChannelRequest struct {
ServerID string `json:"server_id"`
Name string `json:"name"`
Type string `json:"type"`
Category string `json:"category"`
@@ -84,17 +83,23 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
return
}
serverID := chi.URLParam(r, "serverID")
if serverID == "" {
http.Error(w, `{"error":"serverID is required"}`, http.StatusBadRequest)
return
}
var req createChannelRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, `{"error":"invalid request"}`, http.StatusBadRequest)
return
}
if req.ServerID == "" || req.Name == "" {
http.Error(w, `{"error":"server_id and name are required"}`, http.StatusBadRequest)
if req.Name == "" {
http.Error(w, `{"error":"name is required"}`, http.StatusBadRequest)
return
}
member, err := h.isMember(r.Context(), userID, req.ServerID)
member, err := h.isMember(r.Context(), userID, serverID)
if err != nil {
http.Error(w, `{"error":"server error"}`, http.StatusInternalServerError)
return
@@ -122,7 +127,7 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
INSERT INTO channels (server_id, name, type, category, position)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, server_id, name, type, category, position, created_at
`, req.ServerID, req.Name, channelType, category, position).Scan(
`, serverID, req.Name, channelType, category, position).Scan(
&ch.ID, &ch.ServerID, &ch.Name, &ch.Type, &ch.Category, &ch.Position, &ch.CreatedAt,
)
if err != nil {