MCPcopy Create free account
hub / github.com/GoMudEngine/GoMud / apiAdminDeleteStorageItem

Method apiAdminDeleteStorageItem

modules/storage/admin.go:59–124  ·  view source on GitHub ↗

apiAdminDeleteStorageItem handles DELETE /admin/api/v1/storage?user_id= &short_id= Removes the item with the matching short_id from the user's storage.

(r *http.Request)

Source from the content-addressed store, hash-verified

57// apiAdminDeleteStorageItem handles DELETE /admin/api/v1/storage?user_id=<id>&short_id=<sid>
58// Removes the item with the matching short_id from the user's storage.
59func (m *StorageModule) apiAdminDeleteStorageItem(r *http.Request) (int, bool, any) {
60 // Support both query-param DELETE and JSON body DELETE.
61 userIdStr := r.URL.Query().Get("user_id")
62 shortId := r.URL.Query().Get("short_id")
63
64 if userIdStr == "" || shortId == "" {
65 // Try JSON body.
66 var body struct {
67 UserId int `json:"user_id"`
68 ShortId string `json:"short_id"`
69 }
70 if err := json.NewDecoder(r.Body).Decode(&body); err == nil {
71 if userIdStr == "" {
72 userIdStr = strconv.Itoa(body.UserId)
73 }
74 if shortId == "" {
75 shortId = body.ShortId
76 }
77 }
78 }
79
80 if userIdStr == "" || shortId == "" {
81 return http.StatusBadRequest, false, map[string]string{"error": "user_id and short_id are required"}
82 }
83
84 userId, err := strconv.Atoi(userIdStr)
85 if err != nil || userId <= 0 {
86 return http.StatusBadRequest, false, map[string]string{"error": "invalid user_id"}
87 }
88
89 isOnline := users.GetByUserId(userId) != nil
90 var data StorageData
91 if isOnline {
92 data = m.storage[userId]
93 } else {
94 data = m.load(userId)
95 }
96
97 var found items.Item
98 newItems := make([]items.Item, 0, len(data.Items))
99 deleted := false
100 for _, itm := range data.Items {
101 if !deleted && itm.ShorthandId() == shortId {
102 found = itm
103 deleted = true
104 continue
105 }
106 newItems = append(newItems, itm)
107 }
108
109 if !deleted {
110 return http.StatusNotFound, false, map[string]string{"error": "item not found"}
111 }
112
113 data.Items = newItems
114 if isOnline {
115 m.storage[userId] = data
116 }

Callers

nothing calls this directly

Calls 6

loadMethod · 0.95
saveMethod · 0.95
NameMethod · 0.95
GetByUserIdFunction · 0.92
GetMethod · 0.45
ShorthandIdMethod · 0.45

Tested by

no test coverage detected