Skip to content

Commit 3331eb9

Browse files
committed
Agregado estadisticas en el controlador
1 parent 729a2ff commit 3331eb9

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

.tmp/data.db

0 Bytes
Binary file not shown.

api/taxon/config/routes.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
"policies": []
2525
}
2626
},
27+
{
28+
"method": "GET",
29+
"path": "/taxons/:id/statistics",
30+
"handler": "taxon.statistics",
31+
"config": {
32+
"policies": []
33+
}
34+
},
2735
{
2836
"method": "POST",
2937
"path": "/taxons",

api/taxon/controllers/taxon.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
'use strict';
22

3+
const { sanitizeEntity } = require('strapi-utils');
4+
35
/**
46
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers)
57
* to customize this controller
68
*/
79

8-
module.exports = {};
10+
module.exports = {
11+
async statistics(ctx) {
12+
const months = new Array(12).fill(0);
13+
const observations = await strapi.query('observation').find({ 'taxon.id': ctx.params.id });
14+
const weather = observations.filter(obs => obs.weather != null).map(obs => obs.weather);
15+
16+
observations.forEach(observation => { months[(new Date(observation.date)).getMonth()]++ })
17+
18+
const data = {
19+
date_count: months,
20+
temp: {
21+
min: Math.min(...weather.map(w => w.main.temp)),
22+
max: Math.max(...weather.map(w => w.main.temp))
23+
}
24+
}
25+
ctx.send(data)
26+
}
27+
};

0 commit comments

Comments
 (0)