diff --git a/ingestion/internal/api/handler.go b/ingestion/internal/api/handler.go index e3619da..ecabac1 100644 --- a/ingestion/internal/api/handler.go +++ b/ingestion/internal/api/handler.go @@ -33,6 +33,8 @@ type queryRequest struct { type writeRequest struct { Content string `json:"content"` Filename string `json:"filename,omitempty"` + Type string `json:"type,omitempty"` + Domain string `json:"domain,omitempty"` } // Query handles POST /query — full-text search across the brain wiki. @@ -83,8 +85,22 @@ func (h *Handler) Write(w http.ResponseWriter, r *http.Request) { return } + finalContent := req.Content + if req.Type != "" || req.Domain != "" { + var fm strings.Builder + fm.WriteString("---\n") + if req.Type != "" { + fmt.Fprintf(&fm, "type: %s\n", req.Type) + } + if req.Domain != "" { + fmt.Fprintf(&fm, "domain: %s\n", req.Domain) + } + fm.WriteString("---\n") + finalContent = fm.String() + req.Content + } + dest := filepath.Join(rawDir, filepath.Base(filename)) - if err := os.WriteFile(dest, []byte(req.Content), 0o644); err != nil { + if err := os.WriteFile(dest, []byte(finalContent), 0o644); err != nil { h.logger.Error("write failed", "err", err) http.Error(w, "write error", http.StatusInternalServerError) return diff --git a/ingestion/internal/api/handler_test.go b/ingestion/internal/api/handler_test.go index 084cae6..d50a392 100644 --- a/ingestion/internal/api/handler_test.go +++ b/ingestion/internal/api/handler_test.go @@ -79,6 +79,27 @@ func TestQuery_RequiresQuery(t *testing.T) { assert.Equal(t, http.StatusBadRequest, rec.Code) } +func TestWrite_IncludesFrontmatterWhenTypeProvided(t *testing.T) { + dir, h := setup(t) + body, _ := json.Marshal(map[string]any{ + "content": "Some learning.", + "filename": "typed-note.md", + "type": "concept", + "domain": "software", + }) + req := httptest.NewRequest(http.MethodPost, "/write", bytes.NewReader(body)) + rec := httptest.NewRecorder() + + h.Write(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + content, err := os.ReadFile(filepath.Join(dir, "raw", "typed-note.md")) + require.NoError(t, err) + assert.Contains(t, string(content), "type: concept") + assert.Contains(t, string(content), "domain: software") + assert.Contains(t, string(content), "Some learning.") +} + func TestWrite_GeneratesFilenameIfAbsent(t *testing.T) { dir, h := setup(t) body, _ := json.Marshal(map[string]any{"content": "auto name"})