161 lines
2.7 KiB
TypeScript
161 lines
2.7 KiB
TypeScript
import { prisma } from "../prisma";
|
|
import { Workout } from "@prisma/client";
|
|
import { SearchFilters } from "@/types";
|
|
|
|
/**
|
|
* Get all workouts for a user with optional filters
|
|
*/
|
|
export async function getWorkouts(
|
|
userId: string,
|
|
filters?: SearchFilters
|
|
) {
|
|
const {
|
|
query,
|
|
exerciseId,
|
|
dateFrom,
|
|
dateTo,
|
|
limit = 50,
|
|
offset = 0,
|
|
} = filters || {};
|
|
|
|
const where: any = {
|
|
userId,
|
|
};
|
|
|
|
if (query) {
|
|
where.name = {
|
|
contains: query,
|
|
};
|
|
}
|
|
|
|
if (dateFrom || dateTo) {
|
|
where.date = {};
|
|
if (dateFrom) where.date.gte = dateFrom;
|
|
if (dateTo) where.date.lte = dateTo;
|
|
}
|
|
|
|
const workouts = await prisma.workout.findMany({
|
|
where,
|
|
include: {
|
|
setLogs: {
|
|
include: {
|
|
exercise: true,
|
|
},
|
|
orderBy: {
|
|
setNumber: "asc",
|
|
},
|
|
},
|
|
},
|
|
orderBy: {
|
|
date: "desc",
|
|
},
|
|
take: limit,
|
|
skip: offset,
|
|
});
|
|
|
|
// Filter by exerciseId if provided
|
|
if (exerciseId) {
|
|
return workouts.filter((workout) =>
|
|
workout.setLogs.some((set) => set.exerciseId === exerciseId)
|
|
);
|
|
}
|
|
|
|
return workouts;
|
|
}
|
|
|
|
/**
|
|
* Get a single workout by ID with all its sets
|
|
*/
|
|
export async function getWorkoutById(id: string) {
|
|
return prisma.workout.findUnique({
|
|
where: { id },
|
|
include: {
|
|
setLogs: {
|
|
include: {
|
|
exercise: true,
|
|
},
|
|
orderBy: {
|
|
setNumber: "asc",
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create a new workout
|
|
*/
|
|
export async function createWorkout(data: {
|
|
userId: string;
|
|
name: string;
|
|
date?: Date;
|
|
notes?: string;
|
|
duration?: number;
|
|
}): Promise<Workout> {
|
|
return prisma.workout.create({
|
|
data: {
|
|
userId: data.userId,
|
|
name: data.name,
|
|
date: data.date || new Date(),
|
|
notes: data.notes,
|
|
durationMinutes: data.duration,
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Delete a workout and all its associated sets
|
|
*/
|
|
export async function deleteWorkout(id: string): Promise<void> {
|
|
await prisma.setLog.deleteMany({
|
|
where: { workoutId: id },
|
|
});
|
|
|
|
await prisma.workout.delete({
|
|
where: { id },
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get recent workouts for a user
|
|
*/
|
|
export async function getRecentWorkouts(
|
|
userId: string,
|
|
limit: number = 10
|
|
) {
|
|
return prisma.workout.findMany({
|
|
where: { userId },
|
|
include: {
|
|
setLogs: {
|
|
include: {
|
|
exercise: true,
|
|
},
|
|
orderBy: {
|
|
setNumber: "asc",
|
|
},
|
|
},
|
|
},
|
|
orderBy: {
|
|
date: "desc",
|
|
},
|
|
take: limit,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Search workouts by name, date range, or exercise
|
|
*/
|
|
export async function searchWorkouts(
|
|
userId: string,
|
|
query: string,
|
|
dateFrom?: Date,
|
|
dateTo?: Date
|
|
) {
|
|
return getWorkouts(userId, {
|
|
query,
|
|
dateFrom,
|
|
dateTo,
|
|
limit: 100,
|
|
});
|
|
}
|