package main import ( "encoding/json" "net/http" "inou/lib" ) func handleStudies(w http.ResponseWriter, r *http.Request) { ctx := getAccessContextOrFail(w, r) if ctx == nil { return } dossierID := r.URL.Query().Get("dossier") if dossierID == "" { dossierID = r.URL.Query().Get("token") } studyHex := r.URL.Query().Get("study") if dossierID == "" { http.Error(w, "dossier required", http.StatusBadRequest) return } // RBAC: Check read access to dossier if !requireDossierAccess(w, ctx, dossierID) { return } // Single study lookup if studyHex != "" { studyID := studyHex study, err := lib.EntryGet(ctx, studyID) if err != nil || study.DossierID != dossierID { http.Error(w, "Study not found", http.StatusNotFound) return } // Count series (Type="series" under this study) seriesList, _ := lib.EntryChildrenByType(dossierID, studyID, "series") seriesCount := len(seriesList) var data map[string]interface{} json.Unmarshal([]byte(study.Data), &data) result := map[string]interface{}{ "id": studyHex, "patient_name": getString(data, "patient_name"), "study_date": getString(data, "study_date"), "study_time": getString(data, "study_time"), "study_desc": getString(data, "study_desc"), "institution": getString(data, "institution"), "accession": getString(data, "accession_number"), "series_count": seriesCount, } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(result) return } // List all studies directly (top-level entries with type="study") entries, err := lib.EntryRootsByType(dossierID, "study") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var studies []map[string]interface{} for _, e := range entries { // Count series for this study (Type="series") seriesList, _ := lib.EntryChildrenByType(dossierID, e.EntryID, "series") seriesCount := len(seriesList) var data map[string]interface{} json.Unmarshal([]byte(e.Data), &data) studies = append(studies, map[string]interface{}{ "id": e.EntryID, "patient_name": getString(data, "patient_name"), "study_date": getString(data, "study_date"), "study_time": getString(data, "study_time"), "study_desc": getString(data, "study_desc"), "institution": getString(data, "institution"), "accession": getString(data, "accession_number"), "series_count": seriesCount, }) } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(studies) } func getString(m map[string]interface{}, key string) string { if v, ok := m[key]; ok { if s, ok := v.(string); ok { return s } } return "" }