Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution script #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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",
@@ -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);
244 changes: 240 additions & 4 deletions package-lock.json
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -11,6 +11,9 @@
"dependencies": {
"@ant-design/nextjs-registry": "^1.0.1",
"antd": "^5.21.1",
"dotenv": "^16.4.5",
"mongodb": "^6.9.0",
"mongoose": "^8.6.3",
"next": "14.2.13",
"react": "^18",
"react-dom": "^18"