sync: apply multi-chart-management delta specs to main specs

- Created new chart-management capability spec
- Updated data-ingestion: chart-scoped candles, duplicate filename handling
- Updated backend-api: all endpoints gain chartId parameter, chart CRUD
- Updated chart-canvas: chart switching, scoped data fetching
- Updated label-management: annotations scoped to active chart
- Updated ui-shell: upload creates/selects chart, theme-aware link styling
This commit is contained in:
Marko Djordjevic 2026-02-13 09:06:37 +01:00
parent 7cb308788e
commit 4121a87875
6 changed files with 201 additions and 65 deletions

View file

@ -1,46 +1,58 @@
## ADDED Requirements
### Requirement: Upload endpoint
The system SHALL provide a `POST /api/upload` endpoint that accepts a CSV file via multipart form data. The endpoint SHALL parse the CSV using papaparse, validate the format, and insert all candle records into the `candles` table within a single database transaction. On success, the endpoint SHALL return a JSON response with the count of inserted records. On failure, it SHALL return an appropriate error status and message.
The system SHALL provide a `POST /api/upload` endpoint that accepts a CSV file via multipart form data. The endpoint SHALL create a new chart (named from the uploaded filename without extension), parse the CSV using papaparse, validate the format, and insert all candle records into the `candles` table with the new chart's `chart_id` within a single database transaction. On success, the endpoint SHALL return a JSON response with the chart `id`, chart `name`, and the count of inserted records. On failure, it SHALL return an appropriate error status and message without creating a chart.
#### Scenario: Successful upload
- **WHEN** a valid CSV file is sent to POST /api/upload
- **THEN** endpoint returns `{ "success": true, "count": <number> }` with HTTP 200
- **WHEN** a valid CSV file named "BTC-1H.csv" is sent to POST /api/upload
- **THEN** endpoint creates a chart named "BTC-1H", inserts candles with that chart_id, and returns `{ "success": true, "count": <number>, "chart": { "id": <number>, "name": "BTC-1H" } }` with HTTP 200
#### Scenario: Invalid CSV upload
- **WHEN** a CSV with missing or invalid headers is sent to POST /api/upload
- **THEN** endpoint returns `{ "error": "<description>" }` with HTTP 400
- **THEN** endpoint returns `{ "error": "<description>" }` with HTTP 400 and does not create a chart
#### Scenario: No file provided
- **WHEN** POST /api/upload is called without a file
- **THEN** endpoint returns `{ "error": "No file provided" }` with HTTP 400
#### Scenario: Duplicate filename
- **WHEN** a CSV named "BTC-1H.csv" is uploaded and a chart named "BTC-1H" already exists
- **THEN** endpoint creates a chart named "BTC-1H-2" (or next available suffix) and inserts candles under that chart
### Requirement: Get annotations endpoint
The system SHALL provide a `GET /api/annotations` endpoint that returns all annotations from the database as a JSON array. Each annotation object SHALL include: `id`, `timestamp`, `label_type`, `geometry` (parsed from JSON string or null), and `created_at`.
The system SHALL provide a `GET /api/annotations` endpoint that accepts an optional `chartId` query parameter. When `chartId` is provided, the endpoint SHALL return only annotations belonging to that chart. When `chartId` is omitted, the endpoint SHALL return annotations for the most recently created chart. Each annotation object SHALL include: `id`, `chart_id`, `timestamp`, `label_type`, `geometry` (parsed from JSON string or null), and `created_at`.
#### Scenario: Fetch all annotations
- **WHEN** GET /api/annotations is called
- **THEN** endpoint returns a JSON array of all annotation objects with HTTP 200
#### Scenario: Fetch annotations for specific chart
- **WHEN** GET /api/annotations?chartId=3 is called
- **THEN** endpoint returns a JSON array of annotations where chart_id equals 3, with HTTP 200
#### Scenario: No annotations exist
- **WHEN** GET /api/annotations is called and no annotations are in the database
#### Scenario: Fetch annotations without chartId
- **WHEN** GET /api/annotations is called without a chartId parameter
- **THEN** endpoint returns annotations for the most recently created chart with HTTP 200
#### Scenario: No annotations exist for chart
- **WHEN** GET /api/annotations?chartId=3 is called and no annotations exist for chart 3
- **THEN** endpoint returns an empty JSON array `[]` with HTTP 200
### Requirement: Create annotation endpoint
The system SHALL provide a `POST /api/annotations` endpoint that accepts a JSON body with fields: `timestamp` (required, integer), `label_type` (required, string), and `geometry` (optional, object). The endpoint SHALL validate the input, serialize geometry to JSON string if present, and insert the record into the `annotations` table. On success, it SHALL return the created annotation object with its assigned `id`.
The system SHALL provide a `POST /api/annotations` endpoint that accepts a JSON body with fields: `timestamp` (required, integer), `label_type` (required, string), `chart_id` (required, integer), and `geometry` (optional, object). The endpoint SHALL validate the input, verify the chart exists, serialize geometry to JSON string if present, and insert the record into the `annotations` table. On success, it SHALL return the created annotation object with its assigned `id`.
#### Scenario: Create a marker annotation
- **WHEN** POST /api/annotations is called with `{ "timestamp": 1700000000, "label_type": "break_up" }`
- **THEN** endpoint saves the annotation and returns the created object with `id` and HTTP 201
- **WHEN** POST /api/annotations is called with `{ "timestamp": 1700000000, "label_type": "break_up", "chart_id": 3 }`
- **THEN** endpoint saves the annotation with chart_id 3 and returns the created object with `id` and HTTP 201
#### Scenario: Create a line annotation
- **WHEN** POST /api/annotations is called with `{ "timestamp": 1700000000, "label_type": "line", "geometry": { "startTime": 1700000000, "startPrice": 1.05, "endTime": 1700100000, "endPrice": 1.06 } }`
- **THEN** endpoint saves the annotation with serialized geometry JSON and returns the created object with HTTP 201
- **WHEN** POST /api/annotations is called with `{ "timestamp": 1700000000, "label_type": "line", "chart_id": 3, "geometry": { "startTime": 1700000000, "startPrice": 1.05, "endTime": 1700100000, "endPrice": 1.06 } }`
- **THEN** endpoint saves the annotation with chart_id 3 and serialized geometry JSON, returns the created object with HTTP 201
#### Scenario: Invalid annotation data
- **WHEN** POST /api/annotations is called with missing required fields
- **WHEN** POST /api/annotations is called with missing required fields (timestamp, label_type, or chart_id)
- **THEN** endpoint returns `{ "error": "<description>" }` with HTTP 400
#### Scenario: Annotation for non-existent chart
- **WHEN** POST /api/annotations is called with a chart_id that does not exist
- **THEN** endpoint returns `{ "error": "Chart not found" }` with HTTP 404
### Requirement: Delete annotation endpoint
The system SHALL provide a `DELETE /api/annotations/[id]` endpoint that removes an annotation by its ID. On success, it SHALL return HTTP 200. If the annotation does not exist, it SHALL return HTTP 404.
@ -53,23 +65,27 @@ The system SHALL provide a `DELETE /api/annotations/[id]` endpoint that removes
- **THEN** endpoint returns HTTP 404
### Requirement: Export annotations endpoint
The system SHALL provide a `GET /api/export` endpoint that returns all annotations as a downloadable CSV file. The CSV SHALL have columns: `timestamp`, `label_type`, `price` (extracted from geometry for lines, or the candle's close price for markers). The response SHALL set `Content-Type: text/csv` and `Content-Disposition: attachment; filename="annotations.csv"` headers.
The system SHALL provide a `GET /api/export` endpoint that accepts an optional `chartId` query parameter. When `chartId` is provided, the endpoint SHALL export only annotations for that chart. When `chartId` is omitted, the endpoint SHALL export annotations for the most recently created chart. The CSV SHALL have columns: `timestamp`, `label_type`, `price`. The response SHALL set `Content-Type: text/csv` and `Content-Disposition: attachment; filename="annotations.csv"` headers.
#### Scenario: Export as CSV
- **WHEN** GET /api/export is called and annotations exist
- **THEN** endpoint returns a CSV file download with all annotations
#### Scenario: Export for specific chart
- **WHEN** GET /api/export?chartId=3 is called and annotations exist for chart 3
- **THEN** endpoint returns a CSV file download with only annotations belonging to chart 3
#### Scenario: Export with no data
- **WHEN** GET /api/export is called and no annotations exist
- **THEN** endpoint returns a CSV file with only the header row
#### Scenario: Export without chartId
- **WHEN** GET /api/export is called without a chartId parameter
- **THEN** endpoint exports annotations for the most recently created chart
### Requirement: Get candles endpoint
The system SHALL provide a `GET /api/candles` endpoint that returns all candle records from the database as a JSON array, ordered by time ascending. Each object SHALL include: `time`, `open`, `high`, `low`, `close`.
The system SHALL provide a `GET /api/candles` endpoint that accepts an optional `chartId` query parameter. When `chartId` is provided, the endpoint SHALL return only candles belonging to that chart. When `chartId` is omitted, the endpoint SHALL return candles for the most recently created chart. Results SHALL be ordered by time ascending. Each object SHALL include: `time`, `open`, `high`, `low`, `close`.
#### Scenario: Fetch all candles
- **WHEN** GET /api/candles is called
- **THEN** endpoint returns a JSON array of candle objects ordered by time ascending with HTTP 200
#### Scenario: Fetch candles for specific chart
- **WHEN** GET /api/candles?chartId=3 is called
- **THEN** endpoint returns a JSON array of candle objects where chart_id equals 3, ordered by time ascending with HTTP 200
#### Scenario: No candles exist
- **WHEN** GET /api/candles is called and no candle data is in the database
#### Scenario: Fetch candles without chartId
- **WHEN** GET /api/candles is called without a chartId parameter
- **THEN** endpoint returns candles for the most recently created chart with HTTP 200
#### Scenario: No candles exist for chart
- **WHEN** GET /api/candles?chartId=3 is called and no candles exist for chart 3
- **THEN** endpoint returns an empty JSON array `[]` with HTTP 200