aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/api_modules.go
diff options
context:
space:
mode:
authorhistoria <[not public]>2026-09-18 21:44:49 -0400
committerhistoria <[not public]>2026-09-18 21:44:49 -0400
commite7848ea79a9a57fdf391d30c48ce649f3d2f6180 (patch)
treea2972facd5bbe88006f40ff96a1eeadd570ac15b /internal/admin/api_modules.go
parenta8a67482bbff8297a96adff65d2fd7029ed998cc (diff)
downloadthehouseoficarus-e7848ea79a9a57fdf391d30c48ce649f3d2f6180.tar.gz
refactor: dedup admin entity handlers and server construction
Diffstat (limited to 'internal/admin/api_modules.go')
-rw-r--r--internal/admin/api_modules.go147
1 files changed, 0 insertions, 147 deletions
diff --git a/internal/admin/api_modules.go b/internal/admin/api_modules.go
deleted file mode 100644
index ea547c5..0000000
--- a/internal/admin/api_modules.go
+++ /dev/null
@@ -1,147 +0,0 @@
-package admin
-
-import (
- "net/http"
- "os"
- "path/filepath"
- "strings"
-)
-
-func (s *AdminServer) handleModules(w http.ResponseWriter, r *http.Request) {
- switch r.Method {
- case http.MethodGet:
- tree, err := listYAMLTree(s.dataDir, "modules")
- if err != nil {
- failJSON(w, err.Error())
- return
- }
- if tree == nil {
- tree = &YAMLTree{Root: []string{}, Dirs: map[string][]string{}}
- }
- writeJSON(w, tree)
- case http.MethodPost:
- var m map[string]any
- if err := readJSON(r, &m); err != nil {
- failJSON(w, "invalid json")
- return
- }
- id, ok := m["id"].(string)
- if !ok || strings.TrimSpace(id) == "" {
- failJSON(w, "missing id")
- return
- }
- if !validEntityID(id) {
- failJSON(w, "invalid id")
- return
- }
- delete(m, "id")
- path := filepath.Join(s.dataDir, "modules", id+".yaml")
- if _, err := os.Stat(path); err == nil {
- failJSON(w, "module already exists")
- return
- }
- newContent, err := writeMapAsYAML(path, m)
- if err != nil {
- failJSON(w, "write error: "+err.Error())
- return
- }
- s.undoStack.Push(ChangeDesc{
- Description: "Created module " + id,
- FilePath: path,
- NewContent: newContent,
- IsCreate: true,
- })
- writeJSON(w, map[string]any{"ok": true, "id": id})
- default:
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
- }
-}
-
-func (s *AdminServer) handleModuleByID(w http.ResponseWriter, r *http.Request) {
- id := strings.TrimPrefix(r.URL.Path, "/api/modules/")
- if id == "" {
- http.Error(w, `{"error":"missing id"}`, http.StatusBadRequest)
- return
- }
- if !validEntityID(id) {
- http.Error(w, `{"error":"invalid id"}`, http.StatusBadRequest)
- return
- }
-
- switch r.Method {
- case http.MethodGet:
- data, path, err := findYAMLFileInSubdirs(s.dataDir, "modules", id)
- if err != nil {
- failJSON(w, "not found: "+id)
- return
- }
- raw, err := yamlToMap(data)
- if err != nil {
- failJSON(w, "parse error: "+err.Error())
- return
- }
- raw["id"] = id
- raw["_path"] = path
- raw["_raw"] = string(data)
- writeJSON(w, raw)
-
- case http.MethodPost, http.MethodPut:
- var body map[string]any
- if err := readJSON(r, &body); err != nil {
- failJSON(w, "invalid JSON: "+err.Error())
- return
- }
- delete(body, "id")
- delete(body, "_path")
- delete(body, "_raw")
-
- path := filepath.Join(s.dataDir, "modules", id+".yaml")
- oldContent, existed, err := snapshotBefore(path)
- if err != nil {
- failJSON(w, "read existing file: "+err.Error())
- return
- }
-
- newContent, err := writeMapAsYAML(path, body)
- if err != nil {
- failJSON(w, "write error: "+err.Error())
- return
- }
-
- isCreate := !existed
- desc := "Updated module " + id
- if isCreate {
- desc = "Created module " + id
- }
- s.undoStack.Push(ChangeDesc{
- Description: desc,
- FilePath: path,
- OldContent: oldContent,
- NewContent: newContent,
- IsCreate: isCreate,
- })
- writeJSON(w, map[string]any{"ok": true, "id": id})
-
- case http.MethodDelete:
- _, path, err := findYAMLFileInSubdirs(s.dataDir, "modules", id)
- if err != nil {
- failJSON(w, "not found: "+id)
- return
- }
- oldContent, _ := snapshotFile(path)
- if err := os.Remove(path); err != nil {
- failJSON(w, "delete error: "+err.Error())
- return
- }
- s.undoStack.Push(ChangeDesc{
- Description: "Deleted module " + id,
- FilePath: path,
- OldContent: oldContent,
- IsDelete: true,
- })
- writeJSON(w, map[string]any{"ok": true, "id": id})
-
- default:
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
- }
-}