Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
Loopback-API-Example
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Harbor Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Henrique Varella Ehrenfried
Loopback-API-Example
Merge requests
!6
Csv download
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Csv download
SMPPIR/Loopback-API-Example:CSV_download
into
master
Overview
0
Commits
5
Pipelines
0
Changes
2
Open
ns17
requested to merge
SMPPIR/Loopback-API-Example:CSV_download
into
master
5 years ago
Overview
0
Commits
5
Pipelines
0
Changes
2
Expand
0
0
Merge request reports
Viewing commit
8115d8b6
Prev
Next
Show latest version
2 files
+
166
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
8115d8b6
Adding csv download of geolocation data and category
· 8115d8b6
ns17
authored
5 years ago
common/models/category.js
+
61
−
0
Options
@@ -21,4 +21,65 @@ along with SMPPIR-CheckIn. If not, see <https://www.gnu.org/licenses/>.
'
use strict
'
;
module
.
exports
=
function
(
Category
)
{
Category
.
csvexport
=
function
(
type
,
res
,
callback
)
{
var
datetime
=
new
Date
();
var
datetimeFuture
=
new
Date
(
datetime
.
getFullYear
(),
datetime
.
getMonth
(),
datetime
.
getDate
()
+
7
)
res
.
set
(
'
Expires
'
,
datetimeFuture
+
''
);
res
.
set
(
'
Cache-Control
'
,
'
max-age=0, no-cache, must-revalidate, proxy-revalidate
'
);
res
.
set
(
'
Last-Modified
'
,
datetime
+
''
);
res
.
set
(
'
Content-Type
'
,
'
application/force-download
'
);
res
.
set
(
'
Content-Type
'
,
'
application/octet-stream
'
);
res
.
set
(
'
Content-Type
'
,
'
application/download
'
);
res
.
set
(
'
Content-Disposition
'
,
'
attachment;filename=category.csv
'
);
res
.
set
(
'
Content-Transfer-Encoding
'
,
'
binary
'
);
Category
.
find
(
function
(
err
,
categories
)
{
// conversão JSON para CSV
var
itemsFormatted
=
[];
// cabeçalho do csv
var
headers
=
{
category_name
:
"
category_name
"
,
category_description
:
"
category_description
"
,
id
:
"
id
"
};
categories
.
forEach
((
item
)
=>
{
itemsFormatted
.
push
({
category_name
:
item
.
category_name
,
category_description
:
item
.
category_description
,
id
:
item
.
id
});
});
if
(
headers
)
{
itemsFormatted
.
unshift
(
headers
);
}
var
jsonObject
=
JSON
.
stringify
(
itemsFormatted
);
var
array
=
typeof
jsonObject
!=
'
object
'
?
JSON
.
parse
(
jsonObject
)
:
jsonObject
;
var
csv
=
''
;
for
(
var
i
=
0
;
i
<
array
.
length
;
i
++
)
{
var
line
=
''
;
for
(
var
index
in
array
[
i
])
{
if
(
line
!=
''
)
line
+=
'
;
'
// aqui muda o separador do csv
line
+=
array
[
i
][
index
];
}
csv
+=
line
+
'
\r\n
'
;
}
res
.
send
(
csv
);
});
};
Category
.
remoteMethod
(
'
csvexport
'
,
{
accepts
:
[
{
arg
:
'
type
'
,
type
:
'
string
'
,
required
:
false
},
{
arg
:
'
res
'
,
type
:
'
object
'
,
'
http
'
:
{
source
:
'
res
'
}}
],
returns
:
{},
// Para usar o 'type' -> http: {path: '/csv/:type', verb: 'get'}
http
:
{
path
:
'
/csv
'
,
verb
:
'
get
'
},
});
};
Loading