mirror of
https://github.com/soconnor0919/hristudio.git
synced 2025-12-12 07:04:44 -05:00
ability to create trials added; form uploader cleaned up
This commit is contained in:
@@ -1,18 +1,36 @@
|
||||
import { db } from "~/server/db";
|
||||
import { participants } from "~/server/db/schema";
|
||||
import { participants, trialParticipants, trials } from "~/server/db/schema";
|
||||
import { NextResponse } from "next/server";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { eq, sql } 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 });
|
||||
}
|
||||
try {
|
||||
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);
|
||||
const participantsWithLatestTrial = await db
|
||||
.select({
|
||||
id: participants.id,
|
||||
name: participants.name,
|
||||
createdAt: participants.createdAt,
|
||||
latestTrialTimestamp: sql<Date | null>`MAX(${trials.createdAt})`.as('latestTrialTimestamp')
|
||||
})
|
||||
.from(participants)
|
||||
.leftJoin(trialParticipants, eq(participants.id, trialParticipants.participantId))
|
||||
.leftJoin(trials, eq(trialParticipants.trialId, trials.id))
|
||||
.where(eq(participants.studyId, parseInt(studyId)))
|
||||
.groupBy(participants.id)
|
||||
.orderBy(sql`COALESCE(MAX(${trials.createdAt}), ${participants.createdAt}) DESC`);
|
||||
|
||||
return NextResponse.json(participantsWithLatestTrial);
|
||||
} catch (error) {
|
||||
console.error('Error in GET /api/participants:', error);
|
||||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
|
||||
Reference in New Issue
Block a user