switched to approuter

This commit is contained in:
2025-08-18 13:10:47 -05:00
parent c2b0d905e1
commit 6cd941295c
82 changed files with 5154 additions and 9772 deletions
+68
View File
@@ -0,0 +1,68 @@
import { supabase } from '@/utils/supabase';
import { NextRequest, NextResponse } from 'next/server'; // Use next/server for Edge runtime
export const runtime = 'edge';
export default async function POST(req: NextRequest) {
try {
const { name, email, comment, postId } = await req.json();
if (!name || !email || !comment || !postId) {
return NextResponse.json({ message: 'All fields are required' }, { status: 400 });
}
// Insert data into the 'comments' table
const { data, error } = await supabase
.from('comments') // Replace 'comments' with your Supabase table name
.insert([
{
name,
email,
comment,
post_id: postId,
created_at: new Date().toISOString(), // Supabase often uses ISO strings for timestamps
},
]);
if (error) {
console.error('Error inserting comment:', error);
return NextResponse.json({ message: 'Error submitting comment', error }, { status: 500 });
}
return NextResponse.json({ message: 'Comment submitted successfully', data }, { status: 201 });
} catch (error) {
console.error('Internal server error during POST:', error);
// Catching and returning a 500 for unexpected errors
return NextResponse.json({ message: 'Internal server error' }, { status: 500 });
}
}
export async function GET(req: NextRequest) {
try {
// Get query parameters from the URL
const { searchParams } = new URL(req.url);
const postId = searchParams.get('postId');
if (!postId) {
return NextResponse.json({ message: 'Post ID is required' }, { status: 400 });
}
// Fetch comments for a specific postId, ordered by creation date descending
const { data: comments, error } = await supabase
.from('comments') // Replace 'comments' with your Supabase table name
.select('*')
.eq('post_id', postId) // Assuming your Supabase column for post ID is 'post_id'
.order('created_at', { ascending: false }); // Assuming your timestamp column is 'created_at'
if (error) {
console.error('Error fetching comments:', error);
return NextResponse.json({ message: 'Error fetching comments', error }, { status: 500 });
}
return NextResponse.json(comments, { status: 200 });
} catch (error) {
console.error('Internal server error during GET:', error);
// Catching and returning a 500 for unexpected errors
return NextResponse.json({ message: 'Internal server error' }, { status: 500 });
}
}
+61
View File
@@ -0,0 +1,61 @@
import { supabase } from '@/utils/supabase';
import { NextRequest, NextResponse } from 'next/server'; // Use next/server for Edge runtime
export const runtime = 'edge';
export default async function POST(req: NextRequest) {
// Only allow POST requests - this is handled by exporting the POST function
const { email } = await req.json(); // Get body from Edge request
// Check if email was provided
if (!email) {
return NextResponse.json({ message: 'Email is required.' }, { status: 400 });
}
// Validate email format
const emailRegex = /\S+@\S+\.\S+/;
if (!emailRegex.test(email)) {
return NextResponse.json({ message: 'Invalid email address.' }, { status: 400 });
}
try {
// Check if the email already exists in the 'subscriptions' table
const { data: existingEmails, error: selectError } = await supabase
.from('subscriptions') // Replace 'subscriptions' with your Supabase table name
.select('email')
.eq('email', email);
if (selectError) {
console.error('Error checking existing email:', selectError);
return NextResponse.json({ message: 'An unexpected error occurred while checking email.' }, { status: 500 });
}
if (existingEmails && existingEmails.length > 0) {
return NextResponse.json({ message: 'Email is already subscribed.' }, { status: 400 });
}
// Save email to the 'subscriptions' table
const { data: insertedData, error: insertError } = await supabase
.from('subscriptions') // Replace 'subscriptions' with your Supabase table name
.insert([
{
email,
created_at: new Date().toISOString(),
},
]);
if (insertError) {
console.error('Failed to save subscription:', insertError);
return NextResponse.json({ message: 'An unexpected error occurred while saving subscription.' }, { status: 500 });
}
// Respond with success
return NextResponse.json({ message: 'Successfully subscribed!' }, { status: 200 });
} catch (error) {
// Log unexpected errors
console.error('An unexpected server error occurred:', error);
return NextResponse.json({ message: 'An unexpected error occurred.' }, { status: 500 });
}
}