|
| 1 | +import { endOfMonth, format, startOfMonth } from 'date-fns'; |
1 | 2 | import { NextResponse } from 'next/server'; |
2 | 3 |
|
3 | 4 | import createClient from '@/lib/supabase/server'; |
4 | 5 | import { ApiResponse } from '@/types/apiRoute'; |
5 | 6 | import { Song } from '@/types/song'; |
6 | 7 |
|
7 | | -interface ResponseRecentAddSong { |
8 | | - songs: Song; |
9 | | -} |
10 | | -export async function GET( |
11 | | - request: Request, |
12 | | -): Promise<NextResponse<ApiResponse<ResponseRecentAddSong[]>>> { |
| 8 | +export async function GET(request: Request): Promise<NextResponse<ApiResponse<Song[]>>> { |
13 | 9 | try { |
14 | 10 | const { searchParams } = new URL(request.url); |
15 | 11 |
|
16 | | - const year = Number(searchParams.get('year')) || 0; |
17 | | - const month = Number(searchParams.get('month')) || 0; |
| 12 | + const now = new Date(); |
| 13 | + const year = Number(searchParams.get('year')); |
| 14 | + const month = Number(searchParams.get('month')); |
18 | 15 |
|
19 | | - const startDate = new Date(year, month, 1); |
20 | | - const endDate = new Date(year, month + 1, 1); |
| 16 | + // date-fns의 month는 0-indexed이므로 1을 빼줌 (사용자 입력은 1-12) |
| 17 | + const baseDate = new Date(year, month, 1); |
| 18 | + const startDate = format(startOfMonth(baseDate), 'yyyy-MM-01'); |
| 19 | + const endDate = format(endOfMonth(baseDate), 'yyyy-MM-dd'); |
21 | 20 |
|
22 | 21 | const supabase = await createClient(); |
23 | 22 |
|
24 | | - // songs 테이블의 startDate, endDate 사이의 데이터를 가져옴 |
| 23 | + // songs 테이블의 release 날짜가 해당 월의 시작일부터 마지막일 사이인 데이터를 가져옴 |
25 | 24 | 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으로 정렬 |
| 25 | + .from('songs') |
| 26 | + .select('*') |
| 27 | + .gte('release', startDate) |
| 28 | + .lte('release', endDate) |
| 29 | + .order('release', { ascending: false }) |
| 30 | + .limit(100); |
32 | 31 |
|
33 | 32 | if (recentError) throw recentError; |
34 | 33 |
|
35 | | - return NextResponse.json({ success: true, data }); |
| 34 | + return NextResponse.json({ success: true, data: data as Song[] }); |
36 | 35 | } catch (error) { |
37 | 36 | if (error instanceof Error && error.cause === 'auth') { |
38 | 37 | return NextResponse.json( |
|
0 commit comments