-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
180 lines (153 loc) · 5.48 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const cors = require("cors");
const uuid = require("uuid");
const app = express();
app.use(cors());
app.set('view engine', 'ejs', 'cors');
app.use(bodyParser.json());
app.use(express.static("public"));
mongoose.connect("mongodb+srv://Group:<password>@api-cluster.dnb3xm9.mongodb.net/FitnessAppDb", {
useNewUrlParser: "true",
});
mongoose.connection.on("error", err => {
console.log("err", err)
});
mongoose.connection.on("connected", (err, res) => {
console.log("Database is connected");
});
const fitnessProgramesSchema = new mongoose.Schema({
ProgramId: { type: String, unique: true, required: true },
ProgramName: { type: String, maxlength: 64 },
Exercises: { type: Array },
});
const exercisesSchema = new mongoose.Schema({
ExerciseId: { type: String, unique: true, required: true },
ExerciseName: { type: String, maxlength: 128 },
ExerciseLength: { type: Number },
});
const fitnessProgramesModel = mongoose.model("fitnessProgrames", fitnessProgramesSchema);
const exercisesModel = mongoose.model("exercises", exercisesSchema);
app.get("/exercises/getExercises", async (req, res) => {
try {
const result = await exercisesModel.find();
if (result.length > 0) {
res.send(result);
} else {
res.status(404).send('Exercises Not found.');
}
} catch (err) {
console.log(err);
res.status(500).send('Server side error.');
}
});
app.post("/exercises/addExercise", async (req, res) => {
try {
const new_exercise = new exercisesModel({
'ExerciseId': uuid.v4(),
'ExerciseName': req.body.ExerciseName,
'ExerciseLength': req.body.ExerciseLength
});
const result = await new_exercise.save();
res.send(result);
} catch (err) {
console.log(err);
res.status(500).send('Server side error.');
}
});
app.delete("/exercises/deleteExercise", async (req, res) => {
const ExerciseId = req.body.ExerciseId;
try {
const result = await exercisesModel.deleteOne({ "ExerciseId": ExerciseId });
if (result.deletedCount === 0) {
return res.status(404).send('Exercise not found');
}
return res.status(200).send({
"status": true
});
} catch (err) {
console.log(err);
res.status(500).send('Server side error.');
}
});
app.get("/fitnessProgrames/getfitnessProgrames", async (req, res) => {
try {
const result = await fitnessProgramesModel.find();
let data = [];
if (result.length > 0) {
for (let i = 0; i < result.length; i++) {
let exercises = [];
for (let j = 0; j < result[i].Exercises.length; j++) {
// console.log(result[i].Exercises[j]);
const exercise = await exercisesModel.findOne({ "ExerciseId": result[i].Exercises[j] });
exercises.push(exercise);
// console.log(exercises);
};
// console.log(exercises);
data.push({
'ProgramId': result[i].ProgramId,
'ProgramName': result[i].ProgramName,
'Exercises': exercises
})
}
res.send(data);
} else {
res.status(404).send('fitnessProgrames Not found.');
}
} catch (err) {
console.log(err);
res.status(500).send('Server side error.');
}
});
app.post("/fitnessProgrames/addfitnessProgrames", async (req, res) => {
try {
const new_program = new fitnessProgramesModel({
'ProgramId': uuid.v4(),
'ProgramName': req.body.ProgramName,
'Exercises': req.body.Exercises
});
const result = await new_program.save();
res.send(result);
} catch (err) {
console.log(err);
res.status(500).send('Server side error.');
}
});
app.delete("/fitnessProgrames/deletefitnessProgrames", async (req, res) => {
const ProgramId = req.body.ProgramId;
try {
const result = await fitnessProgramesModel.deleteOne({ "ProgramId": ProgramId });
if (result.deletedCount === 0) {
return res.status(404).send('Program not found');
}
return res.status(200).send({
"status": true
});
} catch (err) {
console.log(err);
res.status(500).send('Server side error.');
}
});
app.patch("/fitnessProgrames/updatefitnessProgrames/:ProgramId", async (req, res) => {
try {
const ProgramId = req.params.ProgramId;
const patch = req.body;
const updatedPragrame = await fitnessProgramesModel.findOneAndUpdate(
{ 'ProgramId': ProgramId },
{ $set: patch },
{ new: true }
);
if (!updatedPragrame) {
return res.status(404).json({ error: 'Pragrame not found' });
}
res.status(200).json(updatedPragrame);
} catch (err) {
console.log(err);
res.status(500).send('Server side error.');
}
})
app.listen(process.env.PORT || 3000, function () {
console.log("Server started at http://localhost:3000/");
})