Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full project update including hapi 20.1.3 #4

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
'env': {
'commonjs': true,
'node': true,
},
'extends': '@fbluemle',
'globals': {
},
'parserOptions': {
'ecmaVersion': 11,
},
};
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: ci
on:
push:
branches: ['*']
pull_request:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node: ['12', '14', '16']
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm run lint
- run: npm test
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
node_modules
.DS_Store
node_modules/
yarn-error.log
.idea/
*.iml
.vscode/
3 changes: 3 additions & 0 deletions .labrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
globals: 'AggregateError,atob,btoa,AbortController,AbortSignal,EventTarget,Event,MessageChannel,MessagePort,MessageEvent,performance',
};
35 changes: 25 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
# car-buying-service

This a node/HapiJS server that supports the car-buying app.
It provides a set of service end points that support create/update endpoints for the data used in the car-buying app.
[![ci][1]][2]

To run the server
A Node.js/[hapi][3] server that supports the car-buying app.
It provides a set of service end points that support create/update endpoints
for the data used in the car-buying app.

```bash
## Usage

To run the server:

```sh
npm start
```

## API ENDPOINTS:
Run the server in dev mode (nodemon):

```sh
npm dev
```

## API Endpoints

| Path | Method | Purpose |
| --- | --- | --- |
| /vehicles | GET | Return all vehicles in record. |
| /transactions | GET | Return all transactions in record. |
| /get-negotiations | GET | Return all transactions of type Negotiation. |
| /create-transaction | POST | Create a new transaction. |
| /update-transaction | POST | Update a transaction. |
| /vehicles | GET | Return all vehicles in record |
| /transactions | GET | Return all transactions in record |
| /get-negotiations | GET | Return all transactions of type Negotiation |
| /create-transaction | POST | Create a new transaction |
| /update-transaction | POST | Update a transaction |

[1]: https://github.com/electrode-io/car-buying-service/workflows/ci/badge.svg
[2]: https://github.com/electrode-io/car-buying-service/actions
[3]: https://hapi.dev/
165 changes: 3 additions & 162 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,164 +1,5 @@
"use strict";
'use strict';

const Hapi = require("hapi");
const Inert = require("inert");
const fs = require("fs");
const path = require("path");
const _ = require("lodash");
const HTTP_CREATED = 201;
const { start } = require('./src/server');

const server = new Hapi.Server();
server.connection({ port: 8000, host: "localhost" });

server.register(
{
register: require("inert")
},
function(err) {
if (err) throw err;

server.start(err => {
if (err) {
throw err;
}
console.log(`Server running at: ${server.info.uri}`);
});
}
);

server.route({
method: "GET",
path: "/images/{param*}",
handler: {
directory: {
path: "images"
}
}
});
server.route({
method: "GET",
path: "/",
handler: function(request, reply) {
reply("Hello! from the car-buying-service.\n");
}
});

server.route({
method: "GET",
path: "/vehicles",
handler: (request, reply) => {
fs.readFile(path.resolve("data", "vehicles.json"), "utf8", (err, data) => {
if (err) {
console.log("Vehicles READ ERROR");
return reply("Vehicles READ ERROR").code(500);
}
return reply(JSON.parse(data));
});
}
});

server.route({
method: "GET",
path: "/transactions",
handler: (request, reply) => {
fs.readFile(path.resolve("data", "transactions.json"), "utf8", (err, data) => {
if (err) {
console.log("Transactions READ ERROR");
return reply();
}
return reply(JSON.parse(data));
});
}
});

server.route({
method: "GET",
path: "/get-negotiations",
handler: (request, reply) => {
fs.readFile(path.resolve("data", "transactions.json"), "utf8", (err, data) => {
if (err) {
console.log("Transactions READ ERROR");
return reply();
}
const parsedData = JSON.parse(data);
const found = _.filter(parsedData, { status: "NEGOTIATION" });
if (!_.isEmpty(found)) {
return reply(found);
} else {
return reply();
}
});
}
});

server.route({
method: "POST",
path: "/create-transaction",
handler: (request, reply) => {
fs.readFile(path.resolve("data", "transactions.json"), "utf8", (readErr, data) => {
if (readErr) {
console.log("Transactions READ ERROR");
return reply("Transactions UPDATE ERROR");
}
const parsedData = JSON.parse(data);
const payload = request.payload;
payload.id = Date.now();
payload.customer_id = parseInt(payload.customer_id);
parsedData.push(payload);

fs.writeFile(
path.join(process.cwd(), "data/transactions.json"),
JSON.stringify(parsedData),
"utf-8",
writeErr => {
if (writeErr) {
return reply("Write Error").code(HTTP_ISE);
} else {
return reply(payload).code(HTTP_CREATED);
}
}
);
});
}
});

server.route({
method: "POST",
path: "/update-transaction",
handler: (request, reply) => {
fs.readFile(path.resolve("data", "transactions.json"), "utf8", (readErr, data) => {
if (readErr) {
console.log("Transactions READ ERROR");
return reply("Transactions UPDATE ERROR");
}
const parsedData = JSON.parse(data);
const found = _.find(parsedData, {
id: parseInt(request.payload.id),
status: "NEGOTIATION"
});

if (!_.isEmpty(found)) {
if (request.payload.comments) {
found.comments = request.payload.comments;
}
if (request.payload.status) {
found.status = request.payload.status;
}
fs.writeFile(
path.join(process.cwd(), "data/transactions.json"),
JSON.stringify(parsedData),
"utf-8",
writeErr => {
if (writeErr) {
return reply("Write Error").code(HTTP_ISE);
} else {
return reply(found).code(HTTP_CREATED);
}
}
);
} else {
return reply("NOT FOUND").code(HTTP_CREATED);
}
});
}
});
start();