ba-cafe

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

route.ts (4001B)


      1 import { NextResponse } from 'next/server';
      2 import { createClient } from '@supabase/supabase-js';
      3 
      4 export async function OPTIONS() {
      5   return new NextResponse(null, {
      6     status: 204,
      7     headers: {
      8       'Access-Control-Allow-Origin': 'https://rabbit1.cc',
      9       'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
     10       'Access-Control-Allow-Headers': 'Content-Type, Authorization',
     11     },
     12   });
     13 }
     14 
     15 export async function GET(request: Request) {
     16   try {
     17     const authHeader = request.headers.get('Authorization');
     18     if (!authHeader) return NextResponse.json({ error: 'No token' }, { status: 401 });
     19 
     20     const supabase = createClient(
     21       process.env.NEXT_PUBLIC_SUPABASE_URL!,
     22       process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
     23       { global: { headers: { Authorization: authHeader } } }
     24     );
     25 
     26     const { data: { user }, error: authError } = await supabase.auth.getUser();
     27     if (authError || !user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
     28 
     29     const { data: relationships, error } = await supabase
     30       .from('relationship')
     31       .select('id, char_key, bond_level, recorded_at')
     32       .eq('user_id', user.id)
     33       .order('recorded_at', { ascending: false });
     34 
     35     if (error) throw error;
     36 
     37     return NextResponse.json(relationships || []);
     38   } catch (error: any) {
     39     console.error("GET API Error:", error);
     40     return NextResponse.json({ error: error.message }, { status: 500 });
     41   }
     42 }
     43 
     44 export async function POST(request: Request) {
     45   try {
     46     const body = await request.json();
     47     const { char_key, bond_level, recorded_at } = body;
     48     const authHeader = request.headers.get('Authorization');
     49     
     50     if (!authHeader) return NextResponse.json({ error: 'No token' }, { status: 401 });
     51 
     52     const supabase = createClient(
     53       process.env.NEXT_PUBLIC_SUPABASE_URL!,
     54       process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
     55       { global: { headers: { Authorization: authHeader } } }
     56     );
     57 
     58     const { data: { user }, error: authError } = await supabase.auth.getUser();
     59     if (authError || !user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
     60 
     61     const { data: existingRecord, error: checkError } = await supabase
     62       .from('relationship')
     63       .select('id')
     64       .eq('user_id', user.id)
     65       .eq('char_key', char_key)
     66       .eq('bond_level', bond_level)
     67       .maybeSingle();
     68 
     69     if (checkError) throw checkError;
     70 
     71     if (existingRecord) {
     72       return NextResponse.json(
     73         { error: `RANK ${bond_level} は既に登録されています。` }, 
     74         { status: 400 }
     75       );
     76     }
     77 
     78     const { error } = await supabase
     79       .from('relationship')
     80       .insert({
     81         user_id: user.id,
     82         char_key,
     83         bond_level,
     84         recorded_at
     85       });
     86 
     87     if (error) throw error;
     88 
     89     return NextResponse.json({ success: true });
     90   } catch (error: any) {
     91     console.error("POST API Error:", error);
     92     return NextResponse.json({ error: error.message }, { status: 500 });
     93   }
     94 }
     95 
     96 export async function DELETE(request: Request) {
     97   try {
     98     const body = await request.json();
     99     const { id } = body;
    100     const authHeader = request.headers.get('Authorization');
    101 
    102     if (!authHeader) return NextResponse.json({ error: 'No token' }, { status: 401 });
    103 
    104     const supabase = createClient(
    105       process.env.NEXT_PUBLIC_SUPABASE_URL!,
    106       process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
    107       { global: { headers: { Authorization: authHeader } } }
    108     );
    109 
    110     const { data: { user }, error: authError } = await supabase.auth.getUser();
    111     if (authError || !user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
    112 
    113     const { error } = await supabase
    114       .from('relationship')
    115       .delete()
    116       .eq('id', id)
    117       .eq('user_id', user.id);
    118 
    119     if (error) throw error;
    120 
    121     return NextResponse.json({ success: true });
    122   } catch (error: any) {
    123     console.error("DELETE API Error:", error);
    124     return NextResponse.json({ error: error.message }, { status: 500 });
    125   }
    126 }