← back to Wine Finder Next

app/api/history/route.ts

29 lines

import { NextRequest, NextResponse } from 'next/server'

const googleSheets = require('@/lib/services/googleSheets')

export async function GET(request: NextRequest) {
  try {
    const searchParams = request.nextUrl.searchParams
    const wineName = searchParams.get('wine')

    if (!wineName) {
      return NextResponse.json({ error: 'Wine name is required' }, { status: 400 })
    }

    const history = await googleSheets.getWineHistory(wineName)

    return NextResponse.json({
      wine: wineName,
      history,
      count: history.length
    })
  } catch (error: any) {
    console.error('History error:', error)
    return NextResponse.json(
      { error: 'Failed to fetch history', message: error.message },
      { status: 500 }
    )
  }
}