aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/api_room_courses.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/admin/api_room_courses.go')
-rw-r--r--internal/admin/api_room_courses.go40
1 files changed, 20 insertions, 20 deletions
diff --git a/internal/admin/api_room_courses.go b/internal/admin/api_room_courses.go
index b8f334e..bebacff 100644
--- a/internal/admin/api_room_courses.go
+++ b/internal/admin/api_room_courses.go
@@ -131,28 +131,28 @@ func (s *AdminServer) handleCourses(w http.ResponseWriter, r *http.Request) {
case http.MethodPost:
var body map[string]any
if err := readJSON(r, &body); err != nil {
- writeJSON(w, map[string]any{"error": "invalid json"})
+ failJSON(w, "invalid json")
return
}
id, _ := body["id"].(string)
id = strings.TrimSpace(id)
if id == "" {
- writeJSON(w, map[string]any{"error": "missing id"})
+ failJSON(w, "missing id")
return
}
if !validEntityID(id) {
- writeJSON(w, map[string]any{"error": "invalid id"})
+ failJSON(w, "invalid id")
return
}
delete(body, "id")
path := s.courseFilePath(id)
if _, err := os.Stat(path); err == nil {
- writeJSON(w, map[string]any{"error": "course already exists"})
+ failJSON(w, "course already exists")
return
}
newContent, err := writeMapAsYAML(path, body)
if err != nil {
- writeJSON(w, map[string]any{"error": "write error: " + err.Error()})
+ failJSON(w, "write error: "+err.Error())
return
}
s.undoStack.Push(ChangeDesc{
@@ -174,11 +174,11 @@ func (s *AdminServer) handleCourses(w http.ResponseWriter, r *http.Request) {
func (s *AdminServer) handleCourseByID(w http.ResponseWriter, r *http.Request) {
id := strings.TrimPrefix(r.URL.Path, "/api/courses/")
if id == "" {
- writeJSON(w, map[string]any{"error": "missing id"})
+ failJSON(w, "missing id")
return
}
if !validEntityID(id) {
- writeJSON(w, map[string]any{"error": "invalid id"})
+ failJSON(w, "invalid id")
return
}
path := s.courseFilePath(id)
@@ -187,7 +187,7 @@ func (s *AdminServer) handleCourseByID(w http.ResponseWriter, r *http.Request) {
case http.MethodGet:
m, data, _, err := s.loadCourseMap(id)
if err != nil {
- writeJSON(w, map[string]any{"error": "not found: " + id})
+ failJSON(w, "not found: "+id)
return
}
m["id"] = id
@@ -197,7 +197,7 @@ func (s *AdminServer) handleCourseByID(w http.ResponseWriter, r *http.Request) {
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()})
+ failJSON(w, "invalid JSON: "+err.Error())
return
}
delete(body, "id")
@@ -210,7 +210,7 @@ func (s *AdminServer) handleCourseByID(w http.ResponseWriter, r *http.Request) {
}
newContent, err := writeMapAsYAML(path, body)
if err != nil {
- writeJSON(w, map[string]any{"error": "write error: " + err.Error()})
+ failJSON(w, "write error: "+err.Error())
return
}
isCreate := oldContent == nil
@@ -236,11 +236,11 @@ func (s *AdminServer) handleCourseByID(w http.ResponseWriter, r *http.Request) {
case http.MethodDelete:
oldContent, err := snapshotFile(path)
if err != nil {
- writeJSON(w, map[string]any{"error": "not found: " + id})
+ failJSON(w, "not found: "+id)
return
}
if err := os.Remove(path); err != nil {
- writeJSON(w, map[string]any{"error": "delete error: " + err.Error()})
+ failJSON(w, "delete error: "+err.Error())
return
}
s.undoStack.Push(ChangeDesc{
@@ -269,7 +269,7 @@ func (s *AdminServer) handleRoomCourse(w http.ResponseWriter, r *http.Request, r
}
ids, err := s.listCourseIDs()
if err != nil {
- writeJSON(w, map[string]any{"error": err.Error()})
+ failJSON(w, err.Error())
return
}
for _, id := range ids {
@@ -314,16 +314,16 @@ func (s *AdminServer) handleRoomAttachCourse(w http.ResponseWriter, r *http.Requ
Obstacle map[string]any `json:"obstacle"`
}
if err := readJSON(r, &body); err != nil {
- writeJSON(w, map[string]any{"error": "invalid json"})
+ failJSON(w, "invalid json")
return
}
if strings.TrimSpace(body.CourseID) == "" {
- writeJSON(w, map[string]any{"error": "missing course_id"})
+ failJSON(w, "missing course_id")
return
}
m, _, path, err := s.loadCourseMap(body.CourseID)
if err != nil {
- writeJSON(w, map[string]any{"error": "course not found: " + body.CourseID})
+ failJSON(w, "course not found: "+body.CourseID)
return
}
oldContent, _ := snapshotFile(path)
@@ -331,7 +331,7 @@ func (s *AdminServer) handleRoomAttachCourse(w http.ResponseWriter, r *http.Requ
obstacles := asMapList(m["obstacles"])
for i, o := range obstacles {
if obstacleRoomID(o) == roomID {
- writeJSON(w, map[string]any{"error": fmt.Sprintf("room %d is already step %d of course %s", roomID, i+1, body.CourseID)})
+ failJSON(w, fmt.Sprintf("room %d is already step %d of course %s", roomID, i+1, body.CourseID))
return
}
}
@@ -379,7 +379,7 @@ func (s *AdminServer) handleRoomAttachCourse(w http.ResponseWriter, r *http.Requ
dir = "north"
}
if !isHorizontalDir(dir) {
- writeJSON(w, map[string]any{"error": "invalid direction: " + dir})
+ failJSON(w, "invalid direction: "+dir)
return
}
@@ -396,7 +396,7 @@ func (s *AdminServer) handleRoomAttachCourse(w http.ResponseWriter, r *http.Requ
newContent, err := writeMapAsYAML(path, m)
if err != nil {
- writeJSON(w, map[string]any{"error": "write error: " + err.Error()})
+ failJSON(w, "write error: "+err.Error())
return
}
s.undoStack.Push(ChangeDesc{
@@ -479,7 +479,7 @@ func (s *AdminServer) handleRoomDetachCourse(w http.ResponseWriter, r *http.Requ
}
courseID, deleted := s.detachRoomFromCourse(roomID)
if courseID == "" && !deleted {
- writeJSON(w, map[string]any{"error": "room is not part of any course"})
+ failJSON(w, "room is not part of any course")
return
}
writeJSON(w, map[string]any{"ok": true, "course_id": courseID, "deleted": deleted})