feat: add charts API endpoints (GET list, DELETE with cascade)
This commit is contained in:
parent
92d3339a48
commit
b53cb1b7d1
3 changed files with 39 additions and 2 deletions
|
|
@ -9,8 +9,8 @@
|
||||||
|
|
||||||
## 2. Charts API Endpoints
|
## 2. Charts API Endpoints
|
||||||
|
|
||||||
- [ ] 2.1 Create `GET /api/charts` endpoint — returns all charts ordered by `created_at` desc
|
- [x] 2.1 Create `GET /api/charts` endpoint — returns all charts ordered by `created_at` desc
|
||||||
- [ ] 2.2 Create `DELETE /api/charts/[id]` endpoint — deletes chart + cascading candles and annotations in a transaction
|
- [x] 2.2 Create `DELETE /api/charts/[id]` endpoint — deletes chart + cascading candles and annotations in a transaction
|
||||||
|
|
||||||
## 3. Upload Endpoint Changes
|
## 3. Upload Endpoint Changes
|
||||||
|
|
||||||
|
|
|
||||||
28
src/app/api/charts/[id]/route.ts
Normal file
28
src/app/api/charts/[id]/route.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
import { db } from '@/lib/db';
|
||||||
|
import { charts, candles, annotations } from '@/lib/db/schema';
|
||||||
|
import { eq } from 'drizzle-orm';
|
||||||
|
|
||||||
|
export async function DELETE(
|
||||||
|
_request: NextRequest,
|
||||||
|
{ params }: { params: Promise<{ id: string }> }
|
||||||
|
) {
|
||||||
|
const { id } = await params;
|
||||||
|
const chartId = parseInt(id, 10);
|
||||||
|
|
||||||
|
if (isNaN(chartId)) {
|
||||||
|
return NextResponse.json({ error: 'Invalid chart ID' }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const existing = await db.select().from(charts).where(eq(charts.id, chartId)).limit(1);
|
||||||
|
if (existing.length === 0) {
|
||||||
|
return NextResponse.json({ error: 'Chart not found' }, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete in order: annotations, candles, then chart (application-level cascade)
|
||||||
|
await db.delete(annotations).where(eq(annotations.chart_id, chartId));
|
||||||
|
await db.delete(candles).where(eq(candles.chart_id, chartId));
|
||||||
|
await db.delete(charts).where(eq(charts.id, chartId));
|
||||||
|
|
||||||
|
return NextResponse.json({ success: true });
|
||||||
|
}
|
||||||
9
src/app/api/charts/route.ts
Normal file
9
src/app/api/charts/route.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
import { NextResponse } from 'next/server';
|
||||||
|
import { db } from '@/lib/db';
|
||||||
|
import { charts } from '@/lib/db/schema';
|
||||||
|
import { desc } from 'drizzle-orm';
|
||||||
|
|
||||||
|
export async function GET() {
|
||||||
|
const allCharts = await db.select().from(charts).orderBy(desc(charts.created_at));
|
||||||
|
return NextResponse.json(allCharts);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue