File tree Expand file tree Collapse file tree 3 files changed +28
-1
lines changed
Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments