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.go25
1 files changed, 18 insertions, 7 deletions
diff --git a/internal/admin/api_room_courses.go b/internal/admin/api_room_courses.go
index db74adf..099ebef 100644
--- a/internal/admin/api_room_courses.go
+++ b/internal/admin/api_room_courses.go
@@ -420,8 +420,9 @@ func (s *AdminServer) handleRoomAttachCourse(w http.ResponseWriter, r *http.Requ
// Detach a room from its course; delete the course file if it becomes empty
-// detachRoomFromCourse removes roomID from the course it belongs to, if any.
-// Returns the course ID that was modified, or "" if none match.
+// detachRoomFromCourse removes roomID from every course it belongs to, if any.
+// Returns the first affected course ID (or "" if none match) and whether any
+// course was deleted.
func (s *AdminServer) detachRoomFromCourse(roomID int) (courseID string, deleted bool) {
ids, err := s.listCourseIDs()
if err != nil {
@@ -448,15 +449,23 @@ func (s *AdminServer) detachRoomFromCourse(roomID int) (courseID string, deleted
remaining = append(remaining, obstacles[idx+1:]...)
if len(remaining) == 0 {
- os.Remove(path)
+ // A failed unlink must not be reported as a detach: the course
+ // would keep listing a deleted room.
+ if err := os.Remove(path); err != nil {
+ continue
+ }
s.undoStack.Push(ChangeDesc{
Description: fmt.Sprintf("delete course %s (last obstacle removed)", id),
FilePath: path,
OldContent: oldContent,
IsDelete: true,
})
+ if courseID == "" {
+ courseID = id
+ }
+ deleted = true
s.reloadCoursesIfWired()
- return id, true
+ continue
}
anyList := make([]any, len(remaining))
@@ -466,7 +475,7 @@ func (s *AdminServer) detachRoomFromCourse(roomID int) (courseID string, deleted
m["obstacles"] = anyList
newContent, err := writeMapAsYAML(path, m)
if err != nil {
- return "", false
+ continue
}
s.undoStack.Push(ChangeDesc{
Description: fmt.Sprintf("detach room %d from course %s", roomID, id),
@@ -474,10 +483,12 @@ func (s *AdminServer) detachRoomFromCourse(roomID int) (courseID string, deleted
OldContent: oldContent,
NewContent: newContent,
})
+ if courseID == "" {
+ courseID = id
+ }
s.reloadCoursesIfWired()
- return id, false
}
- return "", false
+ return courseID, deleted
}
func (s *AdminServer) handleRoomDetachCourse(w http.ResponseWriter, r *http.Request, roomID int) {