aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/api_techs.go
blob: 293b1dbdf79a12a22304b851d95685cfecfd54c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package admin

import (
	"net/http"
	"os"
	"path/filepath"
	"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:
		tree, err := listYAMLTree(s.dataDir, "techs")
		if err != nil {
			writeJSON(w, map[string]any{"error": 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 {
			writeJSON(w, map[string]any{"error": "invalid json"})
			return
		}
		id, ok := m["id"].(string)
		if !ok || strings.TrimSpace(id) == "" {
			writeJSON(w, map[string]any{"error": "missing id"})
			return
		}
		delete(m, "id")
		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()})
			return
		}
		s.undoStack.Push(ChangeDesc{
			Description: "Created tech " + 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) handleTechByID(w http.ResponseWriter, r *http.Request) {
	id := strings.TrimPrefix(r.URL.Path, "/api/techs/")
	if id == "" {
		http.Error(w, `{"error":"missing id"}`, http.StatusBadRequest)
		return
	}

	switch r.Method {
	case http.MethodGet:
		data, path, err := s.findTechFile(id)
		if err != nil {
			writeJSON(w, map[string]any{"error": "not found: " + id})
			return
		}
		raw, err := yamlToMap(data)
		if err != nil {
			writeJSON(w, map[string]any{"error": "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 {
			writeJSON(w, map[string]any{"error": "invalid JSON: " + err.Error()})
			return
		}
		delete(body, "id")
		delete(body, "_path")
		delete(body, "_raw")

		_, path, findErr := s.findTechFile(id)
		isCreate := findErr != nil
		if isCreate {
			path = filepath.Join(s.dataDir, "techs", id+".yaml")
		}

		var oldContent []byte
		if !isCreate {
			if existing, err := snapshotFile(path); err == nil {
				oldContent = existing
			}
		}

		newContent, err := writeMapAsYAML(path, body)
		if err != nil {
			writeJSON(w, map[string]any{"error": "write error: " + err.Error()})
			return
		}

		desc := "Updated tech " + id
		if isCreate {
			desc = "Created tech " + 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 := s.findTechFile(id)
		if err != nil {
			writeJSON(w, map[string]any{"error": "not found: " + id})
			return
		}
		oldContent, _ := snapshotFile(path)
		if err := os.Remove(path); err != nil {
			writeJSON(w, map[string]any{"error": "delete error: " + err.Error()})
			return
		}
		s.undoStack.Push(ChangeDesc{
			Description: "Deleted tech " + 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)
	}
}