diff options
Diffstat (limited to 'internal/admin/api_items.go')
| -rw-r--r-- | internal/admin/api_items.go | 186 |
1 files changed, 0 insertions, 186 deletions
diff --git a/internal/admin/api_items.go b/internal/admin/api_items.go deleted file mode 100644 index 8798543..0000000 --- a/internal/admin/api_items.go +++ /dev/null @@ -1,186 +0,0 @@ -package admin - -import ( - "fmt" - "net/http" - "os" - "path/filepath" - "strings" -) - -func (s *AdminServer) handleItems(w http.ResponseWriter, r *http.Request) { - switch r.Method { - case http.MethodGet: - tree, err := listYAMLTree(s.dataDir, "items") - if err != nil { - http.Error(w, `{"error":"failed to list items"}`, http.StatusInternalServerError) - return - } - if tree == nil { - tree = &YAMLTree{Root: []string{}, Dirs: map[string][]string{}} - } - writeJSON(w, tree) - case http.MethodPost: - s.createItem(w, r) - default: - http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed) - } -} - -func (s *AdminServer) handleItemByID(w http.ResponseWriter, r *http.Request) { - id := strings.TrimPrefix(r.URL.Path, "/api/items/") - if id == "" { - http.Error(w, `{"error":"missing item id"}`, http.StatusBadRequest) - return - } - if !validEntityID(id) { - http.Error(w, `{"error":"invalid item id"}`, http.StatusBadRequest) - return - } - - switch r.Method { - case http.MethodGet: - s.getItem(w, id) - case http.MethodPut: - s.updateItem(w, r, id) - case http.MethodDelete: - s.deleteItem(w, id) - default: - http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed) - } -} - -// invalidateItemCache drops the in-game item cache after an editor write so -// gameplay picks up the change without a manual reload, and rebuilds the -// craft index. Also refreshes the path index in case the file was created -// or removed. -func (s *AdminServer) invalidateItemCache() { - s.invalidateEntityCaches("items") -} - -func (s *AdminServer) findItemFile(id string) ([]byte, string, error) { - data, path, err := readYAMLFile(s.dataDir, "items", id) - if err == nil { - return data, path, nil - } - return findYAMLFileInSubdirs(s.dataDir, "items", id) -} - -func (s *AdminServer) getItem(w http.ResponseWriter, id string) { - data, path, err := s.findItemFile(id) - if err != nil { - http.Error(w, `{"error":"item not found"}`, http.StatusNotFound) - return - } - m, err := yamlToMap(data) - if err != nil { - http.Error(w, `{"error":"failed to parse item yaml"}`, http.StatusInternalServerError) - return - } - m["_path"] = path - m["_raw"] = string(data) - writeJSON(w, m) -} - -func (s *AdminServer) updateItem(w http.ResponseWriter, r *http.Request, id string) { - _, path, err := s.findItemFile(id) - if err != nil { - http.Error(w, `{"error":"item not found"}`, http.StatusNotFound) - return - } - - var m map[string]any - if err := readJSON(r, &m); err != nil { - http.Error(w, `{"error":"invalid json body"}`, http.StatusBadRequest) - return - } - m["id"] = id - - oldContent, err := snapshotFile(path) - if err != nil { - http.Error(w, `{"error":"failed to read existing item"}`, http.StatusInternalServerError) - return - } - - newContent, err := writeMapAsYAML(path, m) - if err != nil { - http.Error(w, `{"error":"failed to write item"}`, http.StatusInternalServerError) - return - } - m["_raw"] = string(newContent) - - s.undoStack.Push(ChangeDesc{ - Description: fmt.Sprintf("Update item %s", id), - FilePath: path, - OldContent: oldContent, - NewContent: newContent, - }) - s.invalidateItemCache() - - writeJSON(w, m) -} - -func (s *AdminServer) deleteItem(w http.ResponseWriter, id string) { - _, path, err := s.findItemFile(id) - if err != nil { - http.Error(w, `{"error":"item not found"}`, http.StatusNotFound) - return - } - - oldContent := backupFile(path) - - if err := os.Remove(path); err != nil { - http.Error(w, `{"error":"failed to delete item"}`, http.StatusInternalServerError) - return - } - - s.undoStack.Push(ChangeDesc{ - Description: fmt.Sprintf("Delete item %s", id), - FilePath: path, - OldContent: oldContent, - IsDelete: true, - }) - s.invalidateItemCache() - - writeJSON(w, map[string]any{"deleted": id}) -} - -func (s *AdminServer) createItem(w http.ResponseWriter, r *http.Request) { - var m map[string]any - if err := readJSON(r, &m); err != nil { - http.Error(w, `{"error":"invalid json body"}`, http.StatusBadRequest) - return - } - id, ok := m["id"].(string) - if !ok || strings.TrimSpace(id) == "" { - http.Error(w, `{"error":"missing item id"}`, http.StatusBadRequest) - return - } - if !validEntityID(id) { - http.Error(w, `{"error":"invalid item id"}`, http.StatusBadRequest) - return - } - - if _, _, err := s.findItemFile(id); err == nil { - http.Error(w, `{"error":"item already exists"}`, http.StatusConflict) - return - } - - path := filepath.Join(s.dataDir, "items", id+".yaml") - - newContent, err := writeMapAsYAML(path, m) - if err != nil { - http.Error(w, `{"error":"failed to write item"}`, http.StatusInternalServerError) - return - } - - s.undoStack.Push(ChangeDesc{ - Description: fmt.Sprintf("Create item %s", id), - FilePath: path, - NewContent: newContent, - IsCreate: true, - }) - s.invalidateItemCache() - - writeJSON(w, m) -} |
