Skip to content

Commit

Permalink
Merge pull request #1 from WebMo21/develop
Browse files Browse the repository at this point in the history
Finished intial CRUD backend for all resources
  • Loading branch information
SaschaWebDev authored Jun 25, 2021
2 parents a61fdd5 + 5a42b1b commit 1e40a52
Show file tree
Hide file tree
Showing 38 changed files with 5,293 additions and 11,550 deletions.
53 changes: 30 additions & 23 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

# thunder-tests
thunderEnvironment.db
thunderEnvironment.db~
/thunder-tests/thunderEnvironment.db
/thunder-tests/thunderEnvironment.db~
42 changes: 21 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
MIT License

Copyright (c) 2021 WebMo21

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
MIT License
Copyright (c) 2021 Fitness Time
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
90 changes: 88 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,88 @@
# webmo21-backend
Alex ist supercool!
# Fitness Time Backend

## Development & Ideas

### Database Architecture

![Alt Text](./assets/img/db_architecture_v2.png)

**users** <br>
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.

**workouts** <br>
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.

**weekly_workout_plans** <br>
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:

```json
"day_1": [
{ "workout_id": "17",
"workout_completed": "no",
"workout_tracked_time": "0",
"workout_time_start": "13:00",
"workout_time_end": "13:30"
},
{ "workout_id": "24",
"workout_completed": "no",
"workout_tracked_time": "0",
"workout_time_start": "13:30",
"workout_time_end": "13:45"
}
],
"day_2": [
{ "workout_id": "13",
"workout_completed": "no",
"workout_tracked_time": "0",
"workout_time_start": "11:00",
"workout_time_end": "12:00"
},
],
"day_3": [],
"day_4": [],
"day_5": [],
"day_6": [],
"day_7": []
```

## Working with Knex.JS Query Builder

### Creating new migration files

```bash
npx knex migrate:make init --migrations-directory db/migrations
```

### Running new unmigrated migrations

```bash
npx knex migrate:latest --knexfile db/knexfile.js
```

### Running Seed Files

```bash
npx knex seed:run --knexfile db/knexfile.js
```

## Fixing SSL problems with Knex and Heroku PostgreSQL database

Configure the heroku app with the config var/environment variable:

```bash
PGSSLMODE=no-verify
```

````javascript
// in your knexfile.js
// Should come with install of pg
const parse = require("pg-connection-string").parse;
// Parse the environment variable into an object
const pgconfig = parse(process.env.DATABASE_URL);
// Add SSL setting to default environment variable
pgconfig.ssl = { rejectUnauthorized: false };
const db = knex({
client: "pg",
connection: pgconfig,
});```
````
201 changes: 201 additions & 0 deletions api/resources/users/users-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
const usersService = require("./users-service");

const getAllUsers = (req, res) =>
usersService
.find()
.then((users) =>
res.status(200).json({
users,
})
)
.catch((error) => {
console.log("Fehler beim Erhalten von allen Nutzern. ", error);
return res.status(500).json({
message: "Fehler beim Erhalten von allen Nutzern.",
});
});

const getUserById = (req, res) => {
const { id } = req.params;

if (id) {
usersService
.findById(id)
.then((user) => {
user
? res.status(200).json({
user,
})
: res.status(404).json({
message: "Dieser Nutzer wurde nicht gefunden.",
});
})
.catch((error) => {
console.log("Fehler beim Erhalten von diesem Nutzer. ", error);
return res.status(500).json({
message: "Fehler beim Erhalten von diesem Nutzer.",
});
});
} else {
return res.status(400).json({
message: "Fehler beim Erhalten von diesem Nutzer, da Angaben fehlen.",
});
}
};

const getUserByEmail = (req, res) => {
const { email } = req.params;

if (email) {
usersService
.findByEmail(email)
.then((user) => {
user
? res.status(200).json({
user,
})
: res.status(404).json({
message: "Dieser Nutzer wurde nicht gefunden.",
});
})
.catch((error) => {
console.log("Fehler beim Erhalten von diesem Nutzer. ", error);
return res.status(500).json({
message: "Fehler beim Erhalten von diesem Nutzer.",
});
});
} else {
return res.status(400).json({
message: "Fehler beim Erhalten von diesem Nutzer, da Angaben fehlen.",
});
}
};

const addUser = (req, res) => {
const userDTO = ({ name, email } = req.body);

if (email) {
usersService
.add(userDTO)
.then((newUser) =>
res.status(201).json({
id: newUser.id,
name: newUser.name,
email: newUser.email,
active: newUser.active,
isAdmin: newUser.is_admin,
})
)
.catch((error) => {
console.log("Fehler beim Hinzufügen von diesem Nutzer. ", error);
return res.status(500).json({
message: "Fehler beim Hinzufügen von diesem Nutzer.",
});
});
} else {
return res.status(400).json({
message: "Fehler beim Hinzufügen von diesem Nutzer, da Angaben fehlen.",
});
}
};

const updateUser = (req, res) => {
const updateUserDTO = ({ name, email, is_active } = req.body);

if (req.body.id && (name || email)) {
usersService
.update(req.body.id, updateUserDTO)
.then((successFlag) =>
successFlag > 0
? res.status(200).json({
message: "Die Nutzerinformationen wurden aktualisiert.",
})
: res.status(500).json({
message:
"Fehler bei der Aktualisierung der Nutzerinformationen, da Fehler in der Datenbank auftraten.",
})
)
.catch((error) => {
console.log(
"Fehler bei der Aktualisierung der Nutzerinformationen. ",
error
);
return res.status(500).json({
message: "Fehler bei der Aktualisierung der Nutzerinformationen.",
});
});
} else {
return res.status(400).json({
message:
"Fehler bei der Aktualisierung der Nutzerinformationen, da Angaben fehlen.",
});
}
};

const deleteUserById = (req, res) => {
const { id } = req.params;

if (id) {
usersService
.removeById(id)
.then((successFlag) =>
successFlag > 0
? res.status(200).json({
message: "Der Nutzer wurde als inaktiv vermerkt.",
})
: res.status(500).json({
message:
"Der Nutzer konnte nicht gelöscht werden, da Fehler in der Datenbank auftraten.",
})
)
.catch((error) => {
console.log("Fehler beim Löschen des Nutzers. ", error);
return res.status(500).json({
message: "Fehler beim Löschen des Nutzers.",
});
});
} else {
return res.status(400).json({
message: "Fehler beim Löschen des Nutzers, da Angaben fehlen.",
});
}
};

const deleteUserByEmail = (req, res) => {
const { email } = req.params;

if (email) {
usersService
.removeByEmail(email)
.then((successFlag) =>
successFlag > 0
? res.status(200).json({
message: "Der Nutzer wurde als inaktiv vermerkt.",
})
: res.status(500).json({
message:
"Der Nutzer konnte nicht gelöscht werden, da Fehler in der Datenbank auftraten.",
})
)
.catch((error) => {
console.log("Fehler beim Löschen des Nutzers. ", error);
return res.status(500).json({
message: "Fehler beim Löschen des Nutzers.",
});
});
} else {
return res.status(400).json({
message: "Fehler beim Löschen des Nutzers, da Angaben fehlen.",
});
}
};

module.exports = {
getAllUsers,
getUserById,
getUserByEmail,
addUser,
updateUser,
deleteUserById,
deleteUserByEmail,
};
Loading

0 comments on commit 1e40a52

Please sign in to comment.