40 lines
1005 B
TypeScript
40 lines
1005 B
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import * as fs from 'fs'
|
|
import * as path from 'path'
|
|
|
|
const dataFile = path.join(process.cwd(), 'trading-trades.json')
|
|
|
|
export async function GET() {
|
|
try {
|
|
let trades = []
|
|
if (fs.existsSync(dataFile)) {
|
|
trades = JSON.parse(fs.readFileSync(dataFile, 'utf-8'))
|
|
}
|
|
return NextResponse.json({ trades })
|
|
} catch (error) {
|
|
return NextResponse.json({ trades: [] })
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json()
|
|
let trades = []
|
|
|
|
if (fs.existsSync(dataFile)) {
|
|
trades = JSON.parse(fs.readFileSync(dataFile, 'utf-8'))
|
|
}
|
|
|
|
trades.push({
|
|
...body,
|
|
id: Date.now().toString(),
|
|
openedAt: new Date().toISOString()
|
|
})
|
|
|
|
fs.writeFileSync(dataFile, JSON.stringify(trades, null, 2))
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
return NextResponse.json({ error: 'Failed to save' }, { status: 500 })
|
|
}
|
|
}
|