|
| 1 | +import { NextResponse } from 'next/server'; |
| 2 | + |
| 3 | +import createClient from '@/lib/supabase/server'; |
| 4 | +import { ApiResponse } from '@/types/apiRoute'; |
| 5 | +import { Song } from '@/types/song'; |
| 6 | + |
| 7 | +interface ResponseRecentAddSong { |
| 8 | + songs: Song; |
| 9 | +} |
| 10 | +export async function GET( |
| 11 | + request: Request, |
| 12 | +): Promise<NextResponse<ApiResponse<ResponseRecentAddSong[]>>> { |
| 13 | + try { |
| 14 | + const { searchParams } = new URL(request.url); |
| 15 | + |
| 16 | + const year = Number(searchParams.get('year')) || 0; |
| 17 | + const month = Number(searchParams.get('month')) || 0; |
| 18 | + |
| 19 | + const startDate = new Date(year, month, 1); |
| 20 | + const endDate = new Date(year, month + 1, 1); |
| 21 | + |
| 22 | + const supabase = await createClient(); |
| 23 | + |
| 24 | + // songs 테이블의 startDate, endDate 사이의 데이터를 가져옴 |
| 25 | + const { data, error: recentError } = await supabase |
| 26 | + .from('songs') // songs 테이블에서 검색 |
| 27 | + .select(`*`) |
| 28 | + .gte('created_at', startDate.toISOString()) |
| 29 | + .lte('created_at', endDate.toISOString()) |
| 30 | + .order('created_at', { ascending: false }) |
| 31 | + .limit(100); // 단순히 songs의 created_at으로 정렬 |
| 32 | + |
| 33 | + if (recentError) throw recentError; |
| 34 | + |
| 35 | + return NextResponse.json({ success: true, data }); |
| 36 | + } catch (error) { |
| 37 | + if (error instanceof Error && error.cause === 'auth') { |
| 38 | + return NextResponse.json( |
| 39 | + { |
| 40 | + success: false, |
| 41 | + error: 'User not authenticated', |
| 42 | + }, |
| 43 | + { status: 401 }, |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + console.error('Error in recent API:', error); |
| 48 | + return NextResponse.json( |
| 49 | + { success: false, error: 'Failed to get recent songs' }, |
| 50 | + { status: 500 }, |
| 51 | + ); |
| 52 | + } |
| 53 | +} |
0 commit comments