- Add GET /api endpoint to list all markdown files - Filter to only include .md files - Return JSON response with files array - Add comprehensive tests for file listing functionality
74 lines
2.7 KiB
Markdown
74 lines
2.7 KiB
Markdown
# Implementation Summary: File Listing Feature
|
|
|
|
## Problem
|
|
The frontend was unable to list existing files because the backend API did not have an endpoint to retrieve the list of files in the data directory.
|
|
|
|
## Root Cause
|
|
The backend API handler only supported individual file operations (`GET /api/{filename}.md`, `POST /api/{filename}.md`, etc.) but had no endpoint to list all files in the data directory. The frontend was attempting to fetch `/api` to get the file list, but this route was not configured.
|
|
|
|
## Solution
|
|
Added a new endpoint `GET /api` that returns a JSON list of all markdown files in the data directory.
|
|
|
|
### Changes Made
|
|
|
|
#### 1. Backend API (`backend/internal/api/api.go`)
|
|
- Added `handleListFiles()` method that:
|
|
- Reads all files from the data directory
|
|
- Filters to only include `.md` files
|
|
- Returns JSON response with format: `{"files": ["file1.md", "file2.md", ...]}`
|
|
- Modified `handleGet()` to check if filename is empty and call `handleListFiles()` if so
|
|
|
|
#### 2. Backend Server (`backend/internal/server/server.go`)
|
|
- Added route handler for `/api` with GET method
|
|
- Kept existing route handler for `/api/{filename:.+.md}`
|
|
|
|
### API Endpoints
|
|
|
|
#### New Endpoint
|
|
- **Method:** GET
|
|
- **Path:** `/api`
|
|
- **Response:** `{"files": ["file1.md", "file2.md", ...]}`
|
|
- **Status Codes:**
|
|
- 200 OK - Success
|
|
- 500 Internal Server Error - Failed to read directory
|
|
|
|
#### Existing Endpoints (Unchanged)
|
|
- **GET** `/api/{filename}.md` - Get file content
|
|
- **POST** `/api/{filename}.md` - Create file
|
|
- **PUT** `/api/{filename}.md` - Update file
|
|
- **DELETE** `/api/{filename}.md` - Delete file
|
|
|
|
### Testing
|
|
Added comprehensive tests in `backend/tests/file_listing_test.go`:
|
|
- `TestFileListing` - Verifies multiple markdown files are listed
|
|
- `TestFileListingWithNonMarkdownFiles` - Verifies only `.md` files are returned
|
|
- `TestFileListingEmptyDirectory` - Verifies empty array for empty directory
|
|
|
|
All existing tests continue to pass.
|
|
|
|
### Frontend Compatibility
|
|
The frontend (`frontend/src/App.tsx`) already expects the correct response format:
|
|
```typescript
|
|
const loadFiles = async () => {
|
|
const response = await fetch('/api')
|
|
if (response.ok) {
|
|
const data = await response.json()
|
|
setFiles(data.files || []) // Expects { files: string[] }
|
|
}
|
|
}
|
|
```
|
|
|
|
No frontend changes were required.
|
|
|
|
## Verification
|
|
The implementation was verified by:
|
|
1. Running all existing tests - ✅ PASS
|
|
2. Running new file listing tests - ✅ PASS
|
|
3. Manual API testing with curl - ✅ WORKING
|
|
4. Frontend build verification - ✅ SUCCESS
|
|
|
|
## Files Modified
|
|
- `backend/internal/api/api.go` - Added file listing functionality
|
|
- `backend/internal/server/server.go` - Added `/api` route
|
|
- `backend/tests/file_listing_test.go` - Added comprehensive tests (NEW FILE)
|