AI, API, Profile Page Update

This commit is contained in:
2025-04-13 03:00:45 -04:00
parent 203aa93923
commit 99981de104
9 changed files with 221 additions and 62 deletions

View File

@@ -1,13 +0,0 @@
import { db } from "../../lib/scripts/db";
export default function handler(req, res) {
if (req.method === 'GET') {
// Handle GET request
res.status(200).json({ message: 'Hello, this is a GET request!' });
} else {
// Handle unsupported methods
res.setHeader('Allow', ['GET']);
res.status(405).end(`Method ${req.method} Not Allowed`);
}
}

View File

@@ -0,0 +1,36 @@
import { NextResponse } from "next/server";
import { auth0 } from "../../../../lib/scripts/auth0";
import { db } from "@/lib/scripts/db";
async function authenticateUser() {
const session = await auth0.getSession();
if (!session) {
return NextResponse.json({ message: "No session found" }, { status: 401 });
}
const sessionUser = session.user;
let userData = await db.users.findByEmail(sessionUser.email as string);
if (!userData) return NextResponse.json({ message: "User not found" }, { status: 404 });
return userData != null;
}
export async function GET(req: Request, { params }: any) {
try {
if (!(await authenticateUser())) return;
const { id } = await params;
const post = await db.posts.getById(id);
if (!post) {
return NextResponse.json({ message: "Post not found" }, { status: 404 });
}
return NextResponse.json({ post }, { status: 200 });
} catch (error) {
console.error("Error finding post by ID:", error);
return NextResponse.json({ message: "Internal server error" }, { status: 500 });
}
}

55
src/app/api/post/route.ts Normal file
View File

@@ -0,0 +1,55 @@
import { NextResponse } from "next/server";
import { auth0 } from "@/lib/scripts/auth0";
import { db } from "@/lib/scripts/db";
import { gemini } from "@/lib/scripts/gemini";
async function authenticateUser() {
const session = await auth0.getSession();
if (!session) {
return NextResponse.json({ message: "No session found" }, { status: 401 });
}
const sessionUser = session.user;
let userData = await db.users.findByEmail(sessionUser.email as string);
if (!userData)
return NextResponse.json({ message: "User not found" }, { status: 404 });
return userData;
}
export async function POST(req: Request) {
try {
// if (!(await authenticateUser())) return;
const formData = await req.formData();
const file = formData.get("image");
if (!file || !(file instanceof Blob)) {
return NextResponse.json(
{ message: "No image file provided" },
{ status: 400 }
);
}
const arrayBuffer = await file.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
const prompt = `Generate a 1-3 sentence description for this image.`;
const data = await gemini.generateDescription(prompt, buffer);
let postData = await db.posts.create("6gi1f", data?.description);
return NextResponse.json(
{ message: "Image uploaded successfully", postData },
{ status: 200 }
);
} catch (error) {
console.error("Error handling image upload:", error);
return NextResponse.json(
{ message: "Internal server error" },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,36 @@
import { NextResponse } from "next/server";
import { auth0 } from "../../../../lib/scripts/auth0";
import { db } from "@/lib/scripts/db";
async function authenticateUser() {
const session = await auth0.getSession();
if (!session) {
return NextResponse.json({ message: "No session found" }, { status: 401 });
}
const sessionUser = session.user;
let userData = await db.users.findByEmail(sessionUser.email as string);
if (!userData) return NextResponse.json({ message: "User not found" }, { status: 404 });
return userData != null;
}
export async function GET(req: Request, { params }: any) {
try {
if (!(await authenticateUser())) return;
const { id } = params;
const user = await db.users.findById(id);
if (!user) {
return NextResponse.json({ message: "User not found" }, { status: 404 });
}
return NextResponse.json({ user }, { status: 200 });
} catch (error) {
console.error("Error finding user by ID:", error);
return NextResponse.json({ message: "Internal server error" }, { status: 500 });
}
}