Skip to content

Commit 1e40a52

Browse files
authored
Merge pull request #1 from WebMo21/develop
Finished intial CRUD backend for all resources
2 parents a61fdd5 + 5a42b1b commit 1e40a52

38 files changed

+5293
-11550
lines changed

.gitignore

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1-
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2-
3-
# dependencies
4-
/node_modules
5-
/.pnp
6-
.pnp.js
7-
8-
# testing
9-
/coverage
10-
11-
# production
12-
/build
13-
14-
# misc
15-
.DS_Store
16-
.env.local
17-
.env.development.local
18-
.env.test.local
19-
.env.production.local
20-
21-
npm-debug.log*
22-
yarn-debug.log*
23-
yarn-error.log*
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env
17+
.env.local
18+
.env.development.local
19+
.env.test.local
20+
.env.production.local
21+
22+
npm-debug.log*
23+
yarn-debug.log*
24+
yarn-error.log*
25+
26+
# thunder-tests
27+
thunderEnvironment.db
28+
thunderEnvironment.db~
29+
/thunder-tests/thunderEnvironment.db
30+
/thunder-tests/thunderEnvironment.db~

LICENSE

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
MIT License
2-
3-
Copyright (c) 2021 WebMo21
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
1+
MIT License
2+
3+
Copyright (c) 2021 Fitness Time
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,88 @@
1-
# webmo21-backend
2-
Alex ist supercool!
1+
# Fitness Time Backend
2+
3+
## Development & Ideas
4+
5+
### Database Architecture
6+
7+
![Alt Text](./assets/img/db_architecture_v2.png)
8+
9+
**users** <br>
10+
Since the authentication process is handled through Next-Auth.js with magic links for social media login and email magic links there is not a lot of data gathered from the users, only email and if social media is used also the name. On top of that active declares if a user is logically deleted since real deletion would not be compliant with regulatory rules and is_admin signals the authorization of a given user.
11+
12+
**workouts** <br>
13+
Users should be able to create custom workouts but also can select already created example workouts for themself. Only workout attributes and a foreign key to the user owning them is stored.
14+
15+
**weekly_workout_plans** <br>
16+
During the design architecture some discussion about granularity came up but consensus was that each workoutplan is consisting of 7 possible workout days, a name, a year and which calender week it represents. In the backend users can be searched for all of their weekly_workout_plans and them sent to the frontend to be displayed in a calender view accordingly to their week. For each day there will be a json containing information about the individual workouts like tracking their completing, the real invested time, the scheduled time each of them should happen for the day and the id of the workout similar to this example:
17+
18+
```json
19+
"day_1": [
20+
{ "workout_id": "17",
21+
"workout_completed": "no",
22+
"workout_tracked_time": "0",
23+
"workout_time_start": "13:00",
24+
"workout_time_end": "13:30"
25+
},
26+
{ "workout_id": "24",
27+
"workout_completed": "no",
28+
"workout_tracked_time": "0",
29+
"workout_time_start": "13:30",
30+
"workout_time_end": "13:45"
31+
}
32+
],
33+
"day_2": [
34+
{ "workout_id": "13",
35+
"workout_completed": "no",
36+
"workout_tracked_time": "0",
37+
"workout_time_start": "11:00",
38+
"workout_time_end": "12:00"
39+
},
40+
],
41+
"day_3": [],
42+
"day_4": [],
43+
"day_5": [],
44+
"day_6": [],
45+
"day_7": []
46+
```
47+
48+
## Working with Knex.JS Query Builder
49+
50+
### Creating new migration files
51+
52+
```bash
53+
npx knex migrate:make init --migrations-directory db/migrations
54+
```
55+
56+
### Running new unmigrated migrations
57+
58+
```bash
59+
npx knex migrate:latest --knexfile db/knexfile.js
60+
```
61+
62+
### Running Seed Files
63+
64+
```bash
65+
npx knex seed:run --knexfile db/knexfile.js
66+
```
67+
68+
## Fixing SSL problems with Knex and Heroku PostgreSQL database
69+
70+
Configure the heroku app with the config var/environment variable:
71+
72+
```bash
73+
PGSSLMODE=no-verify
74+
```
75+
76+
````javascript
77+
// in your knexfile.js
78+
// Should come with install of pg
79+
const parse = require("pg-connection-string").parse;
80+
// Parse the environment variable into an object
81+
const pgconfig = parse(process.env.DATABASE_URL);
82+
// Add SSL setting to default environment variable
83+
pgconfig.ssl = { rejectUnauthorized: false };
84+
const db = knex({
85+
client: "pg",
86+
connection: pgconfig,
87+
});```
88+
````
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
const usersService = require("./users-service");
2+
3+
const getAllUsers = (req, res) =>
4+
usersService
5+
.find()
6+
.then((users) =>
7+
res.status(200).json({
8+
users,
9+
})
10+
)
11+
.catch((error) => {
12+
console.log("Fehler beim Erhalten von allen Nutzern. ", error);
13+
return res.status(500).json({
14+
message: "Fehler beim Erhalten von allen Nutzern.",
15+
});
16+
});
17+
18+
const getUserById = (req, res) => {
19+
const { id } = req.params;
20+
21+
if (id) {
22+
usersService
23+
.findById(id)
24+
.then((user) => {
25+
user
26+
? res.status(200).json({
27+
user,
28+
})
29+
: res.status(404).json({
30+
message: "Dieser Nutzer wurde nicht gefunden.",
31+
});
32+
})
33+
.catch((error) => {
34+
console.log("Fehler beim Erhalten von diesem Nutzer. ", error);
35+
return res.status(500).json({
36+
message: "Fehler beim Erhalten von diesem Nutzer.",
37+
});
38+
});
39+
} else {
40+
return res.status(400).json({
41+
message: "Fehler beim Erhalten von diesem Nutzer, da Angaben fehlen.",
42+
});
43+
}
44+
};
45+
46+
const getUserByEmail = (req, res) => {
47+
const { email } = req.params;
48+
49+
if (email) {
50+
usersService
51+
.findByEmail(email)
52+
.then((user) => {
53+
user
54+
? res.status(200).json({
55+
user,
56+
})
57+
: res.status(404).json({
58+
message: "Dieser Nutzer wurde nicht gefunden.",
59+
});
60+
})
61+
.catch((error) => {
62+
console.log("Fehler beim Erhalten von diesem Nutzer. ", error);
63+
return res.status(500).json({
64+
message: "Fehler beim Erhalten von diesem Nutzer.",
65+
});
66+
});
67+
} else {
68+
return res.status(400).json({
69+
message: "Fehler beim Erhalten von diesem Nutzer, da Angaben fehlen.",
70+
});
71+
}
72+
};
73+
74+
const addUser = (req, res) => {
75+
const userDTO = ({ name, email } = req.body);
76+
77+
if (email) {
78+
usersService
79+
.add(userDTO)
80+
.then((newUser) =>
81+
res.status(201).json({
82+
id: newUser.id,
83+
name: newUser.name,
84+
email: newUser.email,
85+
active: newUser.active,
86+
isAdmin: newUser.is_admin,
87+
})
88+
)
89+
.catch((error) => {
90+
console.log("Fehler beim Hinzufügen von diesem Nutzer. ", error);
91+
return res.status(500).json({
92+
message: "Fehler beim Hinzufügen von diesem Nutzer.",
93+
});
94+
});
95+
} else {
96+
return res.status(400).json({
97+
message: "Fehler beim Hinzufügen von diesem Nutzer, da Angaben fehlen.",
98+
});
99+
}
100+
};
101+
102+
const updateUser = (req, res) => {
103+
const updateUserDTO = ({ name, email, is_active } = req.body);
104+
105+
if (req.body.id && (name || email)) {
106+
usersService
107+
.update(req.body.id, updateUserDTO)
108+
.then((successFlag) =>
109+
successFlag > 0
110+
? res.status(200).json({
111+
message: "Die Nutzerinformationen wurden aktualisiert.",
112+
})
113+
: res.status(500).json({
114+
message:
115+
"Fehler bei der Aktualisierung der Nutzerinformationen, da Fehler in der Datenbank auftraten.",
116+
})
117+
)
118+
.catch((error) => {
119+
console.log(
120+
"Fehler bei der Aktualisierung der Nutzerinformationen. ",
121+
error
122+
);
123+
return res.status(500).json({
124+
message: "Fehler bei der Aktualisierung der Nutzerinformationen.",
125+
});
126+
});
127+
} else {
128+
return res.status(400).json({
129+
message:
130+
"Fehler bei der Aktualisierung der Nutzerinformationen, da Angaben fehlen.",
131+
});
132+
}
133+
};
134+
135+
const deleteUserById = (req, res) => {
136+
const { id } = req.params;
137+
138+
if (id) {
139+
usersService
140+
.removeById(id)
141+
.then((successFlag) =>
142+
successFlag > 0
143+
? res.status(200).json({
144+
message: "Der Nutzer wurde als inaktiv vermerkt.",
145+
})
146+
: res.status(500).json({
147+
message:
148+
"Der Nutzer konnte nicht gelöscht werden, da Fehler in der Datenbank auftraten.",
149+
})
150+
)
151+
.catch((error) => {
152+
console.log("Fehler beim Löschen des Nutzers. ", error);
153+
return res.status(500).json({
154+
message: "Fehler beim Löschen des Nutzers.",
155+
});
156+
});
157+
} else {
158+
return res.status(400).json({
159+
message: "Fehler beim Löschen des Nutzers, da Angaben fehlen.",
160+
});
161+
}
162+
};
163+
164+
const deleteUserByEmail = (req, res) => {
165+
const { email } = req.params;
166+
167+
if (email) {
168+
usersService
169+
.removeByEmail(email)
170+
.then((successFlag) =>
171+
successFlag > 0
172+
? res.status(200).json({
173+
message: "Der Nutzer wurde als inaktiv vermerkt.",
174+
})
175+
: res.status(500).json({
176+
message:
177+
"Der Nutzer konnte nicht gelöscht werden, da Fehler in der Datenbank auftraten.",
178+
})
179+
)
180+
.catch((error) => {
181+
console.log("Fehler beim Löschen des Nutzers. ", error);
182+
return res.status(500).json({
183+
message: "Fehler beim Löschen des Nutzers.",
184+
});
185+
});
186+
} else {
187+
return res.status(400).json({
188+
message: "Fehler beim Löschen des Nutzers, da Angaben fehlen.",
189+
});
190+
}
191+
};
192+
193+
module.exports = {
194+
getAllUsers,
195+
getUserById,
196+
getUserByEmail,
197+
addUser,
198+
updateUser,
199+
deleteUserById,
200+
deleteUserByEmail,
201+
};

0 commit comments

Comments
 (0)