You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

50 lines
1.4 KiB

import {getRequest, postRequest} from "./shared";
import {PastWorkout, WorkoutState} from "../models/Workouts";
interface WorkoutFilter {
daysBack?: number
includeTest: boolean
}
export interface CreateWorkoutOptions {
deviceId: string
programId?: string
test?: boolean
}
interface WorkoutRepository {
findById(workoutId: string): Promise<PastWorkout | null>
fetchByFilter(filter: WorkoutFilter): Promise<PastWorkout[]>
fetchStates(workoutId: string): Promise<WorkoutState[]>
createWorkout(options: CreateWorkoutOptions): Promise<boolean>
}
export default function workoutRepo(): WorkoutRepository {
switch (import.meta.env.VITE_MODE) {
case "webapp":
return defaultImpl;
case "chrome-plugin":
default:
throw new Error("Not implemented");
}
}
const defaultImpl: WorkoutRepository = {
findById(workoutId: string): Promise<PastWorkout | null> {
return getRequest<PastWorkout>(`/workouts/${workoutId}`).catch(() => null);
},
fetchByFilter({daysBack, includeTest}: WorkoutFilter): Promise<PastWorkout[]> {
return getRequest(`/workouts?daysBack=${daysBack}&includeTest=${includeTest}`);
},
fetchStates(workoutId: string): Promise<WorkoutState[]> {
return getRequest(`/workouts/${workoutId}/states`);
},
async createWorkout(options: CreateWorkoutOptions) {
try {
await postRequest("/workouts", options);
return true;
} catch (e) {
return false;
}
},
};