Skip to content

Commit 253bb53

Browse files
shantanu pandeyshantanu pandey
shantanu pandey
authored and
shantanu pandey
committed
Added the createHistory API
1 parent 9618481 commit 253bb53

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

controllers/api/v1/users_api.js

+37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const User = require("../../../models/user");
22
const jwt = require("jsonwebtoken");
33
const Food = require("../../../models/food");
4+
const History = require('../../../models/history');
45

56

67

@@ -30,6 +31,42 @@ module.exports.createSession = async function (req, res) {
3031
}
3132
};
3233

34+
35+
module.exports.createHistory = async function (req, res) {
36+
try {
37+
38+
let history = await History.create({
39+
date: req.body.date,
40+
caloriesgain: req.body.total,
41+
caloriesburn: req.body.burnout,
42+
user:req.body.id
43+
44+
});
45+
46+
47+
return res.json(200, {
48+
message: "History Created Successfully",
49+
50+
data: {
51+
52+
history:history,
53+
},
54+
success: true,
55+
});
56+
;
57+
}
58+
59+
catch (err) {
60+
console.log(err);
61+
62+
return res.json(500, {
63+
message: "Internal Server Error",
64+
});
65+
}
66+
};
67+
68+
69+
3370
module.exports.signUp = async function (req, res) {
3471
try {
3572
if (req.body.password != req.body.confirm_password) {

models/history.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const mongoose = require('mongoose');
2+
3+
4+
const historySchema = new mongoose.Schema({
5+
date: {
6+
type: String,
7+
required: true
8+
},
9+
caloriesgain: {
10+
type: Number,
11+
default:0
12+
},
13+
caloriesburn:{
14+
type:Number,
15+
default:0
16+
},
17+
user: {
18+
type: mongoose.Schema.Types.ObjectId,
19+
ref: 'User'
20+
}
21+
22+
23+
});
24+
25+
26+
const History = mongoose.model('History', historySchema);
27+
28+
module.exports = History;

routes/api/v1/users.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ router.post('/create-session',usersApi.createSession);
1010
router.post('/signup', usersApi.signUp);
1111
router.post('/edit', usersApi.editProfile);
1212
router.get('/search/:name', usersApi.searchUser);
13+
router.post('/createhistory',usersApi.createHistory);
1314

1415

1516
module.exports = router;

0 commit comments

Comments
 (0)