vubao2303 / excercise-tracking

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

excercise-tracking

An Excerise Tracker application that is built using MongoDB. The application allows a user to track their workout routine by adding exercise types, resistance or cardio, followed by data for each including sets, reps, distance, etc. The user can add multiple exercises to one workout, and their statistics will be generated showing the total duration, total reps, total sets. Data for the last 7 workouts will also be displayed which provide another visual representation in addition to their physique to track progress over time.

Site Picture Site Picture

Heroku-Deployed

Deployed Link

Table of Contents

Project Introduction Table of Contents Heroku
Technologies Used Author License

Description of Page Building

  • In server.js

    • Requiring necessary npm packages
    • Setting up port and requiring models for syncing
    • Creating express app and configuring middleware needed for authentication
    • Requiring our routes
    • Listen to port to activate the server
  • In public folder

    • A css style with images and styling
    • Interface files such as exercise.html file and index.html and stats.
    • Clientside javascript files where it will listen to the clients request and send it to the back-end as well as poppulate chart
  • In routes folder to handle when the user "visit" the page

    • Html routes that serve up the html page when client calls
    • API routes to serve up JSON object use to populate the page
  • In models folder

    • excercise.js file uses mongoose to create table that content date, excercise array of type, name, duration, weight, reps, and sets
    • module.export Schema

Code Snippet

Install npm package npm install express mongoose morgan

Required variables

const express = require("express");
const logger = require("morgan");
const mongoose = require("mongoose");

Sets up the Express app to handle data parsing

const app = express();

app.use(logger("dev"));

app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.use(express.static("public"));

Set routes to handle when user "visit" the page

require("./routes/html-routes.js")(app);
app.use(require("./routes/api-routes.js"));

Use MongoDB aggregate function to dynamically add up and return the total duration for each workout

router.get("/api/workouts", (req,res)=>{
  db.aggregate( [
    { $addFields: {
        totalDuration: { $sum: "$exercises.duration" } 
      }
    }
 ] ).then(dbWorkouts =>{
      res.json(dbWorkouts)
    })
    .catch(err =>{
      res.json(err);
    });
  });

Create database model using mongoose Schema

const mongoose = require("mongoose");

const Schema = mongoose.Schema;

const UserWorkout = new Schema ({
    day: {type:Date,
    default:Date.now 
  },

  exercises:[{
    type:{type: String,trim: true,require: "type is required" },
    name: {type: String,trim: true,require: "name of excercise is required" ,
    duration: {type: Number },
    weight: {type: Number},
    reps: {type: Number},
    sets:{type:Number }, 
    distance: {type: Number}
  }],
  totalWorkoutDuration: {
    type:Number 
  }
});

const Workouts = mongoose.model("Workouts", UserWorkout);
module.exports = Workouts;

Technologies Used

HTML CSS Heroku Javascript GitHub
Express Node.js Mongoose Morgan

Author

B Tram Vu Linkedin LinkedIn GitHub GitHub

License

License: MIT

About


Languages

Language:JavaScript 66.5%Language:CSS 18.2%Language:HTML 15.3%