Skip to content

Commit

Permalink
generate user service
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienBtr committed Nov 7, 2020
1 parent de393c3 commit 53333c6
Show file tree
Hide file tree
Showing 31 changed files with 829 additions and 2 deletions.
2 changes: 1 addition & 1 deletion generator/generators/app/usedPorts.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"used":[3500,3501,3502,3503,3504,3505]}
{"used":[3500,3501,3502,3503,3504,3505,3506]}
1 change: 1 addition & 0 deletions services/user/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
11 changes: 11 additions & 0 deletions services/user/.env.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SERVER_PORT=3500

PRISMA_MANAGEMENT_API_SECRET=wsflk897dh%£

LOGGING_DRIVER=json-file

DB_HOST=
DB_PORT=
DB_NAME=
DB_USER=
DB_PASSWORD=
1 change: 1 addition & 0 deletions services/user/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generated
10 changes: 10 additions & 0 deletions services/user/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"env": {
"es6": true,
"node": true
},
"extends": [
"airbnb-base"
],
"rules": {}
}
3 changes: 3 additions & 0 deletions services/user/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
node_modules
generated
8 changes: 8 additions & 0 deletions services/user/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:13
RUN curl -o- -L https://yarnpkg.com/install.sh | bash
RUN yarn global add prisma

WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install
COPY . .
24 changes: 24 additions & 0 deletions services/user/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# User

Generated Express.js microservice using our [generator](../../generator).

To run this service use the [launcher script](../launcher.sh) as described in the [readme](../README.md).

# Description

Service to manage users

# Install

* Run `yarn install`
* Install prisma CLI: https://v1.prisma.io/docs/1.34/prisma-cli-and-configuration/using-the-prisma-cli-alx4/
* Run `prisma generate`

These steps are necessary to have autocompletion.
Now you can start coding but make sure to not edit code that will be overriden by the generator. In case you want to do any change in the service contracts, you need to do it through the [specification](../specification).

# Documentation

A generated swagger documentation can be found at the route `/documentation` through the api-gateway.

You can also find the postman collection file under [services/documentation/postman/user-postman.json](../documentation/postman/user-postman.json).
9 changes: 9 additions & 0 deletions services/user/datamodel.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type User {
id: ID! @id
firstName: String!
lastName: String!
email: String! @unique
password: String!
createdAt: DateTime! @createdAt
updatedAt: DateTime! @updatedAt
}
5 changes: 5 additions & 0 deletions services/user/docker-compose.hot-reload.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: '3'
services:
app:
volumes:
- ./src:/app/src
44 changes: 44 additions & 0 deletions services/user/docker-compose.local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: '3'
services:
app:
ports:
- '${SERVER_PORT:-3506}:3500'
networks:
- kafka_broker

prisma:
networks:
- postgres
environment:
PRISMA_CONFIG: |
port: 4466
managementApiSecret: ${PRISMA_MANAGEMENT_API_SECRET:-wsflk897dh%£}
databases:
default:
connector: postgres
host: postgres
port: 5432
user: postgres
password: postgres
postgres:
image: postgres:10.3
restart: always
networks:
- postgres
environment:
POSTGRES_DB: prisma
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- postgres-${ENV:-dev}:/var/lib/postgresql/data

networks:
kafka_broker:
external: true
postgres:
driver: bridge

volumes:
postgres-dev:
postgres-test:
42 changes: 42 additions & 0 deletions services/user/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
version: "3"
services:
app:
build: .
command: sh -c "./wait-for-it.sh -t 15 prisma:4466 --strict -- prisma generate && prisma deploy && yarn lint-check && yarn ${ENV:-dev}"
volumes:
- /app/node_modules
- /app/generated/prisma-client
networks:
- app
depends_on:
- prisma
logging:
driver: ${LOGGING_DRIVER:-json-file}
environment:
PRISMA_MANAGEMENT_API_SECRET: ${PRISMA_MANAGEMENT_API_SECRET:-wsflk897dh%£}
restart: always

