mirror of
https://github.com/soconnor0919/hristudio.git
synced 2025-12-12 07:04:44 -05:00
Update participant and study API routes
This commit is contained in:
37
src/app/api/participants/[id]/route.ts
Normal file
37
src/app/api/participants/[id]/route.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { db } from "~/server/db";
|
||||
import { participants } from "~/server/db/schema";
|
||||
import { NextResponse } from "next/server";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
export async function DELETE(
|
||||
request: Request,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
console.log('DELETE route hit, params:', params);
|
||||
const id = parseInt(params.id);
|
||||
|
||||
if (isNaN(id)) {
|
||||
console.log('Invalid ID:', id);
|
||||
return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('Attempting to delete participant with ID:', id);
|
||||
const deletedParticipant = await db.delete(participants)
|
||||
.where(eq(participants.id, id))
|
||||
.returning();
|
||||
|
||||
console.log('Deleted participant:', deletedParticipant);
|
||||
|
||||
if (deletedParticipant.length === 0) {
|
||||
console.log('Participant not found');
|
||||
return NextResponse.json({ error: 'Participant not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
console.log('Participant deleted successfully');
|
||||
return NextResponse.json({ message: "Participant deleted successfully" });
|
||||
} catch (error) {
|
||||
console.error('Error deleting participant:', error);
|
||||
return NextResponse.json({ error: 'Failed to delete participant', details: String(error) }, { status: 500 });
|
||||
}
|
||||
}
|
||||
25
src/app/api/participants/route.ts
Normal file
25
src/app/api/participants/route.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { db } from "~/server/db";
|
||||
import { participants } from "~/server/db/schema";
|
||||
import { NextResponse } from "next/server";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const studyId = searchParams.get('studyId');
|
||||
|
||||
if (!studyId) {
|
||||
return NextResponse.json({ error: 'Study ID is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const allParticipants = await db.select().from(participants).where(eq(participants.studyId, parseInt(studyId)));
|
||||
return NextResponse.json(allParticipants);
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const { name, studyId } = await request.json();
|
||||
if (!name || !studyId) {
|
||||
return NextResponse.json({ error: 'Name and Study ID are required' }, { status: 400 });
|
||||
}
|
||||
const newParticipant = await db.insert(participants).values({ name, studyId }).returning();
|
||||
return NextResponse.json(newParticipant[0]);
|
||||
}
|
||||
86
src/app/api/studies/[id]/route.ts
Normal file
86
src/app/api/studies/[id]/route.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { db } from "~/server/db";
|
||||
import { studies } from "~/server/db/schema";
|
||||
import { NextResponse } from "next/server";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import { auth } from "@clerk/nextjs/server";
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
const { userId } = auth();
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) {
|
||||
return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const study = await db.select().from(studies).where(and(eq(studies.id, id), eq(studies.userId, userId))).limit(1);
|
||||
|
||||
if (study.length === 0) {
|
||||
return NextResponse.json({ error: 'Study not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json(study[0]);
|
||||
}
|
||||
|
||||
export async function PUT(
|
||||
request: Request,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
const { userId } = auth();
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) {
|
||||
return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const { title, description } = await request.json();
|
||||
if (!title) {
|
||||
return NextResponse.json({ error: 'Title is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const updatedStudy = await db
|
||||
.update(studies)
|
||||
.set({ title, description })
|
||||
.where(and(eq(studies.id, id), eq(studies.userId, userId)))
|
||||
.returning();
|
||||
|
||||
if (updatedStudy.length === 0) {
|
||||
return NextResponse.json({ error: 'Study not found or unauthorized' }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json(updatedStudy[0]);
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: Request,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
const { userId } = auth();
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) {
|
||||
return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const deletedStudy = await db
|
||||
.delete(studies)
|
||||
.where(and(eq(studies.id, id), eq(studies.userId, userId)))
|
||||
.returning();
|
||||
|
||||
if (deletedStudy.length === 0) {
|
||||
return NextResponse.json({ error: 'Study not found or unauthorized' }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: "Study deleted successfully" });
|
||||
}
|
||||
@@ -2,30 +2,29 @@ import { db } from "~/server/db";
|
||||
import { studies } from "~/server/db/schema";
|
||||
import { NextResponse } from "next/server";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { auth } from "@clerk/nextjs/server";
|
||||
|
||||
export async function GET() {
|
||||
const allStudies = await db.select().from(studies);
|
||||
export async function GET(request: Request) {
|
||||
const { userId } = auth();
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const allStudies = await db.select().from(studies).where(eq(studies.userId, userId));
|
||||
return NextResponse.json(allStudies);
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const { userId } = auth();
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const { title, description } = await request.json();
|
||||
const newStudy = await db.insert(studies).values({ title, description }).returning();
|
||||
if (!title) {
|
||||
return NextResponse.json({ error: 'Title is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const newStudy = await db.insert(studies).values({ title, description, userId }).returning();
|
||||
return NextResponse.json(newStudy[0]);
|
||||
}
|
||||
|
||||
export async function PUT(request: Request) {
|
||||
const { id, title, description } = await request.json();
|
||||
const updatedStudy = await db
|
||||
.update(studies)
|
||||
.set({ title, description })
|
||||
.where(eq(studies.id, id))
|
||||
.returning();
|
||||
return NextResponse.json(updatedStudy[0]);
|
||||
}
|
||||
|
||||
export async function DELETE(request: Request) {
|
||||
const { id } = await request.json();
|
||||
await db.delete(studies).where(eq(studies.id, id));
|
||||
return NextResponse.json({ message: "Study deleted" });
|
||||
}
|
||||
21
src/app/api/users/route.ts
Normal file
21
src/app/api/users/route.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { db } from "~/server/db";
|
||||
import { users } from "~/server/db/schema";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { email } = await request.json();
|
||||
|
||||
// Check if email is provided
|
||||
if (!email) {
|
||||
return NextResponse.json({ error: "Email is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Insert the new user into the database
|
||||
const newUser = await db.insert(users).values({ email }).returning();
|
||||
return NextResponse.json(newUser[0]);
|
||||
} catch (error) {
|
||||
console.error("Error creating user:", error);
|
||||
return NextResponse.json({ error: "Failed to create user" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import { type PropsWithChildren } from "react"
|
||||
import { Sidebar } from "~/components/sidebar"
|
||||
import { Inter } from "next/font/google"
|
||||
|
||||
import "~/styles/globals.css"
|
||||
|
||||
const inter = Inter({
|
||||
subsets: ["latin"],
|
||||
display: "swap",
|
||||
variable: "--font-sans",
|
||||
})
|
||||
|
||||
export default function RootLayout({ children }: PropsWithChildren) {
|
||||
return (
|
||||
<html lang="en">
|
||||
<body className={`font-sans ${inter.variable}`}>
|
||||
<div className="flex h-screen">
|
||||
<Sidebar />
|
||||
<main className="flex-1 overflow-y-auto">
|
||||
{children}
|
||||
</main>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
)
|
||||
}
|
||||
@@ -1,21 +1,45 @@
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '~/components/ui/card';
|
||||
import { Button } from '~/components/ui/button';
|
||||
import { Studies } from "~/components/Studies";
|
||||
import Layout from "~/components/layout";
|
||||
import { Card, CardHeader, CardTitle, CardContent } from "~/components/ui/card";
|
||||
|
||||
const HomePage: React.FC = () => {
|
||||
return (
|
||||
<div className="min-h-screen bg-gradient-to-b from-blue-100 to-white pt-14 lg:pt-0">
|
||||
<div className="container mx-auto px-4 py-16">
|
||||
<header className="text-center mb-16">
|
||||
<h1 className="text-5xl font-bold mb-4 text-blue-800">Welcome to the HRIStudio Dashboard!</h1>
|
||||
<p className="text-xl text-gray-600 max-w-3xl mx-auto">
|
||||
Manage your Human-Robot Interaction projects and experiments
|
||||
</p>
|
||||
</header>
|
||||
<Studies />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
const DashboardPage: React.FC = () => {
|
||||
return (
|
||||
<Layout>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Platform Information</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{/* Add content for Platform Information */}
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Participants</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{/* Add content for Participants */}
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Project Members</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{/* Add content for Project Members */}
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Completed Trials</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{/* Add content for Completed Trials */}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</Layout>
|
||||
);
|
||||
};
|
||||
|
||||
export default HomePage;
|
||||
export default DashboardPage;
|
||||
@@ -1,5 +1,6 @@
|
||||
import { ClerkProvider } from '@clerk/nextjs'
|
||||
import { Inter } from "next/font/google"
|
||||
import { StudyProvider } from '~/context/StudyContext'
|
||||
|
||||
import "~/styles/globals.css"
|
||||
|
||||
@@ -10,7 +11,7 @@ const inter = Inter({
|
||||
})
|
||||
|
||||
export const metadata = {
|
||||
title: "T3 App",
|
||||
title: "HRIStudio",
|
||||
description: "Created with create-t3-app",
|
||||
icons: [{ rel: "icon", url: "/favicon.ico" }],
|
||||
}
|
||||
@@ -18,13 +19,13 @@ export const metadata = {
|
||||
export default function RootLayout({ children }: React.PropsWithChildren) {
|
||||
return (
|
||||
<ClerkProvider>
|
||||
{/* <ThemeProvider attribute="class" defaultTheme="system" enableSystem> */}
|
||||
<StudyProvider>
|
||||
<html lang="en" className={inter.variable}>
|
||||
<body className="font-sans">
|
||||
{children}
|
||||
</body>
|
||||
</html>
|
||||
{/* </ThemeProvider> */}
|
||||
</StudyProvider>
|
||||
</ClerkProvider>
|
||||
)
|
||||
}
|
||||
12
src/app/participants/page.tsx
Normal file
12
src/app/participants/page.tsx
Normal file
@@ -0,0 +1,12 @@
|
||||
import Layout from "~/components/layout";
|
||||
import { Participants } from "~/components/participant/Participants";
|
||||
|
||||
const ParticipantsPage = () => {
|
||||
return (
|
||||
<Layout>
|
||||
<Participants />
|
||||
</Layout>
|
||||
);
|
||||
};
|
||||
|
||||
export default ParticipantsPage;
|
||||
@@ -29,6 +29,20 @@ export default function SignUpPage() {
|
||||
|
||||
if (result.status === "complete") {
|
||||
await setActive({ session: result.createdSessionId })
|
||||
|
||||
// Create a user entry in the database
|
||||
const response = await fetch('/api/users', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ email: emailAddress }),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json()
|
||||
console.error("Error creating user in database:", errorData.error)
|
||||
return // Optionally handle the error (e.g., show a message to the user)
|
||||
}
|
||||
|
||||
router.push("/dash")
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
10
src/app/studies/page.tsx
Normal file
10
src/app/studies/page.tsx
Normal file
@@ -0,0 +1,10 @@
|
||||
import Layout from "~/components/layout";
|
||||
import { Studies } from "~/components/study/Studies";
|
||||
|
||||
export default function StudiesPage() {
|
||||
return (
|
||||
<Layout>
|
||||
<Studies />
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user