Initial commit for Start9 packaging

This commit is contained in:
MacPro
2026-02-28 09:27:26 -06:00
commit 1b64c45c52
124 changed files with 15671 additions and 0 deletions
@@ -0,0 +1,189 @@
import { getCurrentUser } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
/**
* GET /api/exercises/[id]
* Get exercise with history
*/
export async function GET(
_request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const user = await getCurrentUser();
if (!user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const exercise = await prisma.exercise.findFirst({
where: {
id: params.id,
userId: user.id,
},
});
if (!exercise) {
return NextResponse.json({ error: "Exercise not found" }, { status: 404 });
}
// Get exercise history grouped by workout
const setLogs = await prisma.setLog.findMany({
where: {
exerciseId: params.id,
workout: {
userId: user.id,
},
},
include: {
workout: {
select: {
id: true,
date: true,
name: true,
},
},
},
orderBy: [
{ workout: { date: "desc" } },
{ setNumber: "asc" },
],
take: 100,
});
// Group by workout
const workoutMap = new Map<string, { workout: any; sets: any[] }>();
for (const log of setLogs) {
const key = log.workoutId;
if (!workoutMap.has(key)) {
workoutMap.set(key, { workout: log.workout, sets: [] });
}
workoutMap.get(key)!.sets.push(log);
}
const history = Array.from(workoutMap.values()).slice(0, 20);
return NextResponse.json({
exercise,
history,
});
} catch (error) {
console.error("GET /api/exercises/[id] error:", error);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
}
/**
* PATCH /api/exercises/[id]
* Edit exercise details
*/
const updateExerciseSchema = z.object({
name: z.string().min(1).optional(),
type: z.string().min(1).optional(),
muscleGroups: z.array(z.string()).optional(),
description: z.string().optional(),
inputFields: z.array(z.string().min(1)).optional(),
defaultWeightUnit: z.string().nullable().optional(),
});
export async function PATCH(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const user = await getCurrentUser();
if (!user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const exercise = await prisma.exercise.findFirst({
where: {
id: params.id,
userId: user.id,
},
});
if (!exercise) {
return NextResponse.json({ error: "Exercise not found" }, { status: 404 });
}
const body = await request.json();
const validated = updateExerciseSchema.parse(body);
const data: any = {};
if (validated.name !== undefined) data.name = validated.name;
if (validated.type !== undefined) data.type = validated.type;
if (validated.description !== undefined) data.description = validated.description;
if (validated.muscleGroups !== undefined)
data.muscleGroups = JSON.stringify(validated.muscleGroups);
if (validated.inputFields !== undefined)
data.inputFields = JSON.stringify(validated.inputFields);
if (validated.defaultWeightUnit !== undefined)
data.defaultWeightUnit = validated.defaultWeightUnit;
const updated = await prisma.exercise.update({
where: { id: params.id },
data,
});
return NextResponse.json(updated);
} catch (error) {
if (error instanceof z.ZodError) {
return NextResponse.json(
{ error: "Invalid data", details: error.errors },
{ status: 400 }
);
}
console.error("PATCH /api/exercises/[id] error:", error);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
}
/**
* DELETE /api/exercises/[id]
*/
export async function DELETE(
_request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const user = await getCurrentUser();
if (!user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const exercise = await prisma.exercise.findFirst({
where: {
id: params.id,
userId: user.id,
},
});
if (!exercise) {
return NextResponse.json({ error: "Exercise not found" }, { status: 404 });
}
await prisma.setLog.deleteMany({
where: { exerciseId: params.id },
});
await prisma.exercise.delete({
where: { id: params.id },
});
return NextResponse.json({ message: "Exercise deleted successfully" });
} catch (error) {
console.error("DELETE /api/exercises/[id] error:", error);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
}
+124
View File
@@ -0,0 +1,124 @@
import { getCurrentUser } from "@/lib/auth";
import { getExercises, createExercise } from "@/lib/db/exercises";
import { prisma } from "@/lib/prisma";
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
const CreateExerciseSchema = z.object({
name: z.string().min(1, "Exercise name is required"),
type: z.string().min(1),
muscleGroups: z.array(z.string()).default([]),
description: z.string().optional(),
inputFields: z.array(z.string().min(1)).optional(),
defaultWeightUnit: z.string().nullable().optional(),
});
/**
* GET /api/exercises
*/
export async function GET(request: NextRequest) {
try {
const user = await getCurrentUser();
if (!user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const { searchParams } = new URL(request.url);
const query = searchParams.get("q");
let exercises;
if (query) {
exercises = await prisma.exercise.findMany({
where: {
userId: user.id,
name: { contains: query },
},
orderBy: { name: "asc" },
});
} else {
exercises = await getExercises(user.id);
}
return NextResponse.json(exercises);
} catch (error) {
console.error("GET /api/exercises error:", error);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
}
/**
* POST /api/exercises
*/
export async function POST(request: NextRequest) {
try {
const user = await getCurrentUser();
if (!user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const body = await request.json();
const validated = CreateExerciseSchema.parse(body);
const existing = await prisma.exercise.findUnique({
where: {
userId_name: {
userId: user.id,
name: validated.name,
},
},
});
if (existing) {
return NextResponse.json(
{ error: "Exercise already exists" },
{ status: 400 }
);
}
// Determine default inputFields based on type
let inputFields = validated.inputFields;
if (!inputFields) {
if (validated.type === "cardio") {
inputFields = ["sets", "duration", "calories"];
} else {
inputFields = ["sets", "reps", "weight"];
}
}
// Kettlebell defaults to kg
let defaultWeightUnit = validated.defaultWeightUnit;
if (defaultWeightUnit === undefined && validated.type === "kettlebell") {
defaultWeightUnit = "kg";
}
const exercise = await createExercise({
userId: user.id,
name: validated.name,
type: validated.type,
description: validated.description,
muscleGroups: JSON.stringify(validated.muscleGroups),
inputFields: JSON.stringify(inputFields),
defaultWeightUnit: defaultWeightUnit || null,
isCustom: true,
});
return NextResponse.json(exercise, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return NextResponse.json(
{ error: "Validation error", details: error.errors },
{ status: 400 }
);
}
console.error("POST /api/exercises error:", error);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
}