MCPcopy Index your code
hub / github.com/PatchMon/PatchMon / UpdateGroups

Method UpdateGroups

server-source-code/internal/handler/hosts.go:233–272  ·  view source on GitHub ↗

UpdateGroups handles PUT /hosts/:hostId/groups.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

231
232// UpdateGroups handles PUT /hosts/:hostId/groups.
233func (h *HostsHandler) UpdateGroups(w http.ResponseWriter, r *http.Request) {
234 hostID := chi.URLParam(r, "hostId")
235 var req struct {
236 GroupIds []string `json:"groupIds"`
237 }
238 if err := decodeJSON(r, &req); err != nil {
239 Error(w, http.StatusBadRequest, "Invalid request body")
240 return
241 }
242 groupIds := req.GroupIds
243 if groupIds == nil {
244 groupIds = []string{}
245 }
246
247 host, err := h.hosts.GetByID(r.Context(), hostID)
248 if err != nil || host == nil {
249 Error(w, http.StatusNotFound, "Host not found")
250 return
251 }
252
253 for _, gid := range groupIds {
254 _, err = h.hostGroups.GetByID(r.Context(), gid)
255 if err != nil {
256 Error(w, http.StatusBadRequest, "One or more host groups not found")
257 return
258 }
259 }
260
261 if err := h.hosts.SetHostGroups(r.Context(), hostID, groupIds); err != nil {
262 Error(w, http.StatusInternalServerError, "Failed to update host groups")
263 return
264 }
265
266 host, _ = h.hosts.GetByID(r.Context(), hostID)
267 groups, _ := h.hosts.GetHostGroups(r.Context(), hostID)
268 JSON(w, http.StatusOK, map[string]interface{}{
269 "message": "Host groups updated successfully",
270 "host": hostToResponse(host, groups),
271 })
272}
273
274// UpdateFriendlyName handles PATCH /hosts/:hostId/friendly-name.
275func (h *HostsHandler) UpdateFriendlyName(w http.ResponseWriter, r *http.Request) {

Callers

nothing calls this directly

Calls 7

decodeJSONFunction · 0.85
ErrorFunction · 0.85
hostToResponseFunction · 0.85
SetHostGroupsMethod · 0.80
GetHostGroupsMethod · 0.80
JSONFunction · 0.70
GetByIDMethod · 0.45

Tested by

no test coverage detected