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

Adds back the support for loading Entries with CSV and TEXT formats #4114

Merged
merged 4 commits into from
Dec 3, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adds back the support for loading Entries with CSV and TEXT formats
  • Loading branch information
sulkaharo committed Dec 2, 2018
commit 6c66e8ca47e8216f8a78c038862cda35f20d5e31
50 changes: 37 additions & 13 deletions lib/api/entries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,27 +165,51 @@ function configure(app, wares, ctx) {
return;
}

function formatWithSeparator(data, separator) {
var outputdata = [];
data.forEach(function(e) {
var entry = {
"dateString": e.dateString,
"date": e.date,
"sgv": e.sgv,
"direction": e.direction,
"device": e.device
};
outputdata.push(entry);
});

var fields = Object.keys(outputdata[0]);
var replacer = function(key, value) { return value === null ? '' : value }
var csv = outputdata.map(function(row){
return fields.map(function(fieldName){
return JSON.stringify(row[fieldName], replacer)
}).join(separator)
});
return csv.join('\r\n');
}


// if no error, format the payload
// The general pattern here is to create an output stream that reformats
// the data correctly into the desired representation.
// The stream logic allows some streams to ensure that some basic rules,
// such as enforcing a type property to exist, are followed.
return res.format({
text: function() {
res.status(405).send({
status: 405,
message: 'Method Not Allowed (only json is supported)',
type: 'internal'
});
'text/plain': function() {
es.pipeline(output, force_typed_data(type_params), es.writeArray(function(err, out) {
var output = formatWithSeparator(out,"\t");
console.log(output);
res.send(output);
}));
},
csv: function() {
res.status(405).send({
status: 405,
message: 'Method Not Allowed (only json is supported)',
type: 'internal'
});
'text/csv': function() {
es.pipeline(output, force_typed_data(type_params), es.writeArray(function(err, out) {
var output = formatWithSeparator(out,',');
console.log(output);
res.send(output);
}));
},
json: function() {
'application/json': function() {
// so long as every element has a `type` field, and some kind of
// date, we'll consider it valid
es.pipeline(output, force_typed_data(type_params), es.writeArray(function(err, out) {
Expand Down