aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/api_rooms.go
diff options
context:
space:
mode:
authorhistoria <[not public]>2026-09-16 17:05:17 -0400
committerhistoria <[not public]>2026-09-16 17:05:17 -0400
commit1dad15698845d370e998f7cc5feb8aece3fab58f (patch)
tree8d58058782de9f422573e1f8efd5d130ce7d1ce9 /internal/admin/api_rooms.go
parenta6cf03a6ae98e4fb675da25046d148e92bcf7206 (diff)
downloadthehouseoficarus-1dad15698845d370e998f7cc5feb8aece3fab58f.tar.gz
refactor: remove dead code, consolidate save/error handling, add lint gate
Diffstat (limited to 'internal/admin/api_rooms.go')
-rw-r--r--internal/admin/api_rooms.go112
1 files changed, 54 insertions, 58 deletions
diff --git a/internal/admin/api_rooms.go b/internal/admin/api_rooms.go
index 8ee16a5..16fc7a0 100644
--- a/internal/admin/api_rooms.go
+++ b/internal/admin/api_rooms.go
@@ -21,7 +21,7 @@ func (s *AdminServer) handleRooms(w http.ResponseWriter, r *http.Request) {
case http.MethodGet:
ids, err := listRoomIDs(s.dataDir)
if err != nil {
- writeJSON(w, map[string]any{"error": err.Error()})
+ failJSON(w, err.Error())
return
}
if ids == nil {
@@ -33,13 +33,13 @@ func (s *AdminServer) handleRooms(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
r.Body.Close()
if err != nil {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("read body: %v", err)})
+ failJSON(w, fmt.Sprintf("read body: %v", err))
return
}
var m map[string]any
if err := json.Unmarshal(body, &m); err != nil {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("invalid json: %v", err)})
+ failJSON(w, fmt.Sprintf("invalid json: %v", err))
return
}
if m == nil {
@@ -93,11 +93,7 @@ func (s *AdminServer) handleRooms(w http.ResponseWriter, r *http.Request) {
if linkFrom != nil {
fromID = *linkFrom
}
- id, subdir, allocErr := s.nextRoomID(fromID)
- if allocErr != nil {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("alloc id: %v", allocErr)})
- return
- }
+ id, subdir := s.nextRoomID(fromID)
m["id"] = id
if linkFrom != nil && linkDir != nil && *linkDir != "" {
@@ -131,11 +127,11 @@ func (s *AdminServer) handleRooms(w http.ResponseWriter, r *http.Request) {
conflicts++
})
if conflicts > 0 {
- writeJSON(w, map[string]any{"error": "Add Next Step: that grid spot is already occupied — room would cause map overlap"})
+ failJSON(w, "Add Next Step: that grid spot is already occupied — room would cause map overlap")
return
}
} else {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("invalid direction: %s", *linkDir)})
+ failJSON(w, fmt.Sprintf("invalid direction: %s", *linkDir))
return
}
}
@@ -153,7 +149,7 @@ func (s *AdminServer) handleRooms(w http.ResponseWriter, r *http.Request) {
if opp, ok := world.OppositeExit[dir]; ok {
srcRoom, loadErr := s.world.LoadRoom(*linkFrom)
if loadErr != nil {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("load link_from room %d: %v", *linkFrom, loadErr)})
+ failJSON(w, fmt.Sprintf("load link_from room %d: %v", *linkFrom, loadErr))
return
}
@@ -166,7 +162,7 @@ func (s *AdminServer) handleRooms(w http.ResponseWriter, r *http.Request) {
oldContent, _ := snapshotFile(srcPath)
newContent, writeErr := writeYAMLFile(srcPath, srcRoom)
if writeErr != nil {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("write source room: %v", writeErr)})
+ failJSON(w, fmt.Sprintf("write source room: %v", writeErr))
return
}
extraFiles = append(extraFiles, ExtraFile{
@@ -185,7 +181,7 @@ func (s *AdminServer) handleRooms(w http.ResponseWriter, r *http.Request) {
path := filepath.Join(subdir, strconv.Itoa(id)+".yaml")
newContent, writeErr := writeMapAsYAML(path, m)
if writeErr != nil {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("write room: %v", writeErr)})
+ failJSON(w, fmt.Sprintf("write room: %v", writeErr))
return
}
@@ -214,7 +210,7 @@ func (s *AdminServer) handleRoomByID(w http.ResponseWriter, r *http.Request) {
}
id, err := strconv.Atoi(idStr)
if err != nil {
- writeJSON(w, map[string]any{"error": "invalid room id"})
+ failJSON(w, "invalid room id")
return
}
@@ -234,17 +230,17 @@ func (s *AdminServer) handleRoomByID(w http.ResponseWriter, r *http.Request) {
case http.MethodGet:
path, ok := s.world.GetRoomPath(id)
if !ok {
- writeJSON(w, map[string]any{"error": "room not found"})
+ failJSON(w, "room not found")
return
}
data, err := os.ReadFile(path)
if err != nil {
- writeJSON(w, map[string]any{"error": err.Error()})
+ failJSON(w, err.Error())
return
}
m, err := yamlToMap(data)
if err != nil {
- writeJSON(w, map[string]any{"error": "yaml parse error"})
+ failJSON(w, "yaml parse error")
return
}
m["id"] = id
@@ -253,13 +249,13 @@ func (s *AdminServer) handleRoomByID(w http.ResponseWriter, r *http.Request) {
case http.MethodPut:
path, ok := s.world.GetRoomPath(id)
if !ok {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("room %d not found", id)})
+ failJSON(w, fmt.Sprintf("room %d not found", id))
return
}
oldContent, snapErr := snapshotFile(path)
if snapErr != nil {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("snapshot room: %v", snapErr)})
+ failJSON(w, fmt.Sprintf("snapshot room: %v", snapErr))
return
}
@@ -267,11 +263,11 @@ func (s *AdminServer) handleRoomByID(w http.ResponseWriter, r *http.Request) {
body, readErr := io.ReadAll(r.Body)
r.Body.Close()
if readErr != nil {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("read body: %v", readErr)})
+ failJSON(w, fmt.Sprintf("read body: %v", readErr))
return
}
if err := json.Unmarshal(body, &m); err != nil {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("invalid json: %v", err)})
+ failJSON(w, fmt.Sprintf("invalid json: %v", err))
return
}
if m == nil {
@@ -285,7 +281,7 @@ func (s *AdminServer) handleRoomByID(w http.ResponseWriter, r *http.Request) {
newContent, writeErr := writeMapAsYAML(path, m)
if writeErr != nil {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("write room: %v", writeErr)})
+ failJSON(w, fmt.Sprintf("write room: %v", writeErr))
return
}
@@ -301,20 +297,20 @@ func (s *AdminServer) handleRoomByID(w http.ResponseWriter, r *http.Request) {
case http.MethodDelete:
path, ok := s.world.GetRoomPath(id)
if !ok {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("room %d not found", id)})
+ failJSON(w, fmt.Sprintf("room %d not found", id))
return
}
oldContent, backupErr := snapshotFile(path)
if backupErr != nil {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("backup room: %v", backupErr)})
+ failJSON(w, fmt.Sprintf("backup room: %v", backupErr))
return
}
courseID, _ := s.detachRoomFromCourse(id)
if err := os.Remove(path); err != nil {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("delete room: %v", err)})
+ failJSON(w, fmt.Sprintf("delete room: %v", err))
return
}
@@ -433,16 +429,16 @@ func (s *AdminServer) handleRenameRoomDir(w http.ResponseWriter, r *http.Request
NewName string `json:"new_name"`
}
if err := readJSON(r, &body); err != nil || body.NewName == "" {
- writeJSON(w, map[string]any{"error": "invalid"})
+ failJSON(w, "invalid")
return
}
if body.Dir == "" {
- writeJSON(w, map[string]any{"error": "cannot rename root"})
+ failJSON(w, "cannot rename root")
return
}
newName := strings.TrimSpace(body.NewName)
if newName == "" || strings.ContainsAny(newName, "/\\") || newName == "." || newName == ".." {
- writeJSON(w, map[string]any{"error": "invalid directory name"})
+ failJSON(w, "invalid directory name")
return
}
base := filepath.Join(s.dataDir, "rooms")
@@ -451,15 +447,15 @@ func (s *AdminServer) handleRenameRoomDir(w http.ResponseWriter, r *http.Request
newRel := filepath.Join(parent, newName)
newPath := filepath.Join(base, newRel)
if _, err := os.Stat(oldPath); os.IsNotExist(err) {
- writeJSON(w, map[string]any{"error": "directory not found"})
+ failJSON(w, "directory not found")
return
}
if _, err := os.Stat(newPath); err == nil {
- writeJSON(w, map[string]any{"error": "directory already exists: " + newRel})
+ failJSON(w, "directory already exists: "+newRel)
return
}
if err := os.Rename(oldPath, newPath); err != nil {
- writeJSON(w, map[string]any{"error": "rename failed: " + err.Error()})
+ failJSON(w, "rename failed: "+err.Error())
return
}
s.world.RebuildRoomIndex(s.dataDir)
@@ -475,21 +471,21 @@ func (s *AdminServer) handleCreateRoomDir(w http.ResponseWriter, r *http.Request
Name string `json:"name"`
}
if err := readJSON(r, &body); err != nil {
- writeJSON(w, map[string]any{"error": "invalid"})
+ failJSON(w, "invalid")
return
}
name := strings.TrimSpace(body.Name)
if name == "" || strings.ContainsAny(name, "/\\") || name == "." || name == ".." {
- writeJSON(w, map[string]any{"error": "invalid directory name"})
+ failJSON(w, "invalid directory name")
return
}
newPath := filepath.Join(s.dataDir, "rooms", name)
if _, err := os.Stat(newPath); err == nil {
- writeJSON(w, map[string]any{"error": "directory already exists"})
+ failJSON(w, "directory already exists")
return
}
if err := os.Mkdir(newPath, 0755); err != nil {
- writeJSON(w, map[string]any{"error": "create failed: " + err.Error()})
+ failJSON(w, "create failed: "+err.Error())
return
}
writeJSON(w, map[string]any{"ok": true, "name": name})
@@ -802,37 +798,37 @@ func (s *AdminServer) handleRoomMove(w http.ResponseWriter, r *http.Request) {
Dir string `json:"dir"`
}
if err := readJSON(r, &body); err != nil || body.ID <= 0 {
- writeJSON(w, map[string]any{"error": "invalid"})
+ failJSON(w, "invalid")
return
}
oldPath, ok := s.world.GetRoomPath(body.ID)
if !ok {
- writeJSON(w, map[string]any{"error": "room not found"})
+ failJSON(w, "room not found")
return
}
dir := strings.TrimSpace(body.Dir)
destDir := filepath.Join(s.dataDir, "rooms", dir)
if _, err := os.Stat(destDir); os.IsNotExist(err) {
- writeJSON(w, map[string]any{"error": "directory does not exist: " + dir})
+ failJSON(w, "directory does not exist: "+dir)
return
}
fileName := strconv.Itoa(body.ID) + ".yaml"
newPath := filepath.Join(destDir, fileName)
if _, err := os.Stat(newPath); err == nil {
- writeJSON(w, map[string]any{"error": "room already exists in target directory"})
+ failJSON(w, "room already exists in target directory")
return
}
data, err := os.ReadFile(oldPath)
if err != nil {
- writeJSON(w, map[string]any{"error": "read: " + err.Error()})
+ failJSON(w, "read: "+err.Error())
return
}
if err := os.WriteFile(newPath, data, 0644); err != nil {
- writeJSON(w, map[string]any{"error": "write: " + err.Error()})
+ failJSON(w, "write: "+err.Error())
return
}
if err := os.Remove(oldPath); err != nil {
- writeJSON(w, map[string]any{"error": "remove old: " + err.Error()})
+ failJSON(w, "remove old: "+err.Error())
return
}
s.world.RebuildRoomIndex(s.dataDir)
@@ -1137,7 +1133,7 @@ func (s *AdminServer) handleRoomSwap(w http.ResponseWriter, r *http.Request) {
func (s *AdminServer) handleDuplicateRooms(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
- writeJSON(w, map[string]any{"error": "method not allowed"})
+ failJSON(w, "method not allowed")
return
}
dups := behavior.CheckDuplicateIDs(filepath.Join(s.dataDir, "rooms"))
@@ -1154,7 +1150,7 @@ func (s *AdminServer) handleDuplicateRooms(w http.ResponseWriter, r *http.Reques
func (s *AdminServer) handleResolveDuplicate(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
- writeJSON(w, map[string]any{"error": "method not allowed"})
+ failJSON(w, "method not allowed")
return
}
var body struct {
@@ -1163,7 +1159,7 @@ func (s *AdminServer) handleResolveDuplicate(w http.ResponseWriter, r *http.Requ
NewID int `json:"new_id"`
}
if err := readJSON(r, &body); err != nil || body.Path == "" || body.Action == "" {
- writeJSON(w, map[string]any{"error": "invalid body"})
+ failJSON(w, "invalid body")
return
}
@@ -1171,7 +1167,7 @@ func (s *AdminServer) handleResolveDuplicate(w http.ResponseWriter, r *http.Requ
absRooms, _ := filepath.Abs(roomsDir)
absPath, err := filepath.Abs(body.Path)
if err != nil || !strings.HasPrefix(absPath, absRooms+string(filepath.Separator)) {
- writeJSON(w, map[string]any{"error": "invalid path"})
+ failJSON(w, "invalid path")
return
}
@@ -1179,7 +1175,7 @@ func (s *AdminServer) handleResolveDuplicate(w http.ResponseWriter, r *http.Requ
case "delete":
old, _ := snapshotFile(absPath)
if err := os.Remove(absPath); err != nil {
- writeJSON(w, map[string]any{"error": "delete failed: " + err.Error()})
+ failJSON(w, "delete failed: "+err.Error())
return
}
s.world.RebuildRoomIndex(s.dataDir)
@@ -1196,27 +1192,27 @@ func (s *AdminServer) handleResolveDuplicate(w http.ResponseWriter, r *http.Requ
case "rename":
if body.NewID <= 0 {
- writeJSON(w, map[string]any{"error": "new_id must be positive"})
+ failJSON(w, "new_id must be positive")
return
}
if _, ok := s.world.GetRoomPath(body.NewID); ok {
- writeJSON(w, map[string]any{"error": "room ID already exists"})
+ failJSON(w, "room ID already exists")
return
}
newRel := filepath.Join(filepath.Dir(absPath), strconv.Itoa(body.NewID)+".yaml")
if _, err := os.Stat(newRel); err == nil {
- writeJSON(w, map[string]any{"error": "target file already exists"})
+ failJSON(w, "target file already exists")
return
}
oldContent, err := os.ReadFile(absPath)
if err != nil {
- writeJSON(w, map[string]any{"error": "read failed: " + err.Error()})
+ failJSON(w, "read failed: "+err.Error())
return
}
m, err := yamlToMap(oldContent)
if err != nil {
- writeJSON(w, map[string]any{"error": "yaml parse failed: " + err.Error()})
+ failJSON(w, "yaml parse failed: "+err.Error())
return
}
// A duplicate file was never in the room index (only one of the pair
@@ -1228,11 +1224,11 @@ func (s *AdminServer) handleResolveDuplicate(w http.ResponseWriter, r *http.Requ
newContent, err := writeMapAsYAML(newRel, m)
if err != nil {
- writeJSON(w, map[string]any{"error": "write failed: " + err.Error()})
+ failJSON(w, "write failed: "+err.Error())
return
}
if err := os.Remove(absPath); err != nil {
- writeJSON(w, map[string]any{"error": "remove old failed: " + err.Error()})
+ failJSON(w, "remove old failed: "+err.Error())
return
}
s.world.RebuildRoomIndex(s.dataDir)
@@ -1246,7 +1242,7 @@ func (s *AdminServer) handleResolveDuplicate(w http.ResponseWriter, r *http.Requ
writeJSON(w, map[string]any{"ok": true})
default:
- writeJSON(w, map[string]any{"error": "unknown action: " + body.Action})
+ failJSON(w, "unknown action: "+body.Action)
}
}
@@ -1259,7 +1255,7 @@ func (s *AdminServer) handleCreateEmptyRoom(w http.ResponseWriter, r *http.Reque
Dir string `json:"dir"`
}
if err := readJSON(r, &body); err != nil {
- writeJSON(w, map[string]any{"error": "invalid"})
+ failJSON(w, "invalid")
return
}
base := filepath.Join(s.dataDir, "rooms")
@@ -1267,7 +1263,7 @@ func (s *AdminServer) handleCreateEmptyRoom(w http.ResponseWriter, r *http.Reque
if body.Dir != "" {
targetDir = filepath.Join(base, body.Dir)
if _, err := os.Stat(targetDir); os.IsNotExist(err) {
- writeJSON(w, map[string]any{"error": "directory not found"})
+ failJSON(w, "directory not found")
return
}
}
@@ -1298,7 +1294,7 @@ func (s *AdminServer) handleCreateEmptyRoom(w http.ResponseWriter, r *http.Reque
}
newContent, writeErr := writeMapAsYAML(path, m)
if writeErr != nil {
- writeJSON(w, map[string]any{"error": "write room: " + writeErr.Error()})
+ failJSON(w, "write room: "+writeErr.Error())
return
}
s.world.RebuildRoomIndex(s.dataDir)