Commit 6fb32b4d by RNH

вывод транзакций, смена статуса ответа, обновление даты

parents
module.exports = {
url: 'mongodb://localhost:27017/'
};
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Transactions Log</title>
</head>
<body>
<button id="getTransactions">show transactions</button>
<button id="changeStatus">change response status</button>
<span id="status">(current: true)</span>
<script>
const getTransactionsBtn = document.getElementById('getTransactions');
const changeStatusBtn = document.getElementById('changeStatus');
const status = document.getElementById('status');
getTransactionsBtn.addEventListener('click', (e) => {
fetch('http://localhost:3000/getTransactions')
.then(res => res.json())
.then(json => {
const strings = json.transactions.map(transactionObj =>
Object.keys(transactionObj).reduce((acc, key) =>
`${acc} ${key}:${transactionObj[key]}`, ``));
const pElements = strings.map(string => `<p>${string}</p>`);
document.body.innerHTML = document.body.innerHTML + pElements.join('');
});
});
changeStatusBtn.addEventListener('click', (e) => {
fetch('http://localhost:3000/changeStatus')
.then(res => res.json())
.then(json => {
status.innerHTML = `(current: ${json.responseStatus})`;
});
});
</script>
</body>
</html>
\ No newline at end of file
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const dbConfig = require('./config/db');
const MongoClient = require('mongodb').MongoClient;
const mongoose = require('mongoose');
const port = 3000;
const transactions = [];
let responseStatus = true;
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(port, function () {
console.log('App listening on port 3000!');
});
app.get('/', function (req, res) {
// Transaction.find(function (err, transactions) {
// if (err) return console.error(err);
// console.log(transactions);
// });
res.sendFile(__dirname + '/index.html');
});
app.post('/', function (req, res) {
if (Object.keys(req.body).length === 0) {
res.status(400).send('Failed, request body is empty!');
}
// const { transctionId, amount, createdAt, updatedAt, status } = req.body;
// const transaction = new Transaction({
// transctionId,
// amount,
// createdAt,
// updatedAt,
// status,
// });
// transaction.save(function (err, transaction) {
// if (err) return console.error(err);
// });
const transactionExists = transactions.reduce((acc, trObj, i) => {
if (acc) return acc;
return trObj.transactionId === req.body.transactionId ? i : false;
}, false);
if (transactionExists) {
transactions[transactionExists] = {
...transactions[transactionExists],
updatedAt: Date.now(),
status: responseStatus
};
res.send(transactions[transactionExists]);
} else {
const newTransaction = {
...req.body,
createdAt: Date.now(),
updatedAt: Date.now(),
status: responseStatus
};
transactions.push(newTransaction);
res.send(newTransaction);
}
});
app.get('/getTransactions', function (req, res) {
res.json({ transactions });
});
app.get('/changeStatus', function (req, res) {
responseStatus = !responseStatus;
res.json({ responseStatus });
});
// mongoose.connect(dbConfig.url);
// const db = mongoose.connection;
// db.on('error', console.error.bind(console, 'connection error:'));
// db.once('open', function () {
// console.log('Mongoose connection opened');
// });
// const transactionSchema = mongoose.Schema({
// transctionId: String,
// ammount: Number,
// createdAt: Number,
// updatedAt: Number,
// status: Boolean,
// });
// const Transaction = mongoose.model('Transaction', transactionSchema);
\ No newline at end of file
{
"name": "transactionserver",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.3",
"mongodb": "^3.0.4",
"mongoose": "^5.0.10"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment