Add Supabase integration for trading journal
This commit is contained in:
@@ -1,39 +1,39 @@
|
||||
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')
|
||||
import { supabase } from '@/lib/supabase'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
let trades = []
|
||||
if (fs.existsSync(dataFile)) {
|
||||
trades = JSON.parse(fs.readFileSync(dataFile, 'utf-8'))
|
||||
}
|
||||
return NextResponse.json({ trades })
|
||||
const { data: trades, error } = await supabase
|
||||
.from('trades')
|
||||
.select('*')
|
||||
.order('opened_at', { ascending: false })
|
||||
|
||||
if (error) throw error
|
||||
|
||||
return NextResponse.json({ trades: trades || [] })
|
||||
} catch (error) {
|
||||
return NextResponse.json({ trades: [] })
|
||||
console.error('Supabase error:', error)
|
||||
return NextResponse.json({ trades: [], error: 'Failed to fetch' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
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 })
|
||||
const { data, error } = await supabase
|
||||
.from('trades')
|
||||
.insert([{
|
||||
...body,
|
||||
opened_at: new Date().toISOString(),
|
||||
}])
|
||||
.select()
|
||||
|
||||
if (error) throw error
|
||||
|
||||
return NextResponse.json({ success: true, trade: data?.[0] })
|
||||
} catch (error) {
|
||||
console.error('Supabase error:', error)
|
||||
return NextResponse.json({ error: 'Failed to save' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user