added supabase

This commit is contained in:
Austin
2026-02-20 09:12:47 -06:00
parent 3c293159ff
commit 76f1241761
54 changed files with 3162 additions and 445 deletions
-68
View File
@@ -1,68 +0,0 @@
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 });
}
}
+81
View File
@@ -0,0 +1,81 @@
import { createClient } from "@/utils/supabase/server";
import { NextRequest, NextResponse } from "next/server";
export async function POST(req: NextRequest) {
try {
const supabase = await createClient();
const { name, email, comment, postId } = await req.json();
if (!name || !email || !comment || !postId) {
return NextResponse.json(
{ message: "All fields are required" },
{ status: 400 }
);
}
const { data, error } = await supabase.from("comments").insert([
{
name,
email,
comment,
post_id: postId,
},
]);
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);
return NextResponse.json(
{ message: "Internal server error" },
{ status: 500 }
);
}
}
export async function GET(req: NextRequest) {
try {
const supabase = await createClient();
const { searchParams } = new URL(req.url);
const postId = searchParams.get("postId");
if (!postId) {
return NextResponse.json(
{ message: "Post ID is required" },
{ status: 400 }
);
}
const { data: comments, error } = await supabase
.from("comments")
.select("*")
.eq("post_id", postId)
.order("created_at", { ascending: false });
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);
return NextResponse.json(
{ message: "Internal server error" },
{ status: 500 }
);
}
}
-61
View File
@@ -1,61 +0,0 @@
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 });
}
}
+70
View File
@@ -0,0 +1,70 @@
import { createClient } from "@/utils/supabase/server";
import { NextRequest, NextResponse } from "next/server";
export async function POST(req: NextRequest) {
const supabase = await createClient();
const { email } = await req.json();
if (!email) {
return NextResponse.json(
{ message: "Email is required." },
{ status: 400 }
);
}
const emailRegex = /\S+@\S+\.\S+/;
if (!emailRegex.test(email)) {
return NextResponse.json(
{ message: "Invalid email address." },
{ status: 400 }
);
}
try {
const { data: existingEmails, error: selectError } = await supabase
.from("subscriptions")
.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 }
);
}
const { error: insertError } = await supabase
.from("subscriptions")
.insert([{ email }]);
if (insertError) {
console.error("Failed to save subscription:", insertError);
return NextResponse.json(
{
message:
"An unexpected error occurred while saving subscription.",
},
{ status: 500 }
);
}
return NextResponse.json(
{ message: "Successfully subscribed!" },
{ status: 200 }
);
} catch (error) {
console.error("An unexpected server error occurred:", error);
return NextResponse.json(
{ message: "An unexpected error occurred." },
{ status: 500 }
);
}
}