Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions app/api/exercises/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { exercises } from "@/constants/fake-data";
import dbConnect from "@/lib/db-connect";
import { Exercise } from "@/models/Exercise";

export async function GET() {
return new Response(JSON.stringify(exercises), {
headers: {
"content-type": "application/json",
},
});
await dbConnect();
const exercises = await Exercise.find({});
return new Response(JSON.stringify(exercises));
}
63 changes: 58 additions & 5 deletions app/api/stats/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
import {stats} from "@/constants/fake-data";
import dbConnect from "@/lib/db-connect";
import { Workout } from "@/models/Workout";

export async function GET() {
return new Response(JSON.stringify(stats), {
headers: {
"content-type": "application/json",
await dbConnect();
const totalWorkouts = await Workout.countDocuments();
const totalReps = await Workout.aggregate([
{
$group: {
_id: null,
total: { $sum: { $multiply: ["$reps", "$sets"] } },
},
},
});
]);
const totalSets = await Workout.aggregate([
{
$group: {
_id: null,
total: { $sum: "$sets" },
},
},
]);
const totalLifted = await Workout.aggregate([
{
$group: {
_id: null,
total: { $sum: { $multiply: ["$reps", "$sets", "$weight"] } },
},
},
]);
// IF TIME PERMITS
const totalChestLifted = await Workout.aggregate([
{
// match chest exercises, which uses info from the Exercise collection
$lookup: {
from: "exercises",
localField: "exercise",
foreignField: "_id",
as: "exercise",
},
},
{
$match: {
"exercise.muscleGroup": "Chest",
},
},
{
$group: {
_id: null,
total: { $sum: { $multiply: ["$reps", "$sets", "$weight"] } },
},
},
]);
console.log("totalChestLifted", totalChestLifted);
const stats = {
totalWorkouts: await totalWorkouts,
totalReps: totalReps[0]?.total || 0,
totalSets: totalSets[0]?.total || 0,
totalLifted: totalLifted[0]?.total || 0,
}
return new Response(JSON.stringify(stats));
}
15 changes: 11 additions & 4 deletions app/api/workouts/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { workouts } from "@/constants/fake-data";
import dbConnect from "@/lib/db-connect";
import { Workout } from "@/models/Workout";

export function GET() {
export async function GET() {
await dbConnect();

const workouts = await Workout.find({}).populate("exercise").exec();
return new Response(JSON.stringify(workouts), {
headers: {
"content-type": "application/json",
Expand All @@ -9,7 +13,10 @@ export function GET() {
}

export async function POST(request: Request) {
await dbConnect();

const body = await request.json();
console.log(body)
return new Response();
const doc = new Workout(body);
const response = await doc.save();
return new Response(JSON.stringify(response));
}
13 changes: 13 additions & 0 deletions models/Exercise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Schema, model, models} from "mongoose";
import {ExerciseType} from "../types/types";

export const exerciseSchema = new Schema<ExerciseType>({
name: String,
muscleGroup: String,
equipment: String,
difficulty: Number,
});

console.log(models)

export const Exercise = models.Exercise || model<ExerciseType>("Exercise", exerciseSchema);
14 changes: 14 additions & 0 deletions models/Workout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Schema, model, models} from "mongoose";
import {WorkoutType} from "../types/types";

const workoutSchema = new Schema<WorkoutType>({
exercise: {
type: Schema.Types.ObjectId,
ref: "Exercise",
},
reps: Number,
sets: Number,
weight: Number,
});

export const Workout = models.Workout || model<WorkoutType>("Workout", workoutSchema);
Loading