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:
@@ -30,7 +30,6 @@ func (h *Handler) RegisterRoutes(r chi.Router) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type createChannelRequest struct {
|
type createChannelRequest struct {
|
||||||
ServerID string `json:"server_id"`
|
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Category string `json:"category"`
|
Category string `json:"category"`
|
||||||
@@ -84,17 +83,23 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
serverID := chi.URLParam(r, "serverID")
|
||||||
|
if serverID == "" {
|
||||||
|
http.Error(w, `{"error":"serverID is required"}`, http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var req createChannelRequest
|
var req createChannelRequest
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
http.Error(w, `{"error":"invalid request"}`, http.StatusBadRequest)
|
http.Error(w, `{"error":"invalid request"}`, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.ServerID == "" || req.Name == "" {
|
if req.Name == "" {
|
||||||
http.Error(w, `{"error":"server_id and name are required"}`, http.StatusBadRequest)
|
http.Error(w, `{"error":"name is required"}`, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
member, err := h.isMember(r.Context(), userID, req.ServerID)
|
member, err := h.isMember(r.Context(), userID, serverID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, `{"error":"server error"}`, http.StatusInternalServerError)
|
http.Error(w, `{"error":"server error"}`, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -122,7 +127,7 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
|
|||||||
INSERT INTO channels (server_id, name, type, category, position)
|
INSERT INTO channels (server_id, name, type, category, position)
|
||||||
VALUES ($1, $2, $3, $4, $5)
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
RETURNING id, server_id, name, type, category, position, created_at
|
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,
|
&ch.ID, &ch.ServerID, &ch.Name, &ch.Type, &ch.Category, &ch.Position, &ch.CreatedAt,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user