Skip to content

Commit 7177029

Browse files
committed
add edit water consumption.
1 parent 5e3bb82 commit 7177029

23 files changed

+1458
-14896
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
22

33
# repositories
4-
/doc
4+
/documentation
55
# dependencies
66
/node_modules
77
/.pnp

.vscode/launch.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Utilisez IntelliSense pour en savoir plus sur les attributs possibles.
3+
// Pointez pour afficher la description des attributs existants.
4+
// Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceFolder}/server.js",
12+
"runtimeExecutable": "/home/max/.config/nvm/versions/node/v10.20.1/bin/node"
13+
}
14+
]
15+
}

Consumption.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function Consumption() {
2+
this.date = null;
3+
this.m3 = null;
4+
5+
/** Parse data from req.body
6+
* @param {object} body
7+
*/
8+
Consumption.prototype.parse = function(body) {
9+
if (body) {
10+
if (body.consumptionDate && body.consumptionM3) {
11+
this.date = body.consumptionDate;
12+
this.m3 = body.consumptionM3;
13+
return;
14+
}
15+
}
16+
17+
this.date = null;
18+
this.m3 = null;
19+
};
20+
21+
/**
22+
* Get consumption as CSV line
23+
*/
24+
Consumption.prototype.getAsCSV = function() {
25+
return this.date + ";" + this.m3 + "\n";
26+
};
27+
}
28+
29+
module.exports = Consumption;

DataHandler.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const fs = require("fs");
2+
const csvParse = require("csv-parse/lib/sync");
3+
const csvStringify = require("csv-stringify");
4+
5+
function DataHandler() {
6+
const fileName = "./consumption.csv";
7+
const fileEncoding = "utf8";
8+
const csvDelimiter = ";";
9+
10+
/**
11+
* Read and parse CSV file
12+
*/
13+
function read() {
14+
const content = fs.readFileSync(fileName, fileEncoding);
15+
const parsedCSV = csvParse(content, {
16+
delimiter: csvDelimiter,
17+
columns: true
18+
});
19+
return parsedCSV;
20+
}
21+
22+
/**
23+
* Find and replace a consumption within a list which as the same date
24+
* @param {*} consumptions list of consumption
25+
* @param {*} consumption the consumption which wil be used for the replacement
26+
*/
27+
function replaceByDate(consumptions, consumption) {
28+
if (consumptions instanceof Array && consumptions.length > 0) {
29+
return consumptions.map(input => {
30+
if (consumption.date === input.date) {
31+
input.m3 = consumption.m3;
32+
}
33+
return input;
34+
});
35+
}
36+
}
37+
38+
/**
39+
* write file
40+
* @returns {Promise}
41+
*/
42+
function write(consumptions) {
43+
return new Promise((resolve, reject) => {
44+
csvStringify(
45+
consumptions,
46+
{
47+
columns: [{ key: "date" }, { key: "m3" }],
48+
header: true,
49+
delimiter: csvDelimiter
50+
},
51+
(err, output) => {
52+
fs.writeFile(fileName, output, { flag: "w" }, err => {
53+
if (err) {
54+
console.log("Could not write data to consumption.csv");
55+
reject();
56+
return;
57+
}
58+
resolve();
59+
});
60+
if (err) {
61+
console.log("Could not write data to consumption.csv");
62+
reject();
63+
}
64+
}
65+
);
66+
});
67+
}
68+
69+
/**
70+
* Append a new consumption to the end of the file
71+
* @param {*} consumption the consumption to append
72+
*/
73+
function append(consumption) {
74+
const line = consumption.getAsCSV();
75+
76+
fs.writeFile(fileName, line, { flag: "a+" }, err => {
77+
if (err) {
78+
console.log("Could not write data to consumption.csv");
79+
}
80+
});
81+
}
82+
83+
return {
84+
append,
85+
read,
86+
replaceByDate,
87+
write
88+
};
89+
}
90+
91+
module.exports = DataHandler;

README.md

-44
This file was deleted.

consumption.csv

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
date;m3
2+
2020-12-18;100
3+
2020-12-20;123
4+
2020-12-25;222
5+
2020-12-26;300
6+
2020-12-10;50
7+
2020-12-24;250
8+
2020-12-23;300
9+
2020-12-22;400
10+
2020-12-11;500
11+
2020-12-31;600
12+
2020-12-01;20
13+
2020-12-02;123
14+
2020-12-03;300
15+
2020-12-04;300
16+
2020-12-05;400
17+
2020-12-05;500
18+
2020-12-06;456
19+
2020-12-07;666
20+
2020-11-02;444
21+
2020-11-03;132
22+
2020-11-04;500
23+
2020-11-05;100
24+
2020-11-06;900
25+
2020-11-11;300
26+
2020-11-20;500
27+
2020-11-23;1000

0 commit comments

Comments
 (0)