|
| 1 | +import log from 'loglevel'; |
1 | 2 | import Country from 'interfaces/Country'; |
2 | 3 | import HttpError from 'utils/HttpError'; |
3 | 4 | import BaseRepository from './BaseRepository'; |
@@ -88,48 +89,17 @@ export default class CountryRepository extends BaseRepository<Country> { |
88 | 89 | regionName = 'South America'; |
89 | 90 | break; |
90 | 91 | default: |
91 | | - regionName = ''; |
| 92 | + regionName = 'Global'; |
92 | 93 | } |
93 | 94 |
|
94 | | - let sql: string; |
95 | | - |
96 | | - if (regionName === '') { |
97 | | - sql = ` |
98 | | - select r2.*,region.name, ST_AsGeoJSON(centroid) as centroid from ( |
99 | | - select count(r.region_id) as planted,r.region_id as region_id from ( |
100 | | - select distinct(tree_id),region_id from public.active_tree_region |
101 | | - left join region_type on type_id = region_type.id |
102 | | - where region_type.type = 'country') r |
103 | | - group by region_id |
104 | | - order by planted desc |
105 | | - limit ${10}) r2 |
106 | | - left join region |
107 | | - on r2.region_id = region.id |
108 | | - `; |
109 | | - } else { |
110 | | - sql = ` |
111 | | - select r.*, region.name, ST_AsGeoJSON(region.centroid) as centroid from ( |
112 | | - select count(region.id) as planted, region.id |
113 | | - from ( |
114 | | - select trees.* from trees, region c |
115 | | - where c.name = '${regionName}' and ST_WITHIN(trees.estimated_geometric_location, c.geom) |
116 | | - ) as trees_in_continent |
117 | | - LEFT JOIN region |
118 | | - on ST_WITHIN(trees_in_continent.estimated_geometric_location, region.geom) |
119 | | - left join region_type |
120 | | - on region.type_id = region_type.id |
121 | | - where |
122 | | - region_type.type = 'country' |
123 | | - group by region.id |
124 | | - order by count(region.id) desc |
125 | | - limit 10 |
126 | | - ) r left join region |
127 | | - on r.id = region.id |
128 | | - `; |
129 | | - } |
| 95 | + const sql = "select * from webmap.config where name = 'country-leader-board'"; |
130 | 96 |
|
131 | 97 | const object = await this.session.getDB().raw(sql); |
132 | | - return object.rows; |
| 98 | + if (object.rows.length === 0) { |
| 99 | + throw new HttpError(404, `Can not found data`); |
| 100 | + } |
| 101 | + log.warn('countries result:', object); |
| 102 | + return object.rows[0].data[regionName]; |
133 | 103 |
|
134 | 104 | // The sql is too slow, need to optimize TODO |
135 | 105 | // return [ |
|
0 commit comments