diff options
| author | historia <[not public]> | 2026-07-14 03:05:42 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-07-14 03:05:42 -0400 |
| commit | 430c81bd2056769e1326df9d1dff983afe7f33df (patch) | |
| tree | 83b7a7695c60519023b7634a3f552f66b34f7dbf /internal/admin/api_techs.go | |
| parent | d72957499f1e25741b8f7a91746fbe132fa68161 (diff) | |
| download | thehouseoficarus-430c81bd2056769e1326df9d1dff983afe7f33df.tar.gz | |
fix(admin): tech tab loading yaml and css
Diffstat (limited to 'internal/admin/api_techs.go')
| -rw-r--r-- | internal/admin/api_techs.go | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/internal/admin/api_techs.go b/internal/admin/api_techs.go index 0d3e201..293b1db 100644 --- a/internal/admin/api_techs.go +++ b/internal/admin/api_techs.go @@ -7,6 +7,14 @@ import ( "strings" ) +func (s *AdminServer) findTechFile(id string) ([]byte, string, error) { + data, path, err := readYAMLFile(s.dataDir, "techs", id) + if err == nil { + return data, path, nil + } + return findYAMLFileInSubdirs(s.dataDir, "techs", id) +} + func (s *AdminServer) handleTechs(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: @@ -31,11 +39,11 @@ func (s *AdminServer) handleTechs(w http.ResponseWriter, r *http.Request) { return } delete(m, "id") - path := filepath.Join(s.dataDir, "techs", id+".yaml") - if _, err := os.Stat(path); err == nil { + if _, _, err := s.findTechFile(id); err == nil { writeJSON(w, map[string]any{"error": "tech already exists"}) return } + path := filepath.Join(s.dataDir, "techs", id+".yaml") newContent, err := writeMapAsYAML(path, m) if err != nil { writeJSON(w, map[string]any{"error": "write error: " + err.Error()}) @@ -62,7 +70,7 @@ func (s *AdminServer) handleTechByID(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: - data, path, err := findYAMLFileInSubdirs(s.dataDir, "techs", id) + data, path, err := s.findTechFile(id) if err != nil { writeJSON(w, map[string]any{"error": "not found: " + id}) return @@ -87,10 +95,17 @@ func (s *AdminServer) handleTechByID(w http.ResponseWriter, r *http.Request) { delete(body, "_path") delete(body, "_raw") - path := filepath.Join(s.dataDir, "techs", id+".yaml") + _, path, findErr := s.findTechFile(id) + isCreate := findErr != nil + if isCreate { + path = filepath.Join(s.dataDir, "techs", id+".yaml") + } + var oldContent []byte - if existing, err := snapshotFile(path); err == nil { - oldContent = existing + if !isCreate { + if existing, err := snapshotFile(path); err == nil { + oldContent = existing + } } newContent, err := writeMapAsYAML(path, body) @@ -99,7 +114,6 @@ func (s *AdminServer) handleTechByID(w http.ResponseWriter, r *http.Request) { return } - isCreate := oldContent == nil desc := "Updated tech " + id if isCreate { desc = "Created tech " + id @@ -114,7 +128,7 @@ func (s *AdminServer) handleTechByID(w http.ResponseWriter, r *http.Request) { writeJSON(w, map[string]any{"ok": true, "id": id}) case http.MethodDelete: - _, path, err := findYAMLFileInSubdirs(s.dataDir, "techs", id) + _, path, err := s.findTechFile(id) if err != nil { writeJSON(w, map[string]any{"error": "not found: " + id}) return |