prisma:
image: prismagraphql/prisma:1.34
restart: always
networks:
- app
logging:
driver: ${LOGGING_DRIVER:-json-file}
environment:
PRISMA_CONFIG: |
port: 4466
managementApiSecret: ${PRISMA_MANAGEMENT_API_SECRET:-wsflk897dh%£}
databases:
default:
connector: postgres
host: ${DB_HOST}
port: ${DB_PORT}
database: ${DB_NAME}
schema: public
user: ${DB_USER}
password: ${DB_PASSWORD}
networks:
app:
driver: bridge
43 changes: 43 additions & 0 deletions services/user/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "user",
"description": "",
"version": "1.0.0",
"license": "MIT",
"main": "src/index.js",
"scripts": {
"start": "node src/index.js",
"dev": "nodemon src/index.js",
"prod": "node src/index.js",
"test": "jest --testTimeout=20000 --runInBand",
"lint-check": "eslint ./ --ext js",
"lint-fix": "eslint ./ --ext js --fix"
},
"dependencies": {
"body-parser": "^1.18.3",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-routes-versioning": "^1.0.1",

"prisma-client-lib": "^1.34.10",
"winston": "^3.2.1"
},
"devDependencies": {
"eslint": "^5.3.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.16.0",
"jest": "^25.2.2",
"nodemon": "^1.18.9",
"supertest": "^4.0.2"
},
"jest": {
"testEnvironment": "node",
"collectCoverage": true,
"coverageReporters": [
"text-summary"
],
"coveragePathIgnorePatterns": [
"/node_modules/",
"/generated/"
]
}
}
5 changes: 5 additions & 0 deletions services/user/prisma.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
endpoint: http://prisma:4466
datamodel: datamodel.prisma
generate:
- generator: javascript-client
output: ./generated/prisma-client/
30 changes: 30 additions & 0 deletions services/user/src/controllers/createUser.ctrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const winston = require('winston');

/**
* Check if the body of the request contains the good elements
*/
const bodyIsValid = (body) => {
const { firstName, lastName, email, password } = body;
if (!firstName || !lastName || !email || !password) {
return false;
}
return true;
};

/**
* Create a user
* @see PUT /users
*/
module.exports.createUser = async (req, res) => {
if (bodyIsValid(req.body)) {
try {
// TODO:
res.status(200).send({});
} catch (e) {
winston.error(e);
res.status(500).send({ message: e });
}
} else {
res.status(400).send({ message: 'Invalid body format' });
}
};
15 changes: 15 additions & 0 deletions services/user/src/controllers/deleteUser.ctrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const winston = require('winston');

/**
* Delete a user
* @see DELETE /users/:id
*/
module.exports.deleteUser = async (req, res) => {
try {
// TODO:
res.status(200).send({});
} catch (e) {
winston.error(e);
res.status(500).send({ message: e });
}
};
15 changes: 15 additions & 0 deletions services/user/src/controllers/getUserById.ctrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const winston = require('winston');

/**
* Get a user by id
* @see GET /users/:id
*/
module.exports.getUserById = async (req, res) => {
try {
// TODO:
res.status(200).send({});
} catch (e) {
winston.error(e);
res.status(500).send({ message: e });
}
};
15 changes: 15 additions & 0 deletions services/user/src/controllers/getUsers.ctrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const winston = require('winston');

/**
* Get users
* @see GET /users
*/
module.exports.getUsers = async (req, res) => {
try {
// TODO:
res.status(200).send({});
} catch (e) {
winston.error(e);
res.status(500).send({ message: e });
}
};
5 changes: 5 additions & 0 deletions services/user/src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.getUsers = require('./getUsers.ctrl').getUsers;
exports.getUserById = require('./getUserById.ctrl').getUserById;
exports.createUser = require('./createUser.ctrl').createUser;
exports.updateUserById = require('./updateUserById.ctrl').updateUserById;
exports.deleteUser = require('./deleteUser.ctrl').deleteUser;
30 changes: 30 additions & 0 deletions services/user/src/controllers/updateUserById.ctrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const winston = require('winston');

/**
* Check if the body of the request contains the good elements
*/
const bodyIsValid = (body) => {
const { firstName, lastName, email, password } = body;
if (!firstName || !lastName || !email || !password) {
return false;
}
return true;
};

/**
* Update a user by id
* @see PUT /users/:id
*/
module.exports.updateUserById = async (req, res) => {
if (bodyIsValid(req.body)) {
try {
// TODO:
res.status(200).send({});
} catch (e) {
winston.error(e);
res.status(500).send({ message: e });
}
} else {
res.status(400).send({ message: 'Invalid body format' });
}
};
9 changes: 9 additions & 0 deletions services/user/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const winston = require('winston');
const app = require('./server');

/**
* Start the server
*/
app.listen(3500, () => {
winston.info('Server is running on: http://localhost:3500');
});
3 changes: 3 additions & 0 deletions services/user/src/repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { prisma } = require('../generated/prisma-client');

// TODO: add your prisma queries here
Loading

0 comments on commit 53333c6

Please sign in to comment.