Skip to content

Commit aaa063c

Browse files
committed
procfile added
0 parents  commit aaa063c

13 files changed

+2629
-0
lines changed

.gitattributes

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Tell Linguist to exclude HTML files to help Replit language detection.
2+
*.html linguist-vendored
3+
*.css linguist-vendored

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
node_modules

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.replit

.replit

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
run = "npm start"
2+
entrypoint = "index.js"
3+
4+
[packager]
5+
language = "nodejs"
6+
7+
[packager.features]
8+
packageSearch = true
9+
guessImports = true
10+
enabledForHosting = false

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Exercise Tracker
2+
3+
This is the boilerplate for the Exercise Tracker project. Instructions for building your project can be found at https://www.freecodecamp.org/learn/apis-and-microservices/apis-and-microservices-projects/exercise-tracker

controller/logger.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// --- Connection details --- //
2+
3+
async function Logger(req, res, next) {
4+
let date = new Date();
5+
6+
console.log(
7+
"User> IP:" +
8+
req.ip +
9+
" PATH:" +
10+
req.path +
11+
" Date: " +
12+
date.toDateString()
13+
);
14+
next();
15+
}
16+
17+
module.exports = Logger;

index.js

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
const express = require("express");
2+
const mongoose = require("mongoose");
3+
const bodyParser = require("body-parser");
4+
const Logger = require("./controller/logger");
5+
const app = express();
6+
const cors = require("cors");
7+
require("dotenv").config();
8+
const { USER, LOG } = require("./models/TrackerDatabaseModel");
9+
const { response } = require("express");
10+
// --- Setting Up PORT && MONGOOSE CONFIG --- //
11+
const MONGO_URI = process.env.MONGO_URI;
12+
const PORT = process.env.PORT;
13+
app.use(Logger);
14+
~app.use(cors());
15+
app.use(bodyParser.urlencoded({ extended: false }));
16+
app.use(express.json());
17+
18+
app.use(express.static("./public"));
19+
20+
// --- Middlewares --- //
21+
// --- database connection --- //
22+
async function ConnectDB() {
23+
return await mongoose.connect(MONGO_URI, {
24+
dbName: "db",
25+
useNewUrlParser: true,
26+
serverSelectionTimeoutMS: 5000,
27+
});
28+
}
29+
30+
const connection = mongoose.connection;
31+
connection.on("error", (error) => {
32+
console.log(error);
33+
});
34+
connection.on("connecting", (data) => {
35+
console.log("Connecting to mongoose database......");
36+
});
37+
connection.on("open", (data) => {
38+
console.log("Connection to mongoose database established successfully.");
39+
});
40+
// --- Home Route Api * Index.html File --- //
41+
app.get("/", (req, res) => {
42+
res.sendFile(__dirname + "/views/index.html");
43+
});
44+
45+
// --- Get All Users * Array Result --- //
46+
app.get("/api/users", async (req, res) => {
47+
const users = await USER.find();
48+
res.json(users);
49+
});
50+
// --- Create New User Route Api --- //
51+
app.post("/api/users", async (req, res) => {
52+
const data = req.body;
53+
const username = data?.username;
54+
// --- Username Nan? --- //
55+
if (!username) {
56+
console.log("Username not provided");
57+
return res.json({
58+
error: "Username must be provided!",
59+
});
60+
}
61+
// --- New User Creation --- //
62+
console.log("user: " + username + " requested admission.");
63+
// --- New User Options --- //
64+
const _user = new USER({
65+
username: username,
66+
});
67+
// --- New User Created --- //
68+
await _user.save();
69+
console.log(_user);
70+
res.json({
71+
username: _user.username,
72+
_id: _user._id,
73+
});
74+
});
75+
76+
app.post("/api/users/:_id/exercises", async (req, res) => {
77+
console.log(req.body);
78+
// --- User id To Insert Data--- //
79+
const data = req.body;
80+
const { _id } = req.params;
81+
const description = data?.description;
82+
const duration = data?.duration;
83+
const date = data?.date;
84+
if (!_id) {
85+
return res.send("please provide user id");
86+
}
87+
if (!description || !duration) {
88+
return res.send("please provide description and duration");
89+
}
90+
// --- User Validation --- //
91+
const _user = await USER.findById(_id);
92+
console.log(_user);
93+
if (!_user) {
94+
// --- No User Found --- //
95+
console.log(`No User Found! with id:${_id}`);
96+
return res.send("No user found!");
97+
}
98+
// --- User Found Creating Log --- //
99+
let _userLog = LOG({
100+
createdBy: _user._id,
101+
description: description,
102+
duration: Number(duration),
103+
});
104+
if (date) {
105+
_userLog.date = date;
106+
}
107+
await _userLog.save();
108+
let response = {
109+
username: _user.username,
110+
_id: _user._id,
111+
description: _userLog.description,
112+
duration: _userLog.duration,
113+
date: _userLog.date,
114+
};
115+
// console.log(response);
116+
res.json(response);
117+
});
118+
// --- Get User All Excercise Logs || Route Api --- //
119+
app.get("/api/users/:_id/logs", async (req, res) => {
120+
// --- Get User ID From req.params (:_id) --- //
121+
const { _id } = req.params;
122+
console.log(_id);
123+
let _user = await USER.findById(_id);
124+
if (!_user) {
125+
console.log("No User Found!");
126+
return res.send("No User Found");
127+
}
128+
let _userLogs = await LOG.find({ createdBy: _user._id })
129+
.select("-createdBy")
130+
.select("-__v")
131+
.select("-_id");
132+
let logArray = [];
133+
logArray.push(_userLogs);
134+
// --- Generating Response --- //
135+
let _response = {
136+
username: _user.username,
137+
_id: _user._id,
138+
count: _userLogs.length,
139+
log: _userLogs,
140+
};
141+
142+
res.json(_response);
143+
});
144+
145+
// --- Server Starts Here --- //
146+
147+
async function START_SERVER() {
148+
await ConnectDB();
149+
//--- Server Listening --- //
150+
const listener = await app.listen(process.env.PORT || 3000, () => {
151+
console.log("Your app is listening on port " + listener.address().port);
152+
});
153+
}
154+
//--- Starting Server && Mongoo Connection --- //
155+
START_SERVER();

models/TrackerDatabaseModel.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const mongoose = require("mongoose");
2+
3+
// Log schema
4+
const LogSchema = new mongoose.Schema({
5+
description: {
6+
type: String,
7+
required: [true, "please provide a description!"],
8+
},
9+
duration: {
10+
type: Number,
11+
required: [true, "please provide a duration of exercise!"],
12+
},
13+
date: {
14+
type: String,
15+
default: new Date().toDateString(),
16+
},
17+
createdBy:{
18+
type:mongoose.Schema.Types.ObjectId,
19+
required:[true, "please provide user id"]
20+
}
21+
});
22+
23+
// userSchema
24+
const UserSchema = new mongoose.Schema({
25+
username: {
26+
type: String,
27+
required: [true, "please provide a username!"],
28+
},
29+
});
30+
31+
const LOG = mongoose.model("Logs", LogSchema);
32+
const USER = mongoose.model("Users", UserSchema);
33+
34+
module.exports = { USER, LOG };

0 commit comments

Comments
 (0)