Skip to content

Commit

Permalink
feat: start adding validation, add documentation for migrating
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynndp committed Feb 6, 2022
1 parent 8ffa814 commit 28c0133
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 67 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Name of this microservice

Description of this microservice

# Development toolkit
Expand All @@ -15,7 +15,7 @@ This repository was created from Greenstand's template for microservice projects


# Getting Started

## Project Setup

Open terminal and navigate to a folder to install this project:
Expand All @@ -24,7 +24,7 @@ Open terminal and navigate to a folder to install this project:
git clone https://github.com/Greenstand/treetracker-repository-name.git
```
Install all necessary dependencies:
Install all necessary dependencies:

```
npm install
Expand All @@ -34,21 +34,22 @@ npm install

### Database Setup

This repository using db-migrate to manage database migrations for its schema.
This repository using knex to manage database migrations for its schema.

To do migrations:
```
cd database/
db-migrate --env dev up
knex migrate:latest
```

If you have not installed db-migrate globally, you can run:

To rollback migrations and empty database:
```
knex migrate:rollback
```
To seed tables:
```
cd database/
../node_modules/db-migrate/bin/db-migrate --env dev up
knex seed:run
```

Documentation for db-migrate: https://db-migrate.readthedocs.io/en/latest/
Documentation for knex migrations: https://knexjs.org/#Migrations

# Architecture of this project

Expand All @@ -65,15 +66,15 @@ The Express-routers work like the controller role in MVC, they receive the reque

* **Service layer**

Both service layer and model layer are where all the business logic is located. Comparing to the Model , `service` object don't have state (stateless).
Both service layer and model layer are where all the business logic is located. Comparing to the Model , `service` object don't have state (stateless).

Please put business logic code into service object when it is hard to put them into the `Model` object.

Because we didn't use Factory or dependency injection to create object, so service layer also can be used as Factory to create `model` object.

* **Model layer**

The business model, major business logic is here. They are real object, in the perspective of object oriented programming: they have states, they have the method to do stuff.
The business model, major business logic is here. They are real object, in the perspective of object oriented programming: they have states, they have the method to do stuff.

There are more discussion about this, check below selection.

Expand Down
12 changes: 0 additions & 12 deletions database/database.json.example

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"test-watch": "NODE_ENV=test NODE_LOG_LEVEL=info mocha -r dotenv/config dotenv_config_path=.env.test --timeout 10000 -w -b --ignore './server/repositories/**/*.spec.js' './server/setup.js' './server/**/*.spec.js' './__tests__/seed-spec-example.spec.js' './__tests__/supertest-example.spec.js'",
"test-watch-debug": "NODE_ENV=test NODE_LOG_LEVEL=debug mocha -r dotenv/config dotenv_config_path=.env.test --timeout 10000 -w -b --ignore './server/repositories/**/*.spec.js' './server/setup.js' './server/**/*.spec.js' './__tests__/seed-spec-example.spec.js' './__tests__/supertest-example.spec.js'",
"prettier-fix": "prettier ./ --write",
"db-migrate-ci": "cd database; db-migrate up",
"migrate": "knex migrate:latest",
"rollback": "knex migrate:rollback",
"seed": "knex seed:run",
Expand Down
80 changes: 40 additions & 40 deletions server/routes.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
const router = require('express').Router();
const uuid = require('uuid');
const log = require('loglevel');
// const uuid = require('uuid');
// const log = require('loglevel');

const validateRequest = (req, res, next) => {
console.log('QUERY -------> ', req.query);
// const validateRequest = (req, res, next) => {
// console.log('QUERY -------> ', req.query);

if(req.query.limit && !Number.isInteger(+req.query.limit)) {
// throw new Error('"limit" must be an integer');
next({ status: 422, message: '"limit" must be an integer' });
}
if (req.query.limit && +req.query.limit < 1) {
next({ status: 422, message: '"limit" must be greater than 0' });
}
if (req.query.limit && +req.query.limit > 100) {
next({ status: 422, message: '"limit" must be less than 101' });
}
if (req.query.offset && !Number.isInteger(+req.query.offset)) {
next({ status: 422, message: '"offset" must be an integer' });
}
if (req.query.offset && +req.query.offset < 0) {
next({ status: 422, message: '"offset" must be greater than -1' });
}
if (req.query.id && !uuid.validate(req.query.id)) {
next({ status: 422, message: '"id" must be a valid GUID' });
}
if (req.query.owner_id && !uuid.validate(req.query.owner_id)) {
next({ status: 422, message: '"id" must be a valid GUID' });
}
if(req.query.organization_id && !Number.isInteger(+req.query.organization_id)) {
next({ status: 422, message: '"organization_id" must be an integer' });
}
// if(req.query.limit && !Number.isInteger(+req.query.limit)) {
// // throw new Error('"limit" must be an integer');
// next({ status: 422, message: '"limit" must be an integer' });
// }
// if (req.query.limit && +req.query.limit < 1) {
// next({ status: 422, message: '"limit" must be greater than 0' });
// }
// if (req.query.limit && +req.query.limit > 100) {
// next({ status: 422, message: '"limit" must be less than 101' });
// }
// if (req.query.offset && !Number.isInteger(+req.query.offset)) {
// next({ status: 422, message: '"offset" must be an integer' });
// }
// if (req.query.offset && +req.query.offset < 0) {
// next({ status: 422, message: '"offset" must be greater than -1' });
// }
// if (req.query.id && !uuid.validate(req.query.id)) {
// next({ status: 422, message: '"id" must be a valid GUID' });
// }
// if (req.query.owner_id && !uuid.validate(req.query.owner_id)) {
// next({ status: 422, message: '"id" must be a valid GUID' });
// }
// if(req.query.organization_id && !Number.isInteger(+req.query.organization_id)) {
// next({ status: 422, message: '"organization_id" must be an integer' });
// }

next();
};
// next();
// };

const {
stakeholderGetAllById,
Expand All @@ -47,20 +47,20 @@ const { handlerWrapper } = require('./utils/utils');

router
.route('/stakeholders/relations/:id')
.get(validateRequest, handlerWrapper(stakeholderGetRelations))
.post(validateRequest, handlerWrapper(stakeholderCreateRelation))
.delete(validateRequest, handlerWrapper(stakeholderDeleteRelation));
.get(handlerWrapper(stakeholderGetRelations))
.post(handlerWrapper(stakeholderCreateRelation))
.delete(handlerWrapper(stakeholderDeleteRelation));

router
.route('/stakeholders/:id')
.get(validateRequest, handlerWrapper(stakeholderGetAllById))
.post(validateRequest, handlerWrapper(stakeholderCreate))
.patch(validateRequest, handlerWrapper(stakeholderUpdate));
.get(handlerWrapper(stakeholderGetAllById))
.post(handlerWrapper(stakeholderCreate))
.patch(handlerWrapper(stakeholderUpdate));

router
.route('/stakeholders')
.get(validateRequest, handlerWrapper(stakeholderGetAll))
.post(validateRequest, handlerWrapper(stakeholderCreate))
.patch(validateRequest, handlerWrapper(stakeholderUpdate));
.get(handlerWrapper(stakeholderGetAll))
.post(handlerWrapper(stakeholderCreate))
.patch(handlerWrapper(stakeholderUpdate));

module.exports = router;

0 comments on commit 28c0133

Please sign in to comment.