mirror of
https://github.com/codecrafters-io/build-your-own-x.git
synced 2026-01-13 16:56:59 +00:00
Merge f27f6c28fc into c56186ac2d
This commit is contained in:
@@ -37,3 +37,4 @@ Thanks for your contribution! If you're submitting a tutorial, please ensure it
|
|||||||
* [ ] Web Search Engine
|
* [ ] Web Search Engine
|
||||||
* [ ] Web Server
|
* [ ] Web Server
|
||||||
* [ ] Uncategorized
|
* [ ] Uncategorized
|
||||||
|
* [ ] Fitness App
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ It's a great way to learn.
|
|||||||
* [Web Browser](#build-your-own-web-browser)
|
* [Web Browser](#build-your-own-web-browser)
|
||||||
* [Web Server](#build-your-own-web-server)
|
* [Web Server](#build-your-own-web-server)
|
||||||
* [Uncategorized](#uncategorized)
|
* [Uncategorized](#uncategorized)
|
||||||
|
* [Fitness App](#build-your-own-fitness-app)
|
||||||
|
|
||||||
## Tutorials
|
## Tutorials
|
||||||
|
|
||||||
@@ -409,6 +410,10 @@ It's a great way to learn.
|
|||||||
* [**Python**: _Implementing a RESTful Web API with Python & Flask_](http://blog.luisrei.com/articles/flaskrest.html)
|
* [**Python**: _Implementing a RESTful Web API with Python & Flask_](http://blog.luisrei.com/articles/flaskrest.html)
|
||||||
* [**Ruby**: _Building a simple websockets server from scratch in Ruby_](http://blog.honeybadger.io/building-a-simple-websockets-server-from-scratch-in-ruby/)
|
* [**Ruby**: _Building a simple websockets server from scratch in Ruby_](http://blog.honeybadger.io/building-a-simple-websockets-server-from-scratch-in-ruby/)
|
||||||
|
|
||||||
|
#### Build your own `Fitness App`
|
||||||
|
|
||||||
|
* [**JavaScript**: _Build a simple fitness app similar to Fitbud_](tutorials/fitness-app.md)
|
||||||
|
|
||||||
#### Uncategorized
|
#### Uncategorized
|
||||||
|
|
||||||
* [**(any)**: _From NAND to Tetris: Building a Modern Computer From First Principles_](http://nand2tetris.org/)
|
* [**(any)**: _From NAND to Tetris: Building a Modern Computer From First Principles_](http://nand2tetris.org/)
|
||||||
|
|||||||
195
tutorials/fitness-app.md
Normal file
195
tutorials/fitness-app.md
Normal file
@@ -0,0 +1,195 @@
|
|||||||
|
# Build a Simple Fitness App
|
||||||
|
|
||||||
|
In this tutorial, we will build a simple fitness app similar to Fitbud. The app will allow users to track their workouts, set goals, and monitor their progress. We will use JavaScript and a few other technologies to create this app.
|
||||||
|
|
||||||
|
## Step 1: Setting Up the Project
|
||||||
|
|
||||||
|
First, let's set up our project. Create a new directory for your project and initialize a new Node.js project:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mkdir fitness-app
|
||||||
|
cd fitness-app
|
||||||
|
npm init -y
|
||||||
|
```
|
||||||
|
|
||||||
|
Next, install the necessary dependencies:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install express body-parser mongoose
|
||||||
|
```
|
||||||
|
|
||||||
|
## Step 2: Creating the Server
|
||||||
|
|
||||||
|
We will use Express to create our server. Create a new file called `server.js` and add the following code:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const express = require('express');
|
||||||
|
const bodyParser = require('body-parser');
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
const port = 3000;
|
||||||
|
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
|
mongoose.connect('mongodb://localhost/fitness-app', { useNewUrlParser: true, useUnifiedTopology: true });
|
||||||
|
|
||||||
|
app.listen(port, () => {
|
||||||
|
console.log(`Server is running on port ${port}`);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Step 3: Creating the User Model
|
||||||
|
|
||||||
|
We will use Mongoose to create our user model. Create a new file called `models/User.js` and add the following code:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
|
const userSchema = new mongoose.Schema({
|
||||||
|
username: { type: String, required: true, unique: true },
|
||||||
|
password: { type: String, required: true },
|
||||||
|
workouts: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Workout' }]
|
||||||
|
});
|
||||||
|
|
||||||
|
const User = mongoose.model('User', userSchema);
|
||||||
|
|
||||||
|
module.exports = User;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Step 4: Creating the Workout Model
|
||||||
|
|
||||||
|
Next, create a new file called `models/Workout.js` and add the following code:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
|
const workoutSchema = new mongoose.Schema({
|
||||||
|
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
|
||||||
|
date: { type: Date, required: true },
|
||||||
|
exercises: [{
|
||||||
|
name: { type: String, required: true },
|
||||||
|
sets: { type: Number, required: true },
|
||||||
|
reps: { type: Number, required: true },
|
||||||
|
weight: { type: Number, required: true }
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
|
||||||
|
const Workout = mongoose.model('Workout', workoutSchema);
|
||||||
|
|
||||||
|
module.exports = Workout;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Step 5: Creating the User Routes
|
||||||
|
|
||||||
|
Create a new file called `routes/user.js` and add the following code:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
const User = require('../models/User');
|
||||||
|
|
||||||
|
router.post('/register', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const user = new User(req.body);
|
||||||
|
await user.save();
|
||||||
|
res.status(201).send(user);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(400).send(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post('/login', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const user = await User.findOne({ username: req.body.username, password: req.body.password });
|
||||||
|
if (!user) {
|
||||||
|
return res.status(401).send({ error: 'Invalid credentials' });
|
||||||
|
}
|
||||||
|
res.send(user);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(400).send(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Step 6: Creating the Workout Routes
|
||||||
|
|
||||||
|
Create a new file called `routes/workout.js` and add the following code:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
const Workout = require('../models/Workout');
|
||||||
|
|
||||||
|
router.post('/', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const workout = new Workout(req.body);
|
||||||
|
await workout.save();
|
||||||
|
res.status(201).send(workout);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(400).send(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get('/:userId', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const workouts = await Workout.find({ user: req.params.userId });
|
||||||
|
res.send(workouts);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(400).send(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Step 7: Integrating the Routes
|
||||||
|
|
||||||
|
Update the `server.js` file to include the user and workout routes:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const express = require('express');
|
||||||
|
const bodyParser = require('body-parser');
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
const userRoutes = require('./routes/user');
|
||||||
|
const workoutRoutes = require('./routes/workout');
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
const port = 3000;
|
||||||
|
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
|
mongoose.connect('mongodb://localhost/fitness-app', { useNewUrlParser: true, useUnifiedTopology: true });
|
||||||
|
|
||||||
|
app.use('/users', userRoutes);
|
||||||
|
app.use('/workouts', workoutRoutes);
|
||||||
|
|
||||||
|
app.listen(port, () => {
|
||||||
|
console.log(`Server is running on port ${port}`);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Step 8: Testing the API
|
||||||
|
|
||||||
|
You can use a tool like Postman to test the API. Here are some example requests:
|
||||||
|
|
||||||
|
- Register a new user:
|
||||||
|
- POST http://localhost:3000/users/register
|
||||||
|
- Body: `{ "username": "testuser", "password": "password123" }`
|
||||||
|
|
||||||
|
- Login a user:
|
||||||
|
- POST http://localhost:3000/users/login
|
||||||
|
- Body: `{ "username": "testuser", "password": "password123" }`
|
||||||
|
|
||||||
|
- Create a new workout:
|
||||||
|
- POST http://localhost:3000/workouts
|
||||||
|
- Body: `{ "user": "userId", "date": "2021-01-01", "exercises": [{ "name": "Squat", "sets": 3, "reps": 10, "weight": 100 }] }`
|
||||||
|
|
||||||
|
- Get workouts for a user:
|
||||||
|
- GET http://localhost:3000/workouts/userId
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
Congratulations! You have built a simple fitness app similar to Fitbud. You can now extend this app by adding more features such as goal tracking, progress monitoring, and more. Happy coding!
|
||||||
Reference in New Issue
Block a user