-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (66 loc) · 1.77 KB
/
index.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
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const cors = require("cors");
const User = require("./models/User");
const Calendar = require("./models/Calendar");
const Workout = require("./models/Workout");
const port = process.env.PORT || 4000;
const corsMiddleware = cors();
const jsonMiddleware = bodyParser.json();
app.use(corsMiddleware);
app.use(jsonMiddleware);
//require("./seeds");
app.listen(port, () => console.log(`Server listening on port ${port}...`));
//----------------USER ROUTES-------------------//
app.get("/user/:userId", (req, res, next) => {
User.findOne({ where: { id: req.params.userId } })
.then(user => {
res.json(user);
})
.catch(next);
});
app.post("/user", (req, res, next) => {
let data = req.body;
User.create(data)
.then(user => res.json(user))
.catch(next);
});
//----------------CALENDAR ROUTES-------------------//
app.get('/calendar', (req,res, next) => {
Calendar.findAll()
.then(calendar => {
res.json(calendar);
})
.catch(next)
})
// PUT route once a user has completed a workout
app.put("/:userId/calendar/:day", (req, res, next) => {
Calendar.findOne({
where: {
day: req.params.day
}
}).then(calendar => {
calendar.completedUsers.push(req.params.userId);
calendar
.update(
{
completedUsers: calendar.completedUsers
},
{
where: {
day: req.params.day
}
}
)
.then(calendar => res.json(calendar));
});
});
//----------------WORKOUT ROUTES-------------------//
app.get('/workout', (req,res, next) => {
Workout.findAll()
.then(workout => {
res.json(workout);
})
.catch(next)
})